Javascript javascript中的智能变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3404800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
smarty variables inside javascript
提问by soField
I am trying to use smarty variables inside javascript inside tpl
我正在尝试在 tpl 中的 javascript 中使用 smarty 变量
{literal}
<script language="javascript">
google.load('visualization','1',{'packages': ['geomap']});
google.setOnLoadCallback(drawMap);
function drawMap() {
var data = new google.visualization.DataTable();
data.addRows(4);
data.addColumn('string', 'Location');
data.addColumn('number', 'Number of links');
{/literal}{foreach from=$last5 item=link name=links key=index}
data.setValue({$index},0,'{$link.location|replace:'\'':'\\''}');
data.setValue({$index},1,{$link.location_count});
{/foreach}{literal}
var options = {};
options['dataMode'] = 'regions';
options['region'] = 'world';
var container = document.getElementById('map');
var geomap = new google.visualization.GeoMap(container);
geomap.draw(data, options);
};
</script>
{/literal}
can you suggest me a solution please
你能给我建议一个解决方案吗
回答by AlexanderMP
Simply close the {literal} tag right before inserting the smarty variable, and re-open it again.
只需在插入 smarty 变量之前关闭 {literal} 标签,然后再次重新打开它。
Or use {ldelim} and {rdelim} for the pieces of code where you assign values from Smarty.
或者使用 {ldelim} 和 {rdelim} 作为您从 Smarty 分配值的代码段。
回答by karvonen
{literal}
function doSomething(myNumber){
var result = myNumber/{/literal}{$myDivider}{literal};
// rest of code...
}
// more functions...
{/literal}
or
或者
{literal}
function doSomething(myNumber){
{/literal}
var result= myNumber/{$myDivider};
{literal}
// rest of code...
}
// more functions...
{/literal}
or
或者
function doSomething(myNumber){ldelim}
var result= myNumber/{$myDivider};
// rest of code below...
{rdelim}
function doSomeMore(another){ldelim}
alert('{$myHello}');
// more code
{rdelim}
OR (with Smarty 3.x and above, no literalsetc necessary)
或(使用 Smarty 3.x 及更高版本,无需文字等)
function doSomething(myNumber){
var result = myNumber/{$myDivider};
// rest of code
}
In Smarty 3 a left curly brace with a space character (space, tab or newline) next to it should not mess with the Smarty logic any more. Problems solved by using the new version :)
在 Smarty 3 中,旁边带有空格字符(空格、制表符或换行符)的左花括号不应再与 Smarty 逻辑混淆。使用新版本解决的问题:)
回答by Mohammed Joraid
After trying (karvonen) answers [I did not try {rdelim}though, only tried {literal}], I got a problem with my ajaxrequests, it stopped fetching any date from server on loading after the smarty breaking in JS. So, what I did is I assigned the smarty value to a hidden field(I know this is not most smart thing to do) and then requested that value from JS and hence assigned it to a variable.
Hope this helps.
在尝试 (karvonen) 回答后 [我没有尝试{rdelim},只是尝试过{literal}],我的ajax请求出现问题,在 JS 智能中断后,它在加载时停止从服务器获取任何日期。所以,我所做的是将 smarty 值分配给 a hidden field(我知道这不是最聪明的做法),然后从 JS 请求该值,从而将其分配给一个变量。希望这可以帮助。

