我正在尝试使用 kostache,“用于 kohana 框架的小胡子”。
有什么方法可以在 mustache 模板文件中使用简单的 PHP 函数。
我知道逻辑,因此方法违反无逻辑设计原则,但我说的是非常简单的功能。
例如:
gettext('some text')
或者__('some text')
- 获取基本网址;在 kohana ->
Url::site('controller/action')
我正在尝试使用 kostache,“用于 kohana 框架的小胡子”。
有什么方法可以在 mustache 模板文件中使用简单的 PHP 函数。
我知道逻辑,因此方法违反无逻辑设计原则,但我说的是非常简单的功能。
例如:
gettext('some text')
或者__('some text')
Url::site('controller/action')
您可以使用“ICanHaz” http://icanhazjs.com/
然后你可以将你的胡子模板声明为
<script id="welcome" type="text/html">
<p>Welcome, {{<?php echo __('some text') ?>}}! </p>
</script>
好吧,您现在可以使用 Bobthecow 的 Mustache Engine 实现来做到这一点。我们在这里需要匿名函数,它们与其他数据一起传递给模板对象。
看看下面的例子:
<?php
$mustache = new Mustache_Engine;
# setting data for our template
$template_data = [
'fullname' => 'HULK',
'bold_it' => function($text){
return "<b>{$text}</b>";
}
];
# preparing and outputting
echo $mustache->render("{{#bold_it}}{{fullname}}{{/bold_it}} !", $template_data);
在上面的示例中,“ bold_it ”指向我们的函数,该函数与其他数据一起指向我们的模板。' fullname ' 的值作为参数传递给此函数。
请注意,在 Mustache 中传递参数不是强制性的。你甚至可以在没有任何参数的情况下调用 php 函数,如下所示:
<?php
# setting data for our template
$template_data = [
'my_name' => function(){
return 'Joe';
}
];
# preparing and outputting
echo $mustache->render("{{my_name}} is a great guy!", $template_data); # outputs: Joe is a great guy!
学分:http ://dwellupper.io/post/24/calling-php-functions-for-data-in-mustache-php