在handlebars模板中访问javascript变量

accessing a javascript variable in handlebars template

本文关键字:访问 javascript 变量 handlebars      更新时间:2023-09-26

我得到一个php变量在javascript

window.apiUserId = '<?php echo Yii::$app->user->id ?>';

我有一个车把模板作为

<script type="text/x-handlebars">
        {{log apiUserId}}
        {{outlet}}
    </script>

但是控制台显示"undefined"

来自文档:

var context = {title: "My New Post", body: "This is my first post!"}
var html    = template(context);
因此:

var context = {apiUserId: window.apiUserId};
var html    = template(context);

我想你可能会设法使:

var html    = template(window);

…工作,但挥霍这么多的任意数据到函数是一个很好的配方意想不到的结果。