admin管理员组文章数量:1332351
I am trying to do some JavaScript with Smarty and using JavaScript variable and checking if the value is contained within a Smarty array.
Below is the code that I have:
if (object.data[i].Application === selectedApplication || {if isset($Applications) && $Applications|@count > 0} true {else} false {/if})
{
{if not isset($Applications)}
$("#cboApplications").append("<option selected value=\""+object.data[i].id+"\">"+object.data[i].Application+"</option>");
{else}
{if '9'|in_array:$Applications}
console.log("id " + id + " in array");
$("#cboApplications").append("<option selected value=\""+object.data[i].id+"\">"+object.data[i].Application+"</option>");
{else}
console.log("id " + id + " not in array");
$("#cboApplications").append("<option value=\""+object.data[i].id+"\">"+object.data[i].Application+"</option>");
{/if}
{/if}
}
The issue I am having with is {if '9'|in_array:$Applications}
. At the moment I've hard-coded the value instead of using the JavaScript variable as a test but I get the same results.
Below is what the array looks like when printed from inside Smarty:
array (
0 => '9',
)
There are multiple values in this list box cboApplications
which have a value of 8, 5, 6, 9 and 10.
For some reason, even though only 9 is in the $Applications array, the console.log is acting as if every single item is inside the array, not just 9.
I am trying to do some JavaScript with Smarty and using JavaScript variable and checking if the value is contained within a Smarty array.
Below is the code that I have:
if (object.data[i].Application === selectedApplication || {if isset($Applications) && $Applications|@count > 0} true {else} false {/if})
{
{if not isset($Applications)}
$("#cboApplications").append("<option selected value=\""+object.data[i].id+"\">"+object.data[i].Application+"</option>");
{else}
{if '9'|in_array:$Applications}
console.log("id " + id + " in array");
$("#cboApplications").append("<option selected value=\""+object.data[i].id+"\">"+object.data[i].Application+"</option>");
{else}
console.log("id " + id + " not in array");
$("#cboApplications").append("<option value=\""+object.data[i].id+"\">"+object.data[i].Application+"</option>");
{/if}
{/if}
}
The issue I am having with is {if '9'|in_array:$Applications}
. At the moment I've hard-coded the value instead of using the JavaScript variable as a test but I get the same results.
Below is what the array looks like when printed from inside Smarty:
array (
0 => '9',
)
There are multiple values in this list box cboApplications
which have a value of 8, 5, 6, 9 and 10.
For some reason, even though only 9 is in the $Applications array, the console.log is acting as if every single item is inside the array, not just 9.
Share Improve this question edited Jun 16, 2019 at 9:31 halfer 20.3k19 gold badges109 silver badges202 bronze badges asked May 30, 2015 at 22:20 BoardyBoardy 36.2k108 gold badges271 silver badges458 bronze badges4 Answers
Reset to default 3You have to use {literal}
in Smarty for JavaScript. Example:
{literal}
<script type="text/javascript">
document.write("{/literal}{$text}{literal}");
</script>
{/literal}
For more information, see Smarty Documentation.
Can you maybe try something like this:
if (object.data[i].Application === selectedApplication || {if isset($Applications) && $Applications|@count > 0} true {else} false {/if})
{
{if not isset($Applications)}
$("#cboApplications").append("<option selected value=\""+object.data[i].id+"\">"+object.data[i].Application+"</option>");
{else}
{if $Applications.indexOf('9') != -1}
console.log("id " + id + " in array");
$("#cboApplications").append("<option selected value=\""+object.data[i].id+"\">"+object.data[i].Application+"</option>");
{else}
console.log("id " + id + " not in array");
$("#cboApplications").append("<option value=\""+object.data[i].id+"\">"+object.data[i].Application+"</option>");
{/if}
{/if}
}
I replaced inArray with javascript indexOf, so maybe that can help.
You are always checking if 9 is in the $Applications
array, so it always passes because we know that 9 is in the array.
Can you provide the actual code which is checking the variable instead 9? Also keep in mind that the smarty code is executed on the server while the JavaScript does not so I can't really imagine what are you trying to achieve. Actual code might help.
<script>
var apps = [];
{if isset($Applications)}
apps = {$Applications|json_encode};
{/if}
if($.inArray(9, apps)) {
console.log('....');
}
</script>
本文标签: phpJavaScript variable and SmartyStack Overflow
版权声明:本文标题:php - JavaScript variable and Smarty - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742320710a2452727.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论