如何将变量从php模板传递给javascript [英] how to pass variable from php template to javascript

查看:140
本文介绍了如何将变量从php模板传递给javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,我想在地图上显示一些点。我有小模板(比如Smarty,但更轻),模板中有变量$ points,它由我需要的点的坐标组成。我需要将它们传递给javascript(因为只有javascript可以使用点呈现该映射)。



我有3种变体。你能告诉,什么是最好的?


$ b 第1种方式: (模板插入带全局变量的javascript标签)



tpl.php文件:

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

.js文件

 函数renderMap(){
var points = MAP_POINTS; //使用全局。它真的很糟糕吗?或谁在乎? =))
}

第二种方式: (通过HTML元素传递变量)

tpl.php.file

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

.js文件

 函数renderMap(){
//没有全局变量,但需要在本地解析
var points = $ .parseJSON($(#map_points_container).val() );
}

第三种方式: (AJAX-way)

我根本不会在模板文件中传递 $ this-> points 。我有另一个.php文件来处理我所有的AJAX请求:

Ajaxing.php

  function get_map_points($ params){
//一些操作
返回json_encode($ map_points);
}

与本地相比,我会有这样的事情:



.js文件

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

第三种方法通常是,但如果我已经将某些值从模板传递到本地页面,那么我也可以传递和映射点。事实上,我不需要再提出只有这个地图点的要求(这就是为什么我不喜欢第三种方式)。



但可能你可以告诉我其他方式?一个更好的方法?

我选择的方式:



第1种方式,小言论。我的所有'地图渲染'代码都在另一个文件中,就像:
MAP_APP = {};
MAP_APP.some_prop = null; //一些属性
MAP_APP.some_method = function(){}; //一些方法

} );

所以在模板文件中,我只需要扩展我的MAP_APP对象:

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

是的,全局变量。但它就像整个应用程序的命名空间一样。



感谢所有人。

解决方案

<第二种方法增加了额外的处理步骤( parseJSON() c $ c>)这是没有必要的。



第三种方法很好,如果你处理大量可选的数据(即只有在用户请求它,而不是100%确定这是否会发生)或动态。它会创建一个新的请求,但不会立即可用。



如果您不想使用全局变量,您可以将您的JavaScript函数包装到一个对象中,并从PHP填充对象属性:
MyObject.MAP_POINTS =<?php echo json_encode($ this-> points); ?取代;
< / script>


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.

解决方案

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>

这篇关于如何将变量从php模板传递给javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆