Php 在呈现任何内容之前,是否有一种方法可以在加载细枝模板后显示其原始内容?
有人知道有没有一种方法可以在加载树枝模板后,在呈现任何内容之前显示其原始内容 假设我有一个根模板:Php 在呈现任何内容之前,是否有一种方法可以在加载细枝模板后显示其原始内容?,php,symfony,twig,Php,Symfony,Twig,有人知道有没有一种方法可以在加载树枝模板后,在呈现任何内容之前显示其原始内容 假设我有一个根模板: {# templates/template.txt.twig #} This is my root template {{arg1}} {{ include('other/internal.txt.twig') }} 另一个包含在根目录中: {# templates/other/internal.txt.twig #} This is my included template {{arg2}}
{# templates/template.txt.twig #}
This is my root template
{{arg1}}
{{ include('other/internal.txt.twig') }}
另一个包含在根目录中:
{# templates/other/internal.txt.twig #}
This is my included template
{{arg2}}
如果我使用arg1='foo'和arg2='bar渲染template.txt.twig
,结果将是
This is my root template
foo
This is my included template
bar
在任何变量求值之前,是否有方法检索加载的模板?
我期望得到的是这样的东西:
This is my root template
{{arg1}}
This is my included template
{{arg2}}
其背后的思想是利用所有细枝加载机制(加载路径、名称空间…)
因为我需要对twig代码本身进行一些自定义自动检查(与twig语法无关,但与twig无关的高级模型的一致性)
让我知道这是否有意义,或者您是否需要更多信息
感谢您的帮助我相信
Elao/WebProfilerExtraBundle
可从以下网址获得:
可能会向您提供您在上述示例中寻求的其他详细信息
另见:
为了呈现模板,Twig将Twig内容编译成PHP代码,因此我认为Twig本身不可能做到这一点。你可以看看蒂埃里给你的链接
我认为您唯一的解决方案是阅读包含
文件内容的文件,但您不会像示例中那样将包含的模板放在正确的位置。如果您希望在呈现模板之前使用某种服务处理模板,那么您可以覆盖细枝环境并在loadTemplate
方法
例如,在这里,我将插入一个templateValidator
,然后在每个模板加载上运行它
App\AcmeBundle\Twig\Twig\u环境
首先扩展\Twig\u环境
并覆盖加载模板
方法,以及添加用于注入模板验证器
的setter
在loadTemplate
中,我将$this->getLoader()->>getSource($name)
的实例替换为$this->validateTemplate($name)
,它执行相同的操作,但也在您希望添加的任何操作之前(在本例中为$this->templateValidator->validate($source)
)
app/config/config.yml
重写参数twig.class
,以便DI使用您的类而不是原始的\twig\u环境
parameters:
twig.class: App\AcmeBundle\Twig\Twig_Environment
App\AcmeBundle\DependencyInject\Compiler\TwigEnvironmentInjectCompilerPass
创建一个编译器类,将templateValidator
注入新创建的TwigEnvironment
(仅当它具有'setTemplateValidator'
方法以便正确降级时)
App\AcmeBundle\AppAcmeBundle
将编译器过程添加到bundle构建中
use App\AcmeBundle\DependencyInject\Compiler\TwigEnvironmentInjectCompilerPass;
class AppAcmeBundle extends Bundle
{
/**
* {@inheritdoc}
*/
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new TwigEnvironmentInjectCompilerPass());
}
}
注意这没有经过任何测试,这只是我的想法,所以可能都是错误的。echo(file_get_contents(“/path/to/template/file.tpl”);你想要{{arg1}
作为字符串还是字符串/解析取决于?@GordonM谢谢,但正如前面提到的,我想从包含期间的细枝加载机制中获益,这可以在templates@Qoop我真的希望{{arg1}}保持字符串的形式如果你不希望它被解析,那么你可以像{{{{arg1}那样把它写成字符串{}
或者,如果您偶尔想将其用作变量或字符串,这取决于是否传递了变量,您可以使用{{arg1已定义?arg1:{{arg1}}}
。谢谢!我会看看它,并让您知道它是否适合我的需要。根据我从“深入细枝”中读到的内容演示文稿,Twig的工作方式不允许我做我想做的事情谢谢,所以你是说在编译成PHP代码之前没有解析include,而是同时解析?如果是这样的话,似乎我必须手动解析include。非常感谢,我会看一看,如果我得到一些信息,会告诉你的事情
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Reference;
class TwigEnvironmentInjectCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('twig') ||
!$container->hasDefinition('acme.template_validator'))
{
return;
}
$twig = $container->getDefinition('twig');
if (!$twig->hasMethodCall('setTemplateValidator')) {
return;
}
$twig->addMethodCall(
'setTemplateValidator',
array(new Reference('acme.template_validator'))
);
}
}
use App\AcmeBundle\DependencyInject\Compiler\TwigEnvironmentInjectCompilerPass;
class AppAcmeBundle extends Bundle
{
/**
* {@inheritdoc}
*/
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new TwigEnvironmentInjectCompilerPass());
}
}