0

我在内容块之外的模板中定义了一些全局 twig 变量,现在升级到 sulu 2.0 后,这会在预览中引发意外的“变量不存在错误”。实际的页面渲染仍然完好无损。在@JohannesWachter 的评论之后,预览现在只呈现内容块并忽略外部变量。

我有以下(简化的)代码,它曾经在 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 %}

在预览中,我收到以下错误{% if hasContent and headline is defined%}变量“hasContent”不存在。(main.html.twig 第 43 行)

有没有办法在 sulu 2.0 的预览版和主页中提供这种全局变量?

4

1 回答 1

0

我通过将内容块中使用的变量移动到内容块中来修复它:

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

我尝试将变量定义移动到setup.html.twig文件中,但仅在包含的模板内定义的变量不再对外部可见。

于 2020-04-23T12:32:18.573 回答