[英]accessing a javascript variable in handlebars template
I am getting a php varible in javascript as 我在JavaScript中获取了一个PHP变量
window.apiUserId = '<?php echo Yii::$app->user->id ?>';
And I have a handlebar template as 我有一个车把模板
<script type="text/x-handlebars">
{{log apiUserId}}
{{outlet}}
</script>
But the console displays "undefined" 但是控制台显示“未定义”
How to log javascript variables in handlebars ? 如何在车把中记录javascript变量?
From the documentation : 从文档中 :
var context = {title: "My New Post", body: "This is my first post!"}
var html = template(context);
Therefore: 因此:
var context = {apiUserId: window.apiUserId};
var html = template(context);
I suppose you might manage to make: 我想您可能会做到:
var html = template(window);
… work, but splurging that much arbitrary data into the function is a good recipe for unexpected results. …可行,但是将大量任意数据浪费到函数中是获得意外结果的好方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.