What is the auto-transition used for Swig templates for Node.js?

I am trying to write a route application built on Express. Swig is a template engine. I am confused by the Swigs autoescape feature. What exactly is he doing?

Swig:

"Control auto-escaping of variable output from within your templates."

// myvar = '<foo>';
{% autoescape true %}{{ myvar }}{% endautoescape %}
// => <foo>
{% autoescape false %}{{ myvar }}{% endautoescape %}
// => <foo>

My code is:

<script>

{% autoescape false %}
var all_hotels = {{ hotels | json }};
var all_restaurants = {{ restaurants | json }};
var all_things_to_do = {{ things_to_do | json }};

{% endautoescape %}

</script>

Thanks.

+4
source share
1 answer

The documentation should look like this:

"Control auto-escaping of variable output from within your templates."

// myvar = '<foo>';
{% autoescape true %}{{ myvar }}{% endautoescape %}
// => &lt;foo&gt;
{% autoescape false %}{{ myvar }}{% endautoescape %}
// => <foo>

Therefore, when autoescape true, it will contain the contents of the HTML-escape variable. I think this is the default value for Swig.

JSON-, ( autoescaping, HTML JSON). , safe:

var all_hotels = {{ hotels | safe | json }};
var all_restaurants = {{ restaurants | safe | json }};
...
+9

Source: https://habr.com/ru/post/1527147/


All Articles