Include the .twig template in the .php template.

I use .twig templates on a website, however I need to be very tricky with a few look logic. I don’t think that a branch can handle this and its a piece of cake to do in PHP. (I will not explain the details of what it is, I just know that it is just in PHP, and I already have the code). The question is, can I include the .PHP template in the base or parent templates of the .twig type?

The example below (which does not work) - blah.html.php is a template that extends the base.html.twig template ...

/some/web/directory/bundlename/Resources/views/blah/blah.html.php

<?php $view->extend('::base.html.twig') ?> <?php $view['slots']->set('title', 'Welcome to nowhere!') ?> <?php $view['slots']->start('body') ?> <div id="body"> html stuff here, blah blah blah... <?php .... ?> complex php and html stuff, and so on... </php end....?> you get the idea... </div> <?php $view['slots']->stop() ?> 

In the browser, the code above displays all the .twig code in the base template as text in the browser (IE it is not executed by the executable template engine).

PS - yes, I have both .twig and .php installed in app / config / config.yml

 templating: { engines: ['twig', 'php'] } 

Well, here are the details of the “complex” stuff ... This is taken from the version of the site that was encoded in Symfony 1.4 - its upgrade to Symfony 2 and we want to use all .twig

 <?php foreach ($all_items as $item): ?> <tr> <td valign="top"> //<?php include_partial('global/item', array('item' => $item)) ?> <<< some code snipped out here >>> <td valign="top"> <table border="1" width="100%"> <?php echo $form->renderFormTag('memberitems/additem') ?> <?php echo $form['item_id']->render(array('value' => $item->getIid())) ?> <?php echo $form['user_id']->render(array('value' => 1)) ?> <?php echo $form['_csrf_token'] ?> <tr> <td width="50" colspan="2" align="center">Quantity<br><?php echo $form['quantity']->render() ?></td> <td width="50" colspan="2" align="center">Amount<br><?php echo $form['value']->render() ?></td> <td width="50" colspan="2" align="center"><input type="submit" value="Add"></td> </tr> </form> <<< here is a complex portion, we're declaring a variable with a value >>> <<< cant do this with twig - from what I know... >>> <?php // determine if item is in registry - if so display "Currently Asking For:" ?> <?php $is_in_registry = false; ?> <?php foreach($member_items as $m_item): ?> <?php if($m_item->getIid() == $item->getIid()): ?> <?php if($is_in_registry == false): ?> <?php $is_in_registry = true; ?> <tr> <td width="50" colspan="6" align="center"><br>Currently Asking For:</td> </tr> <?php endif ?> <?php echo $form->renderFormTag('memberitems/removeitem') ?> <?php echo $form['item_id']->render(array('value' => $item->getIid())) ?> <?php echo $form['user_id']->render(array('value' => 1)) ?> <?php echo $form['id']->render(array('value' => $m_item->getMIid())) ?> <?php echo $form['_csrf_token']->render() ?> <tr> <td width="50" colspan="4" align="center"> <?php echo $m_item->getMIqty()." @ ".$m_item->getMIval() ?> </td> <td width="50" colspan="2" align="center"><input type="submit" value="Remove"></td> </tr> </form> <?php endif ?> <?php endforeach ?> 
+4
source share
1 answer

Sophisticated logic really needs to be placed at the service level in Symfony2. Templates should look clean and tidy for all developers new to PHP (such as third-party developers).

However, I do not see anything in this template that could not be done in Twig. Here's the tricky part written in Twig to prove this:

 {% set is_in_registry = false %} {% for m_item in member_items %} {% if m_item.getIid == item.getIid %} {% if is_in_registry is false %} {% set is_in_registry = true %} <tr> {# you should use css for styling the td #} <td width="50" colspan="6" align="center"><br />Currently asking for:</td> </tr> {% endif %} {{ form.renderFormTag('memberitems/removeitem') }} {{ form.item_id.render({ value: item.getIid }) }} {{ form.user_id.render({ value: 1 }) }} {{ form.id.render({ value: m_item.getMIid }) }} {{ form._csrf_token.render }} <tr> <td width="50" colspan="4" align="center"> {{ m_item.getMIqty ~ ' @ ' ~ m_item.getMIval }} </td> <td width="50" colspan="2" align="center"><input type="submit" value="Remove"></td> </tr> {# didn't notice your form opening tag. I'm assuming you're dynamically generating it #} </form> {% endif %} {% endfor %} 
+4
source

Source: https://habr.com/ru/post/1390898/


All Articles