我在一个网站上使用了.twig模板,但是我需要在几个视图上使用逻辑很狡猾。我不认为树枝可以处理这个问题,并且它是PHP中的一块蛋糕。 (我不会解释这是什么的细节,只需知道它在PHP中的简单性,而且我已经有了代码)。问题是,我可以在.twig类型的基本模板或父模板中包含.PHP模板吗?在.php模板中包含一个.twig基本模板
例以下(不工作) - blah.html.php是它扩展了base.html.twig模板模板...
/一些/ web /目录/软件包名/资源/视图/嗒嗒/ 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(IE它不会由模板引擎执行)。
PS - 是的,我有两个.twig和.PHP在app /配置/ config.yml
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 ?>
什么样的复杂的东西?你可以把它放在一个小枝函数或宏。 – solarc 2012-01-13 19:14:41
卡洛斯 - 我已经更新了一些关于复杂东西的细节问题......这只是我称之为复杂东西的一个例子,复杂的我指的是对.twig复杂,因为我不认为树枝被设计为处理逻辑这个。 (我可能是错的)。 – ElasticThoughts 2012-01-13 19:24:37