3

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?

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 -->
bernhardh
  • 3,137
  • 10
  • 42
  • 77

1 Answers1

0

If it's not in your index view , you can have the name of template with :

{{ app.request.attributes.get('_template').get('name') }}

To get full path :

{{ app.request.attributes.get('_template')}}
BENARD Patrick
  • 30,363
  • 16
  • 99
  • 105
  • 1
    But I would have to add this code to every template. I am looking for an automatical solution, which I can turn on/off for development/production environment. – bernhardh Sep 13 '13 at 09:55
  • 1
    You can create a BaseView with this code, and extended each view with it. Or, you can extend the symfony2 debug bar. – BENARD Patrick Sep 13 '13 at 09:58