我想在页面上附加一个带有id的脚本。Zend文档中的示例似乎有语法错误(第5行):
$template = '<div class="book">{{:title}}</div>';
$this->headScript()->appendScript(
$template,
'text/x-jquery-tmpl',
array('id='tmpl-book', 'noescape' => true)
);
我尝试过array('id' => 'tmpl-book', 'noescape' => true)
,但这不起作用:脚本被添加到页面中,但没有id。
默认情况下,属性上只允许使用'charset', 'defer', 'language', 'src'
,您需要设置$this->headScript()->setAllowArbitraryAttributes(true);
才能将任何属性添加到脚本中;
http://framework.zend.com/manual/1.12/en/zend.view.helpers.html大约在页面中间:
注意:默认情况下禁用任意属性
默认情况下,HeadScript仅渲染由W3C。其中包括"type"、"charset"、"defer"、"language"answers"src"。然而,一些javascript框架,特别是»Dojo,利用了自定义属性以修改行为。允许属性,可以通过setAllowArbitraryAttributes()启用它们方法:
$this->headScript()->setAllowArbitraryAttributes(true);
使用占位符是正确的!
请在此处阅读Zend Helper占位符:http://framework.zend.com/manual/1.12/en/learning.view.placeholders.basics.html
我在结束标签前添加了layout.phtml:
您可以在脚本中任意位置添加行
...
<?php echo $this->placeholder('js-templates'); ?>
</body>
</html>
然后,您可以在任何需要的地方使用占位符视图辅助对象来添加内容。
在控制器动作中
如以下评论中所述:
在我以前的版本中,我确实忘记了停止php语法并重新启动它,评论员是绝对正确的,没有它就无法工作…
以下是运行版本:http://ideone.com/hGbtU8
是的,在视图中插入脚本不是一种好的做法,但它是有效的。
最好的解决方案是创建一个类来管理插入/更新/删除这样的模板。您可以创建一个抽象类,添加模板(或者模板文件的路径),然后输出它们或将它们添加到视图中。。。
...
public function indexAction() {
...
$this->view->placeholder('js-templates')->captureStart(); ?>
<script type="text/x-jquery-tmpl" id="myId">
<div class="book">
{{:title}}
</div>
</script>
<?php $this->view->placeholder('js-templates')->captureEnd();
...
}
...
或者在视图脚本中
...
<?php $this->placeholder('js-templates')->captureStart(); ?>
<script type="text/x-jquery-tmpl" id="myId">
<div class="book">
{{:title}}
</div>
</script>
<?php $this->placeholder('js-templates')->captureEnd(); ?>
...
当然,您也可以编写自己的视图/布局辅助对象,并扩展默认的占位符辅助对象。
希望它能帮助。。。如果不起作用,请告诉我。。。