I use .twig templates on a website however I need to get pretty crafty with the logic on a few of the views. I dont think twig can handle this and its a piece of cake to do in PHP. (I'm not going to explain the details of what that is, just know its easy in PHP and I actually already have the code). The question is, can I include a .PHP template in base or parent templates that are of type .twig?
Example below (which does not work) - blah.html.php is a template which 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() ?>
On the browser the code above renders all the .twig code in the base template as text in the browser (I.E. it does not get executed by the templating engine).
PS - yes I have both .twig and .php set in app/config/config.yml
templating: { engines: ['twig', 'php'] }
Ok, here are the details of the "complex" stuff... This is taken from a version of the site which was coded in Symfony 1.4 - Its being upgraded 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 ?>
Complex logic should really be put in the service layer in Symfony2. The templates should look clean and tidy for all the devs not familiar with PHP (e.g front-end developers).
However, I don't see anything in that template that couldn't be done in Twig. Here's the tricky part written in Twig to prove it:
{% 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 %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With