I have this use case.
Default text, in a span, to be used throughout page. Use jQuery to change text from "placeholder" to "specific case"
Ie,
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?
This happened to me after moving from php7.0 to php7.4.3 on a legacy codebase that was still using twig 1.*.
If you can't update twig to a newer version, here's the fix.
edit twig/twig/lib/Twig/Lexer.php
, on line 163 change:
if (isset($this->positions[2][$this->position][0]) ) {
$text = rtrim($text);
}
to
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With