0
votes

I'm new to Twig and I'm trying to figure out how to achieve the following.

I have an object with a set of properties that I want to render with different Twig templates depending on which type of property it is (text, images, date, etc).

I want to render them as follows:

<div class="row">
      <div class="title">Title of property</div>
      <div class="propertycontent">
           //Specific property content depending on property type
      </div>
</div>

My problem is that I can't figure out to skip the complete output if the property is not defined. I want to be able to use parent templates to take care of the "wrapping" of the rendered property content. Is it possible to use parent templates that returns nothing if the property value is undefined? Is there another good solution that does not rely on include "begin"/"end" for wrapping each template?

Thanks in advance.

1
I'm wondering if is possible to extend a TWIG template depending on a var. For example: {% if my_var = '...' %}{% extends "My:Own:layout.html.twig" %}{% else %}{% extends "Any:Other:layout.html.twig" %} Could it be an option? (I don't publish this as an answer because I'm not sure if it works)Dani Sancas
twig can do it like this: {% extends my_var ? "foo.html" : "bar.html" %} @DaniSancasDasBaconfist

1 Answers

2
votes

Solution: Call parent with value(might be null) and title

Parent (propery_wrapper.twig):

{% if value %}
    <div class="row">
    <div class="title">{{title}}</div>
    <div class="propertyContent">
            {% block content %}{% endblock %}
        </div>
    </div>
{% endif %}

child (for height property):

{% extends 'property_wrapper.twig' %}
{% block content %}
    {{ value.value|number_format(2, ',') }} m
{% endblock %}