12

我想使用我的 jsrender 模板中的一个变量来生成一段 javascript,但我不知道如何<script type="text/javascript">在我的<script id="data-item-tpl" type="text/x-jsrender">

希望下面的内容相当清楚。下面的代码产生一个Uncaught SyntaxError. 如果我从嵌入脚本中删除标签,那么它只是作为文本打印到页面上。

在下面的代码中,我试图生成一个 id 为“chartId”的 div,然后是一个脚本,它将通过 MyChart 对象填充该 div 的内容。

<script id="data-item-tpl" type="text/x-jsrender">
    <div class="item">
        <div class="graphs">
            <ul class="thumbnails">
                {{for graphs}}
                <li><div class="thumbnail"><img src="{{html:url}}" width="190" height="120" /><p>{{html:graphTitle}}<br />{{html:value}}</p></div></li>
                {{/for}}
                <!-- Here we insert both the placeholder for, and the javascript to populate charts -->
                <li><div id="{{html:chartId}}" style="width:120;height:190px;"></div></li>
                <script type="text/javascript">
                    var chartObj = new MyChart("{{html:chartId}}");
                    chartObj.render();
                </script>
            </ul>                           
        </div>
        ...
</script>
4

1 回答 1

13

我只是玩了一下,就让它像这样工作:

{{:"<scr" + "ipt type=&quot;text/javascript&quot;>"}}
    var chartObj = new MyChart("{{html:chartId}}");
    chartObj.render();
{{:"</scr" + "ipt>"}}

也许不是最优雅的解决方案,但是当我把它alert放在那里时,它就被解雇了。

于 2012-07-27T16:23:38.833 回答