防止 TWIG 删除变量前的空格 [英] Prevent TWIG from removing a space before a variable

查看:39
本文介绍了防止 TWIG 删除变量前的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个用例.

在整个页面中使用的跨度的默认文本.使用 jQuery 将文本从占位符"更改为特定情况"

Default text, in a span, to be used throughout page. Use jQuery to change text from "placeholder" to "specific case"

即,

在 html 中我想这样做:(twig/html & js)

Inside html i want to do this: (twig / html & js)

{% block content %}
{% set location = '<span id="location">Somewhere</span>' %}

<p>Hey, hows the weather in {{ location | raw }}?</p>


<script>
    var location = detectLocation();
    update $(#location).html('location');
<script>

所需的输出预脚本运行:

Desired output pre script running:

嘿,某处的天气怎么样?

Hey, hows the weather in Somewhere?

所需的输出后脚本运行(假设它输出澳大利亚):

Desired output post script running (assumes it outputs Austrailia):

嘿,澳大利亚的天气怎么样?

Hey, hows the weather in Australia?

...但是我得到的感谢是树枝剥离空间:

..But what I get thanks to twig stripping spaces:

嘿,某个地方的天气怎么样?

Hey, hows the weather inSomewhere?

推荐答案

在仍然使用 twig 1.* 的遗留代码库上从 php7.0 迁移到 php7.4.3 后,我遇到了这种情况.

This happened to me after moving from php7.0 to php7.4.3 on a legacy codebase that was still using twig 1.*.

如果您无法将 twig 更新到较新版本,这里是修复程序.

If you can't update twig to a newer version, here's the fix.

编辑twig/twig/lib/Twig/Lexer.php,第163行修改:

edit twig/twig/lib/Twig/Lexer.php, on line 163 change:

if (isset($this->positions[2][$this->position][0]) ) {
    $text = rtrim($text);
}

if (isset($this->positions[2][$this->position][0]) && ($this->options['whitespace_trim'] === $this->positions[2][$this->position][0])) {
   $text = rtrim($text);
}

更好的修复:更新你的树枝版本.

Even better fix: update your version of twig.

这篇关于防止 TWIG 删除变量前的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆