Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.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
Twig 使用细枝加载器字符串扩展细枝中的模板_Twig - Fatal编程技术网

Twig 使用细枝加载器字符串扩展细枝中的模板

Twig 使用细枝加载器字符串扩展细枝中的模板,twig,Twig,我必须使用Twig_Loader_字符串呈现模板,我需要扩展模板,如下所示: $body='{% extends "/views/path/to/my/template" %} {% block body %} Hello {% endblock %}'; 在PHPs方面,我写道: $loader = new Twig_Loader_String(); $twig = new Twig_Environment($loader); $twig->render($body,ar

我必须使用Twig_Loader_字符串呈现模板,我需要扩展模板,如下所示:

$body='{% extends "/views/path/to/my/template" %}
       {% block body %} Hello {% endblock %}';
在PHPs方面,我写道:

$loader = new Twig_Loader_String();
$twig = new Twig_Environment($loader);
$twig->render($body,array());
我不明白为什么渲染后的结果只是:

/views/path/to/my/template

您只有一个字符串加载器,这个字符串就是您的实际模板。您正在扩展它,并覆盖主体块-但它没有主体块。

通过@Maerlyn扩展答案,并提供一些代码如何操作:

$loaders = [
    new Twig_Loader_Filesystem('path/to/twig/views'),
    new Twig_Loader_String()
];

$loader = new Twig_Loader_Chain($loaders);

$twig = new Twig_Environment($loader);

我正在尝试扩展/views/path/to/my/template,因为其中有一个名为body的块,我想用Hello替换内容。但是它不起作用,为什么?那么您需要一个包含字符串和文件系统加载程序的环境。对不起,我不能帮你了解细节。