2
枝條函數返回枝條模板可能嗎?枝條函數返回一個模板
例如
class ToTimeExtension extends \Twig_Extension {
public function getFunctions() {
return array(
'totime' => new \Twig_Function_Method($this, 'toTime')
);
}
public function toTime ($string) {
$time = strtotime($string);
return {"time.html.twig", $time};
//return a twig template and pass $time variable
}
}
time.html.twig
<h1>{{time}}</h1>
使用
{{ totime('now') }}
你真的需要一個函數,爲什麼不只是'{%include'time.html.twig'%}'? –
@RuslanOsmanov這只是一個例子。在後端運行更復雜的邏輯。 –