嗨,我正在用codeigniter在mustache.php上工作,现在解析胡子标签真的很好,我如何在胡子标签中使用CI助手或php函数,比如
{{ anchor("http://www.google.com","Google") }}
//php function
{{ date() }}我试过胡子助手,但根据这篇文章github mustache,我没有运气。
在这种情况下,我必须添加额外的开始和结束八字胡标签。我不希望它只是在标签中传递函数并获得输出。
发布于 2012-09-26 23:51:49
您不能直接在Mustache模板中调用函数(记得吗?)
{{ link }}
{{ today }}相反,此功能属于您的渲染上下文或ViewModel。至少,这意味着提前准备好数据:
<?php
$data = array(
'link' => anchor('http://www.google.com', 'Google'),
'today' => date(),
);
$mustache->loadTemplate('my-template')->render($data);一种更好的方法是将my-template.mustache所需的所有逻辑封装在一个ViewModel类中,我们称之为MyTemplate
<?php
class MyTemplate {
public function today() {
return date();
}
public function link() {
return anchor('http://www.google.com', 'Google');
}
}
$mustache->loadTemplate('my-template')->render(new MyTemplate);https://stackoverflow.com/questions/12595473
复制相似问题