如何在mustache.php中调用codeigniter helper或php函数

时间:2012-09-26 06:05:05

标签: php codeigniter mustache

嗨我正在使用codeigniter处理mustache.php它正在解析小胡子标签真的很好现在我怎么能在小胡子标签中使用CI助手或php函数

    {{ anchor("http://www.google.com","Google") }}

//php function

    {{ date() }}

我已经尝试过胡子助手,但根据这篇文章github mustache

没有运气

在这种情况下,我必须添加额外的开始和结束胡子标签。我不想只是在标签中传递函数并获得输出。

1 个答案:

答案 0 :(得分:2)

你不能直接在你的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);
相关问题