Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escaping Braces #135

Closed
SergioBenitez opened this issue Feb 17, 2017 · 9 comments
Closed

Escaping Braces #135

SergioBenitez opened this issue Feb 17, 2017 · 9 comments

Comments

@SergioBenitez
Copy link
Contributor

Can braces be escaped? I know that I can use raw, but that doesn't suffice when I want to mix variables with escaped braces such as below where name is in the context:

{% raw %}
    {{ hello }} {{ name }}
{% endraw %}

I'm looking for something like:

\{\{ hello \}\} {{ name }}
@Keats
Copy link
Owner

Keats commented Feb 17, 2017

No way to do that no, is it not possible to do {% raw %}{{ hello }}{% endraw %} {{ name }} in your case?

@SergioBenitez
Copy link
Contributor Author

No, the actual use case is more complex. It looks like:

<pre>
  {{
      ${{ bar }}
      something {{ foo }} blah {{ variable }}
  }}
</pre>

where only variable is actually in the context.

I think a nice way to work around this is to allow the brace type to be changed in a small block. So something like:

{% raw '[' %}
    {{ hello }} [[ name ]]
{% endraw %}

Where [[ name ]] is rendered by Tera.

@SergioBenitez
Copy link
Contributor Author

P.S: Handlebars uses \{{ var }} (a single backslash) to escape braces.

@Keats
Copy link
Owner

Keats commented Feb 18, 2017

The other way to do in Jinja2 is to do {{ "{{ var }}" }} which wasn't working until 0.7.2 which I just released.

There has been some people wanting another way to escape than raw and the string printing above in Jinja2 but it never felt that needed (cookiecutter/cookiecutter#11)

@Keats
Copy link
Owner

Keats commented Mar 3, 2017

Can this be closed?

@SergioBenitez
Copy link
Contributor Author

Sure, if {{ "{{ var }}" }} works as expected!

@Keats
Copy link
Owner

Keats commented Mar 3, 2017

Should be ok but do re-open if you have an issue with it!

@Keats Keats closed this as completed Mar 3, 2017
@freinn
Copy link

freinn commented Mar 29, 2017

Hi @Keats I'm using Tera 0.7.2 because is the one that comes with Rocket 0.2.3, and this is not working for me.

Autoescaping is enabled in tera globally (didn't touched that because I can't in the present moment) and my template is this:

{% extends "blog/blog_base" %}

{% block content %}

    <h1>{{ titulo }}</h1> 

    {{ "{{ contenido }}" }}

    <br /><br />
    <a href="/blog">Blog</a>
    <br />

{% endblock content %}

{% block gototop %}
    {{ super() }}
{% endblock gototop %}

But it simply displays {{ contenido }} in the HTML output, when I want the variable value without processing.

@Keats
Copy link
Owner

Keats commented Mar 29, 2017

You want {{ contenido | safe }} if contenido contains HTML. {{ "{{ contenido }}" }} will just render the string {{ contenido }}. That made me realise that safe` is not mentioned in the docs, I'll add it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants