Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony细枝扩展渲染视图_Symfony_Twig_Render - Fatal编程技术网

Symfony细枝扩展渲染视图

Symfony细枝扩展渲染视图,symfony,twig,render,Symfony,Twig,Render,我的小枝分机代码如下 /** * @return array */ public function getFunctions() { return [ new \Twig_SimpleFunction('bsPanelTitle', array($this, 'bsPanelTitle')), ]; } /** * @param $headline * @return string */ public function bsPanelTitle($h

我的小枝分机代码如下

/**
 * @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]);
}
我的问题是:


是否有更好的方法从细枝扩展功能访问AppBundle/Resources/views/Common/Placeholder文件夹?

如注释中所述,使用include()将模板包含在需要的任何位置

例如,以下内容与您尝试实现的内容相同:

app/Resources/views/xtitle.html.twig:


这将包括模板、引导样式以及该模板中的任何其他内容。

如注释中所述,使用include()将模板包括在您需要的任何位置

例如,以下内容与您尝试实现的内容相同:

app/Resources/views/xtitle.html.twig:


这将包括模板、引导样式以及该模板中的任何其他内容。

由于此问题用symfony2标记,我假设您确实在谈论symfony2项目,如果是这种情况,您可以使用twig配置向twig注册您自己的名称空间,如下所示:

twig:
    # ...
    paths:
        "%kernel.root_dir%/../pathFromProjectRootToPlaceholder/Placeholder": placeholder
//from controller
return $this->render(
    '@placeholder/index.html.twig',
    $data
);
这将为文件夹路径FromProjectRootTopPlaceHolder/Placeholder创建一个别名,然后您可以使用它来呈现模板,如下所示:

twig:
    # ...
    paths:
        "%kernel.root_dir%/../pathFromProjectRootToPlaceholder/Placeholder": placeholder
//from controller
return $this->render(
    '@placeholder/index.html.twig',
    $data
);

您可以向同一别名添加多个路径,但有关更多信息,请查看symfony 2网站上的官方食谱:

希望这有帮助


Alexandru Cosoi

由于这个问题带有symfony2标记,我假设您确实在谈论symfony2项目,如果是这种情况,您可以使用twig配置向twig注册自己的名称空间,如下所示:

twig:
    # ...
    paths:
        "%kernel.root_dir%/../pathFromProjectRootToPlaceholder/Placeholder": placeholder
//from controller
return $this->render(
    '@placeholder/index.html.twig',
    $data
);
这将为文件夹路径FromProjectRootTopPlaceHolder/Placeholder创建一个别名,然后您可以使用它来呈现模板,如下所示:

twig:
    # ...
    paths:
        "%kernel.root_dir%/../pathFromProjectRootToPlaceholder/Placeholder": placeholder
//from controller
return $this->render(
    '@placeholder/index.html.twig',
    $data
);

您可以向同一别名添加多个路径,但有关更多信息,请查看symfony 2网站上的官方食谱:

希望这有帮助


Alexandru Cosoi

您只需在主模板中包含占位符即可。BSPAnteltile将在整个应用程序中使用。它呈现css引导标记和标题。我不想在每个细枝文件中复制该标记。所以我想将它用作函数。我发现的另一件事是,您可以将所有帮助器视图模板放在一个文件夹中,而不是文件夹和子文件夹(我正在这样做)更改您的函数定义,如下所示/***@return array*/public function getFunctions(){return[new\Twig\u SimpleFunction('bspanelitle',[$this,'bsPanelTitle'],['needs_environment'=>true]),]}公共函数bsPanelTitle(\Twig_environment$environment,$headline){return$environment->render('AppBundle:Common:xtitle.html.Twig',['headline'=>$headline])}它看起来更干净:-)你应该在主模板中包含占位符。bsPanelTitle将在整个应用程序中使用。它呈现css引导标记和标题。我不想在每个小树枝文件中复制该标记。因此我想将其用作一个函数。我还发现,你可以将所有e helper查看文件夹中的模板,而不是文件夹和子文件夹(我正在这样做)更改函数定义如下/***@return array*/public function getFunctions(){return[new\Twig\u SimpleFunction('bspanelitle',[$this,'bspanelitle'],['needs\u environment'=>true]),];}公共函数bsPanelTitle(\Twig_Environment$Environment,$headline){return$Environment->render('AppBundle:Common:xtitle.html.Twig',['headline'=>$headline]);}它看起来更干净:-)您的解决方案也可以工作,但是使用php代码,我可以在它上面写测试,类似于view helper测试您的解决方案也可以工作,但是使用php代码,我可以在它上面写测试,类似于view helper测试