Home » PHP » Twig » Top Twig Template Interview And Answer Part 2

Top Twig Template Interview And Answer Part 2

1. How can we get parameter value from property and match with local array value in twig template?

{% set color = document.getProperty('headerColor') %}
    {% if color %}
        {% set colorMapping = {
            blue: ['#258dc1', '#2aabeb'],
            green: ['#278415', '#1a9f00']
        } %}

        {% if colorMapping[color] is defined %}
            {% set c = colorMapping[color] %}
             <style>
                .cls {
                    background: {{ c[1] }};                  
                }
            </style>      
     {% endif %}
 {% endif %}

2. How can we transform the text in multilingual in twig template?

{{ "Hello World"|trans }}

3. Write a macro for image in twig template.

{% macro image_link(image, editmode = false, linkThumbnail = 'galleryLightbox') %}
{# @var image \Pimcore\Model\Document\Tag\Image #}
{% set hasLink = not editmode and linkThumbnail is not empty %}
{% if hasLink %}
<a href="{{ image.getThumbnail(linkThumbnail) }}" class="thumbnail">
{% endif %}
{{ image|raw }}
{% if hasLink %}
</a>
{% endif %}
{% endmacro %}

4. How we can use select in twig template?

{% set data1 = pimcore_select('data', {
width: 70,
store: [[1,1],[2,2],[3,3]]
}) %}
{% set count = data1.getData() %}
{% if count is defined and count > 0 %}
{# some data here #}
{% endif %}

5. How the pimcore action work in twig template?

{{ pimcore_action('sidebarBox', 'Data', null, {items: count}) }}   {#{ pimcore_action(action, controller, bundle, {}) }#}

Check Also

Top Twig Template Interview And Answer Part 3

1. How the ternary operator work in twig template? {{ admin ? 'Admin' : 'User' …

Leave a Reply

Your email address will not be published. Required fields are marked *