Search code examples
sulu

Unexpected Variable does not exist Error in Preview after Sulu CMS upgrade


I define a handfull of global twig variables in templates outside of the content block, now after the upgrade to sulu 2.0, this is throwing unexpected "Variable does not exist Error" in the preview. The actual page rendering is still intact. After the comment of @JohannesWachter it appears, that the preview is only rendering the content block now and ignoring outside variables.

I have the following (simplified) code, which used to work in sulu 1.6: main.html.twig

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

{% set hasContent = content is defined %}

{% if hasContent %}
    {% set headline = content.headline is defined and content.headline ? content.headline : content.title %}
{% endif %}

{% block content %}
        <div class="row">

            {% block row %}
                <section class="col-sm-8 main-content">
                    {% if hasContent and headline is defined%}
                        <h1 class="headline" property="title">{{ headline }}</h1>
                    {% endif %}

In the preview I get the following error for the line {% if hasContent and headline is defined%}: Variable "hasContent" does not exist. (main.html.twig line 43)

Is there a way to have this kind of global variables available in the preview and the main page for sulu 2.0?


Solution

  • I fixed it by moving variables used in the content block into the content block:

    {% extends "base.html.twig" %}
    
    {# set variables nesessary to adjust base.html.twig only #}
    
    {% block content %}
        {% set hasContent = content is defined %}
    
        {% if hasContent %}
            {% set headline = content.headline is defined and content.headline ? content.headline : content.title %}
        {% endif %}
    
            <div class="row">
    
                {% block row %}
                    <section class="col-sm-8 main-content">
                        {% if hasContent and headline is defined%}
                            <h1 class="headline" property="title">{{ headline }}</h1>
                        {% endif %}
    
    

    I tried a bit around with moving the variable definition into a setup.html.twig file, but variables only defined inside an included template are not visible to the outside anymore.