我在网站上使用.twig模板但是我需要对一些视图中的逻辑非常狡猾。我不认为twig可以处理这个问题,而且它可以用PHP做一块蛋糕。 (我不打算解释那是什么的细节,只是在PHP中知道它的简单,我实际上已经有了代码)。问题是,我可以在.twig类型的基本模板或父模板中包含.PHP模板吗?
下面的示例(不起作用) - blah.html.php是一个扩展base.html.twig模板的模板......
/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() ?>
在浏览器上,上面的代码将基本模板中的所有.twig代码渲染为浏览器中的文本(I.E.它不会被模板引擎执行)。
PS - 是的,我在app / config / config.yml中设置了.twig和.php
templating: { engines: ['twig', 'php'] }
好的,以下是“复杂”内容的详细信息...... 这是从在Symfony 1.4中编码的站点版本中获取的 - 它被升级到Symfony 2并且我们想要使用所有.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 ?>
答案 0 :(得分:4)
复杂的逻辑应该放在Symfony2的服务层中。对于所有不熟悉PHP的开发人员(例如前端开发人员),模板应该看起来干净整洁。
但是,我在该模板中看不到任何无法在Twig中完成的内容。这是用Twig编写的棘手部分来证明它:
{% 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 %}