英文:
spaceless block inside autoescape throws SyntaxException in Twig 3.*
问题
刚刚从Twig 2.*
升级到Twig 3.*
我的大部分模板都包含在{% autoescape %} ... {% endautoescape %}
块中
而一些模板内部有{% spaceless %} ... {% endspaceless %}
块
最终看起来像这样:
{% autoescape %}
...
{% spaceless %}
...
{% endspaceless %}
...
{% endautoescape %}
现在(升级到3.*
后),我从Twig得到了SyntaxException
。
这些块应该如何重新编写/支持?
为什么Twig不再支持这样的结构?
> 致命错误:未捕获的Twig\Error\SyntaxError:意外的“spaceless”标签(期望在第2行附近定义的“autoescape”标签的关闭标签)。
英文:
Just updated from Twig 2.*
to Twig 3.*
Most of my templates are wrapped in the {% autoescape %} ... {% endautoescape %}
block
And some templates have {% spaceless %} ... {% endspaceless %}
block inside.
In final it looks like this:
{% autoescape %}
...
{% spaceless %}
...
{% endspaceless %}
...
{% endautoescape %}
And now (after the update to 3.*
) I get SyntaxException
from Twig.
How should these block be rewritten/supported?
Why such construction is no longer supported by the Twig?
> Fatal error: Uncaught Twig\Error\SyntaxError: Unexpected "spaceless" tag (expecting closing tag for the "autoescape" tag defined near line 2). in
答案1
得分: 20
你现在需要使用 apply
标签,例如:
{% apply spaceless %}
...
{% endapply %}
这个构造的原因是为了保持代码库的一致性,因为现在你可以将过滤器应用于文本块,例如:
{% apply upper %}
This {{ text }} becomes uppercase
{% endapply %}
而以前你需要将它连接起来并添加括号:
{{ ('This '~text~' becomes uppercase')|upper }}
TLDR 为了保持一致性,他们决定移除 tag spaceless 并引入 filter spaceless。
英文:
You need to use the apply
tag now, e.g.
{% apply spaceless %}
...
{% endapply %}
The reason is for this construction is to have a consistent codebase as you now can "apply" filter(s) to chunk of texts, e.g.
{% apply upper %}
This {{ text }} becomes uppercase
{% endapply %}
whereas you needed to concat it in the past and add parentheses
{{ ('This '~text~' becomes uppercase')|upper }}
TLDR To be consistent they've decided to remove the tag spaceless and to introduce the filter spaceless
答案2
得分: 0
在这里有一个类似的错误,不过在修复了其中一个文件(添加apply
和endapply
)之后,错误消息仍然存在。
我喜欢Mediteran主题作为我的管理员主题,在Drupal 9中使用正常。然而,在升级到Drupal 10后,我遇到了这个错误。
错误:
php 错误 Twig\Error\SyntaxError: 意外的 "spaceless" 标记(期望在第27行附近定义的 "for" 标记的闭合标记)。位于Twig\Parser->subparse()(路径/to/themes的第30行)
php 错误 Twig\Error\SyntaxError: 意外的 "spaceless" 标记(期望在第27行附近定义的 "for" 标记的闭合标记)。位于Twig\Parser->subparse()(路径/to/themes的第30行)
php 错误 Twig\Error\SyntaxError: 意外的 "spaceless" 标记(期望在第27行附近定义的 "for" 标记的闭合标记)。位于Twig\Parser->subparse()(路径/to/themes的第30行)
php 错误 Twig\Error\SyntaxError: 意外的 "spaceless" 标记(期望在Twig\Parser->subparse()(路径/to/themes的第30行)附近定义的 "for" 标记的闭合标记)。
解决方案:
在模板目录中搜索 'spaceless'。
/themes/contrib/mediteran/templates
要在目录中的所有文件中搜索特定文本并替换为其他内容,您可以使用find
命令与grep
和sed
结合使用。您可以添加-name
选项到find
命令中。
我遵循的步骤:
grep -rn --include="*.twig" "spaceless" path/to/mediteran/templates
输出:
path/to/mediteran/templates/form/dropbutton-wrapper.html.twig:13: {% spaceless %}
path/to/mediteran/templates/form/dropbutton-wrapper.html.twig:19: {% endspaceless %}
path/to/mediteran/templates/form/select.html.twig:13:{% apply spaceless %}
path/to/mediteran/templates/admin/ckeditor-settings-toolbar.html.twig:14:{% spaceless %}
path/to/mediteran/templates/admin/ckeditor-settings-toolbar.html.twig:73:{% endspaceless %}
path/to/mediteran/templates/field/link-formatter-link-separate.html.twig:15:{% spaceless %}
path/to/mediteran/templates/field/link-formatter-link-separate.html.twig:22:{% endspaceless %}
path/to/mediteran/templates/navigation/toolbar.html.twig:30: {% spaceless %}
path/to/mediteran/templates/navigation/toolbar.html.twig:41: {% endspaceless %}
所以现在我们知道问题在哪里,我们要查找 (spaceless
) 并替换为 (apply spaceless
)。
find path/to/mediteran/templates -type f -name "*.twig" -exec grep -l "spaceless" {} \; -exec sed -i 's/spaceless/apply spaceless/g' {} \;
请注意,上述命令可能会将 endspaceless
更改为 endapply spaceless
。因此,我们需要运行另一个命令将 endapply spaceless
更改为 endapply
。
find path/to/mediteran/templates -type f -name "*.twig" -exec grep -l "endapply spaceless" {} \; -exec sed -i 's/endapply spaceless/endapply/g' {} \;
如果其他已弃用的标记错误消息出现在多个实例中,可以使用相同的步骤来修复它们。
英文:
Similar Error here, however after fixing one of the file (by adding apply and endapply) error messages persists.
I love the Mediteran theme for my Admin, which worked fine with Drupal 9. However, after upgrading to Drupal 10, I got this error.
Error:
---- -------------- drush ws-------- ---------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Type Severity Message
---- -------------- -------- ---------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
php Error Twig\Error\SyntaxError: Unexpected "spaceless" tag (expecting closing tag for the "for" tag defined near line 27). in Twig\Parser->subparse() (line 30 of path/to/themes
php Error Twig\Error\SyntaxError: Unexpected "spaceless" tag (expecting closing tag for the "for" tag defined near line 27). in Twig\Parser->subparse() (line 30 of path/to/themes
php Error Twig\Error\SyntaxError: Unexpected "spaceless" tag (expecting closing tag for the "for" tag defined near line 27). in Twig\Parser->subparse() (line 30 of path/to/themes
php Error Twig\Error\SyntaxError: Unexpected "spaceless" tag (expecting closing tag for the "for" tag defined near line 27). in Twig\Parser->subparse() (line 30 of path/to/themes
Solution:
Is to search for ‘spaceless
’ in the template directory. /themes/contrib/mediteran/templates
To search for a specific text in all files within a directory and replace it with something else, you can use the find
command in combination with grep
and sed
. And you can add the -name
option to the find command.
Steps I followed:
grep -rn --include="*.twig" "spaceless" path/to/mediteran/templates
Output:
path/to/mediteran/templates/form/dropbutton-wrapper.html.twig:13: {% spaceless %}
path/to/mediteran/templates/form/dropbutton-wrapper.html.twig:19: {% endspaceless %}
path/to/mediteran/templates/form/select.html.twig:13:{% apply spaceless %}
path/to/mediteran/templates/admin/ckeditor-settings-toolbar.html.twig:14:{% spaceless %}
path/to/mediteran/templates/admin/ckeditor-settings-toolbar.html.twig:73:{% endspaceless %}
path/to/mediteran/templates/field/link-formatter-link-separate.html.twig:15:{% spaceless %}
path/to/mediteran/templates/field/link-formatter-link-separate.html.twig:22:{% endspaceless %}
path/to/mediteran/templates/navigation/toolbar.html.twig:30: {% spaceless %}
path/to/mediteran/templates/navigation/toolbar.html.twig:41: {% endspaceless %}
So now we know where the issue is, so we find (spaceless
) and replace all with (apply spaceless
).
find path/to/mediteran/templates -type f -name "*.twig" -exec grep -l "spaceless" {} \; -exec sed -i 's/spaceless/apply spaceless/g' {} \;
Note the above command could change endspaceless
to endapply spaceless
So we need to run another command to change endapply spaceless
to endapply
.
find path/to/mediteran/templates -type f -name "*.twig" -exec grep -l "endapply spaceless" {} \; -exec sed -i 's/endapply spaceless/endapply/g' {} \;
These same steps can be employed to fix other deprecated tag error messages, should they appear in more than one instance.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论