php - How to programmatically set twig block content? -


Is it possible to set the value of a dime template block with a PHP code? I am migrating from a different template engine and I need a bridge to set the value of the block without using a twig template.

I just found plain text which I was hoping to allocate before presenting the template.

If you want to include PHP files inside the blocks, then I suggest you create an extension Give it / P>

sample

index.php

  & lt; Php is required (__dir__. '/../vendor/autoload.php'); $ Loader = new twig_loader_file system ('./'); $ Twig = new Twig_Environment ($ loader, array ()); $ Function = new_get_clean ();}, array ('is_safe') = new twig_SimpleFunction ('get_php_contents', function ($ file, $ reference) {ob_start (); included ($ file); // $ reference is available in your php file = & Gt; array ('html')); $ Twig- & gt; AddFunction ($ function); Echo $ twig- & gt; Render ('test.twig', array ('name' = & gt; 'Alain'));  

test.twig

  {% extended 'base.twig'%} {% block content%} {{get_php_contents ('Contents.php', _context)}} {% endblock%}  

base.twig

  & lt; Html & gt; & Lt; Div & gt; I'm a base layout & lt; / Div & gt; {% Block content%} {% endblock%} & lt; / Html & gt;  

content.php

  & lt ;? Php echo & gt; Div style = "color: red" & gt; '; "Hello {$ reference [echo 'name']} echo it, now it is:"; Echo date ("Y-M-DHH: I: S"); Echo & lt; / Div & gt; ';  

Result

  & lt; Html & gt; & Lt; Div & gt; I'm a base layout & lt; / Div & gt; & Lt; Div style = "color: red" & gt; Hello Alain, it is now: 2014-10-28 19: 23: 23 & lt; / Div & gt; & Lt; / Html & gt;  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -