Twig - 自动插入模板名称作为 html 注释 [英] Twig - Automatically insert templatename as html comment
本文介绍了Twig - 自动插入模板名称作为 html 注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个 php 项目并使用 twig 作为模板引擎,想知道是否有可能将使用的模板文件名/路径作为 html 注释自动添加到 twig 模板中?
I am working on a php project and using twig as template engine and wondering if there is a possibility to automatically add the used template filename/path into a twig template as html-comment?
这对调试非常有用,因为我可以查看 html 代码并找出如果我想更改某些内容,我必须查看哪个 twig 文件.
That would be great for debugging, since I could have a look at the html code and find out, in which twig file I have to look, if I want to change something.
例如,结果应该是这样的:
For example, the result should look like this:
<!-- start tpl/layout.twig -->
<html>
<head></head>
<body>
<!-- start tpl/header.twig -->
<header>My header</header>
<!-- end tpl/layout.twig -->
<!-- start tpl/content.twig -->
<section>
<!-- start tpl/article.twig -->
<article>blabla1</article>
<!-- end tpl/article.twig -->
<!-- start tpl/article.twig -->
<article>xyz</article>
<!-- end tpl/article.twig -->
</section>
<!-- end tpl/content.twig -->
</body>
</html>
<!-- end tpl/layout.twig -->
推荐答案
如果它不在您的索引视图中,您可以将模板名称加上:
If it's not in your index view , you can have the name of template with :
{{ app.request.attributes.get('_template').get('name') }}
获取完整路径:
{{ app.request.attributes.get('_template')}}
这篇关于Twig - 自动插入模板名称作为 html 注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文