PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 12, 2022

[FIXED] How to check if a block exists in a twig template - Symfony2

 May 12, 2022     symfony, twig     No comments   

Issue

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.


Solution

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 %}


Answered By - insertusernamehere
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing