ymfony中twig页面渲染自定义组件的方式介绍
Controller:自定义模板需要用的Acion:
* * * * *
/**
* 网站左侧银行列表(导航)
*
*/
public function BankListAction()
{
$em = $this->getDoctrine()->getManager();
$bankEntities = $em->getRepository('AcmeDemoBundle:Bank')
->findBy(array(),array('sort'=>'desc'));
return $this->render('AcmeDemoBundle:Bank:banklist.html.twig', array(
'bankEntities' => $bankEntities,
));
}
* * * * *
twig:要自定义的模板:
{#% extends "AcmeDemoBundle::layout.html.twig" %#}
<div class="list fl">
<div class="box">
<div class="title"><h1>银行列表</h1></div>
<ul>
{% if bankEntities is defined and bankEntities is not empty %}
{% for bank in bankEntities %}
<li><a href="{{ path('bank_show',{'id':bank.id}) }}"><img src="{{ bank.logo }}"/></a></li>
{% endfor %}
{% else %}
<li><a href="javascript:void(0)">暂无银行信息。</a></li>
{% endif %}
</ul>
<a href="{{ path('bank') }}" class="more">查看更多 >></a>
</div>
</div>
* * * * *
routing:路由声明:
# 网站左侧导航菜单——银行列表
navigation_bankList:
pattern: /navBankList
defaults: { _controller: "AcmeDemoBundle:Bank:BankList" }
view:twig页面调用方式:
* * * * *
<!-- 银行列表 -->
{{ render(controller('AcmeDemoBundle:Bank:BankList')) }}
{# { include('AcmeDemoBundle:Bank:banklist.html.twig') } #}