How to escape liquid template tags?
Asked Answered
A

9

104

This sounds very easy, however I couldn't find it anywhere in the docs. How can I write {% this %} in a liquid template, without it being processed by the engine?

Aramenta answered 6/8, 2010 at 17:25 Comment(1)
You can always comment out.Humorous
S
127

For future searchers, there is a way to escape without plugins, use the code below:

{{ "{% this " }}%}

and for tags, to escape {{ this }} use:

{{ "{{ this " }}}}

There is also a jekyll plugin for this which makes it a whole lot easier: https://gist.github.com/1020852

Raw tag for jekyll. Keeps liquid from parsing text betweeen {% raw %} and {% endraw %}

Reference

Stillhunt answered 3/5, 2011 at 7:29 Comment(5)
Great, thanks for the reference. For some reason I wasn't able tno find this in the liquid docs.Aramenta
You no longer need a plugin to use {% raw %}, for me it works out of the box and now it's in the docsPhilibeg
is it possible to escape code with ``` instead of {% in jekyll?Anodize
The first couple suggestions didn't work for me, but using raw did: {% raw %}{{ this }}{% endraw %}.Tuber
it worked, thanks ... see a real example here g14n.info/2014/08/moving-from-blogger-to-github-pages/#commentsPenang
O
137

it is possible to disable liquid processing engine using the raw tag:

{% raw  %}
{% this %}
{% endraw %}

will display

{% this %}
Outrage answered 27/11, 2012 at 10:46 Comment(6)
I believe this used to be a plug-in. Did it get into the core engine in the meantime?Aramenta
It looks like the raw tag has been added a year ago in the core engine. See github.com/Shopify/liquid/commits/master/lib/liquid/tags/raw.rbOutrage
However, this escape is not supported by Github.Buckden
@LeiMing Looks like it is now (probably since May 2013)Yentai
Confirmed, it works on GitHub Pages. The raw tag was introduced in Liquid 2.3.0 and GitHub Pages is currently using version 2.5.5.Hysell
One potential issue, if I want to print {% endraw %}, {% raw %}{% endraw %}{% raw %} won't render.Barrie
S
127

For future searchers, there is a way to escape without plugins, use the code below:

{{ "{% this " }}%}

and for tags, to escape {{ this }} use:

{{ "{{ this " }}}}

There is also a jekyll plugin for this which makes it a whole lot easier: https://gist.github.com/1020852

Raw tag for jekyll. Keeps liquid from parsing text betweeen {% raw %} and {% endraw %}

Reference

Stillhunt answered 3/5, 2011 at 7:29 Comment(5)
Great, thanks for the reference. For some reason I wasn't able tno find this in the liquid docs.Aramenta
You no longer need a plugin to use {% raw %}, for me it works out of the box and now it's in the docsPhilibeg
is it possible to escape code with ``` instead of {% in jekyll?Anodize
The first couple suggestions didn't work for me, but using raw did: {% raw %}{{ this }}{% endraw %}.Tuber
it worked, thanks ... see a real example here g14n.info/2014/08/moving-from-blogger-to-github-pages/#commentsPenang
N
15

You can escape liquid tags in Jekyll posts using {% raw %} {% endraw %} i.e

{% raw %}
  {% for post in site.posts %}
     {{ post.content }}
  {% endfor %}

{% endraw %}

will produce

  {% for post in site.posts %}
     {{ post.content }}
  {% endfor %}
Nonanonage answered 7/11, 2016 at 19:44 Comment(0)
B
14

BTW:

If you want to display {{ "{% this " }}%}in Jekyll, you can code like this:

{{ "{{ " }}"{{ "{% this" }} " }}{{ "}}%}

To escape {{ "{{ this " }}}}use:

{{ "{{ " }}"{{ "{{ this" }} " }}{{ "}}}}
Buckden answered 18/10, 2012 at 6:59 Comment(2)
Ha, ha hahahah ah aaaaahhh.Hefter
wtf is going on here, I literally have no ideaLinnlinnaeus
D
10

There is another option: to use HTML special characters codes for replacing the curly braces with its matching codes:

  • replace each { with {
  • replace each } with }

For more details about this solution see: http://www.tikalk.com/devops/curly_brances_workaround/

Dagmar answered 14/12, 2015 at 11:40 Comment(0)
A
4

I found a omnipotent way to display any text with curly braces. You can assign plain text to a variable, and display it.

{% assign var = "{{ sth }}" %}
{{ var }}
Archicarp answered 30/12, 2016 at 3:11 Comment(1)
Thank you! Useful to escape double or single quotesCaruncle
H
2

As mentioned here also, plain {% raw %} and {% endraw %} are only the second best solution since those are shown if you look up the Markdown on normal github.com.

The best way is to put {% raw %} and {% endraw %} in HTML comments:

<!-- {% raw %} -->
something with curlky brackets like { this } and { that }
<!-- {% endraw %} -->

Due to the HTML comments it is seen by Github as a comment. In Github pages the raw tags will prevent the parsing of the curly brackets in between the tags.

Haddock answered 19/7, 2019 at 23:5 Comment(1)
I like this method as, for some reason, it doesn't mess with my excerpts.Excommunicative
F
1

I tried {% raw %} something {% endraw %} ,

and {{ "{% this " }}%}. But they both don't work.

finally, my working answer is {{ "{%" xxx }} something }}.

My code:

{{ "{%" }} extends 'xadmin/base_site.html' %}
{{ "{%" }} block nav_form %}
    <h3>{{ "{{" }} title }}</h3>
    {{ "{%" }} for i in context1 %}
        <p>{{ "{{" }} i }}</p>
    {{ "{%" }} endfor %}
{{ "{%" }} endblock %}

The result:

{% extends 'xadmin/base_site.html' %}
{% block nav_form %}
    <h3>{{ title }}</h3>
    {% for i in context1 %}
        <p>{{ i }}</p>
    {% endfor %}
{% endblock %}
Froe answered 17/8, 2019 at 16:14 Comment(0)
F
0

Allows output of Liquid code on a page without being parsed.

{% raw %}{{ 5 | plus: 6 }}{% endraw %} equals 11.

{{ 5 | plus: 6 }} equals 11.

For more details about this solution see: https://www.shoplazza.dev/docs/theme-tags

Fivepenny answered 24/4, 2022 at 5:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.