如何检查树枝模板中是否存在块 - Symfony2 [英] How to check if a block exists in a twig template - Symfony2

查看:26
本文介绍了如何检查树枝模板中是否存在块 - Symfony2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下我的树枝模板中有这样的东西

{% block posLeft %}-----{%endblock%}

有没有办法在不调用的情况下检查 posLeft 块是否存在:

block("posLeft")

并检查 posBlock 的返回值以验证是否存在.我是 Symfony2 + Twig 的新手.

解决方案

你可以这样解决,如果你想显示某个块,只有它有内容.希望,这就是您要找的.

示例 index.html.twig

{% set _block = block('dynamic') %}{% 如果 _block 不为​​空 %}{{ _block|raw }}{% 万一 %}

示例 part.html.twig

{% 扩展 "index.html.twig" %}{% 块动态 %}块内容放在这里.{% 结束块 %}

Imagine I have something like this in my twig template

{% block posLeft %}
   -----
{%endblock%}

Is there any way to check for existance of the posLeft block without calling to:

block("posLeft") 

And check the return value of the posBlock to varify the existance. I am a newbie in Symfony2 + Twig.

解决方案

You can solve it like this, if you want to display a certain block only if it has content. Hope, this is what you're looking for.

Example index.html.twig

{% set _block = block('dynamic') %}
{% if _block is not empty %}
    {{ _block|raw }}
{% endif %}

Example part.html.twig

{% extends "index.html.twig" %}

{% block dynamic %}
    Block content goes here.
{% endblock %}

这篇关于如何检查树枝模板中是否存在块 - Symfony2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆