0

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?

Andreas
  • 1,691
  • 1
  • 15
  • 34
  • 1
    are you talking about the preview? in the peview we render only the content block to enhance the performance there. This mechanism was also implemented in 1.6 but refactored in 2.0 – Johannes Wachter Apr 22 '20 at 05:23
  • Hi @JohannesWachter - yes the issue shows up in the preview, on the frontend rendering it is ok indeed! Thx for this insight, I update the question accordingly. – Andreas Apr 22 '20 at 08:53
  • After checking my code I can probably move the definitions in question inside the `block content` - but it would be still interesting, if there is a way to maintain preview variable access outside the content block. – Andreas Apr 22 '20 at 09:07
  • currently not - the problem there is to update only a part of the preview (the content part) we only render exactly this part. Twig does not supporting evaluating the variables outside of this block. – Johannes Wachter Apr 22 '20 at 11:05

1 Answers1

0

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.

Andreas
  • 1,691
  • 1
  • 15
  • 34