【发布时间】:2016-05-06 08:22:19
【问题描述】:
我的树枝扩展代码如下
/**
* @return array
*/
public function getFunctions()
{
return [
new \Twig_SimpleFunction('bsPanelTitle', array($this, 'bsPanelTitle')),
];
}
/**
* @param $headline
* @return string
*/
public function bsPanelTitle($headline)
{
$loader = new \Twig_Loader_Filesystem(__DIR__ . '/../Resources/views/Common/Placeholder');
$twig = new \Twig_Environment($loader);
return $twig->render('xtitle.html.twig', ['headline' => $headline]);
}
我的问题是:
有没有更好的方法从 Twig 扩展功能访问 AppBundle/Resources/views/Common/Placeholder 文件夹?
【问题讨论】:
-
你应该只是
include主模板中的占位符 -
bsPanelTitle 将在整个应用程序中使用。它呈现 css 引导标记和标题。我不想在每个树枝文件中复制该标记。所以我想把它当作一个函数来使用。
-
我发现的另一件事是,您可以将所有帮助视图模板放在一个文件夹中,而不是文件夹和子文件夹(我正在这样做)更改您的函数定义如下 /** * @return array */公共函数 getFunctions() { 返回 [ 新 \Twig_SimpleFunction('bsPanelTitle', [$this, 'bsPanelTitle'], ['needs_environment' => true]), ]; } 公共函数 bsPanelTitle(\Twig_Environment $environment, $headline) { return $environment->render('AppBundle:Common:xtitle.html.twig', ['headline' => $headline]); } 它看起来更干净 :-)