dougu3290 2017-10-19 04:43
浏览 47
已采纳

twig扩展 - php相当于twig的包含

I have a library of widgets (js and other files) which instead of writing

{% include 'path' %}

all the time I have written an extension to just include them with a function call like so:

{% widget('name') %}

So my question is how do I re implement the twig include function? Is it just a matter of returning a php include or is it a file_get_contents?

Examples:

<?php

    class Extensions 
    {
        public function widget($name) 
        {
             return include 'filepath/' . $name . '.html.twig';
             // or 
             return file_get_content('filepath/' . $name . '.html.twig');
        }
    } 

Thank you

Updated: I'm using file_get_contents and I get the following returned:

alert('FROM HERE');
// AS
alert(&#039;FROM HERE&#039;);
  • 写回答

1条回答 默认 最新

  • dongse5408 2017-10-19 04:51
    关注

    its some more they parse the templates on call

    look:

    https://github.com/twigphp/Twig/blob/2.x/lib/Twig/Extension/Core.php#L1287

    edit: file_get_contents do the job. include makes more than only output the content

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?