admin管理员组文章数量:1287650
I am really starting with Smarty and I do not understand this fact:
if I put the next code inside my template index.tpl
<script type="text/javascript">
function toAlert() {
alert('{$text}' );
}
</script>
I can access to the function toAlert and show the content of Smarty variable {{$text}}, but if I put this code into a js file lije javascript.js and I try to access it by putting into de template the link:
I cannot access to the function as well.
Can anyone tell me why or help wher can I find this specific info? thank you!!
I am really starting with Smarty and I do not understand this fact:
if I put the next code inside my template index.tpl
<script type="text/javascript">
function toAlert() {
alert('{$text}' );
}
</script>
I can access to the function toAlert and show the content of Smarty variable {{$text}}, but if I put this code into a js file lije javascript.js and I try to access it by putting into de template the link:
I cannot access to the function as well.
Can anyone tell me why or help wher can I find this specific info? thank you!!
Share Improve this question asked Jul 15, 2014 at 15:28 jmcordobajmcordoba 932 gold badges2 silver badges6 bronze badges 3- 2 php executes before the page is loaded, so the js above actually looks like alert('theactualvalueof$text');. Therefor it cant work the other way arround, as php has already beed parsed when js runs – Steve Commented Jul 15, 2014 at 15:32
- What I finally did was to load the PHP texts n the javaScript variables in the temaplate, like this: var text = '{$text}'; and then I could use the javascript variable text inside the <script> file loaded after the PHP code. – jmcordoba Commented Nov 6, 2014 at 16:20
-
Yes, thats a good solution. Storing them as properties of an object instead of individual variables might be a little cleaner, eg
var data = { text: '{$text}', other: '{$other}'};
Then you can accessalert(data.text + ' ' + data.other);
– Steve Commented Nov 6, 2014 at 16:30
2 Answers
Reset to default 4Smarty 2 requires escaping of the "{" and "}" characters, you can use {ldelim} and {rdelim} to escape them individually or wrap entire blocks of text with {literal}{/literal}. It's usually cleaner to use {ldelim} and {rdelim} when there is embedded smarty tags, so example:
<script type="text/javascript">
function toAlert() {ldelim}
alert('{$text}');
{rdelim}
</script>
Smarty 3 conveniently ignores "{" and "}" characters surrounded by white space, so your javascript example would work as-is.
You should put this code intp TPL file to make this work. Only TPL files are processed as Smarty files and you can use there Smarty variables.
Your code put should work out of the box in your index.tpl file but if it weren't try:
<script type="text/javascript">
{literal}
function toAlert() {
alert('{/literal}{$text}{literal}' );
}
{/literal}
</script>
本文标签: phpSmarty variable inside javascriptStack Overflow
版权声明:本文标题:php - Smarty variable inside javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741315317a2371878.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论