Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6008243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:47:49+00:00 2026-05-23T01:47:49+00:00

I have a page where I want to display some points on map. I

  • 0

I have a page where I want to display some points on map. I have small templates (like Smarty, but lighter) and there in template I have variable $points, that consists of coordinates of points I need. I need to pass them to javascript (because only javascript can render that map with points).

I have 3 variants of doing it. Can you tell, what is best?

1th way: (Template inserting javascript-tags with global variable)

tpl.php file:

<script>
MAP_POINTS = <?php echo json_encode($this->points); ?>;
</script>

.js file

function renderMap(){
var points = MAP_POINTS; // using global. Is it really bad? or who cares? =))
}

2nd way: (Passing variable through the HTML element)

tpl.php.file

<input type="hidden" 
       value="<?php echo json_encode($this->points); ?>" 
       id="map_points_container">

.js file

function renderMap(){
// without globals, but needed to be parsed on local side
var points = $.parseJSON ( $( "#map_points_container" ).val() );
}

3rd way: (AJAX-way)

I don’t pass $this->points from template file at all. I have another .php file that handles all my AJAX requests:

Ajaxing.php

function get_map_points($params){
// some operations
return json_encode ($map_points);
}

And than on local side I’ll have something like this:

.js file

$.post ( 'ajaxing.php', params, 
         function(points){
           renderMap(points);
         }, 'json');

The third way is usual, but if I already pass some values from template to local page, then I can pass and map points too. In fact, I don’t need to make another request for only this map points (that’s why I don’t like third way)

But may be you can advise me another way? a better way?

The way I choosed:

1th way with little remarks. All my ‘map-rendering’ code is in another file and it’s like:

$(function(){

MAP_APP = {};
MAP_APP.some_prop = null; // some properties
MAP_APP.some_method = function(){}; // some methods

});

So in template file I only have to extend my MAP_APP object:

<script>
MAP_APP.points = <?php echo json_encode($this->points); ?>;
</script>

Yes, global variable. But it’s like namespace of whole application.

Thanks to everybody.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:47:50+00:00Added an answer on May 23, 2026 at 1:47 am

    The first way is definitely the least complicated and fastest.

    The second one adds an additional processing step (the parseJSON()) that isn’t necessary.

    The third way is good if you’re dealing with lots of data that is optional (i.e. is needed only if the user requests it, and it isn’t 100% sure whether that will happen) or dynamic. It creates a new request though, and is not going to be immediately available.

    If you don’t want to use globals, you could e.g. wrap your JavaScript functions into an object, and populate an object property from PHP:

    <script>
    MyObject.MAP_POINTS = <?php echo json_encode($this->points); ?>;
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi-lingual page where I want to display form validation error in
I have an iframe in my page and I want to display the content
In my page i have a block of content where i want to display
I have a list of images which I want to display in a page.
I have a page http://www.mysite.com/image.aspx , that I want to load and display an
I have the password field on page. I want to display text Enter password
I have a section of an html page that I want to display in
I have a page that displays messages and I want it to work just
I have a page that displays posts and I want to get the name
i want to have an iframe, which displays the page given as parameter in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.