Introduction

When I was writing my blog I found the interesting challenge of presenting Liquid code in my article.

Because the Liquid engine is used to generate the static HTML code, where the Liquid template parsing is done before the Markdown parsing, it is impossible to use the Liquid code directly here

The solutions given online are mostly to use the raw tag, which is somehow not working for me (probablily due to the version incompatibility, or bugs, or issues with the parsing sequence).

{% raw %}{% endraw %}

This doesn’t work.

Solution

Assign the curly braces to Liquid variables, and use the Liquid variables in the text to present the curly braces, so that the syntax like {+% will not be found since the engine only parses once.

example:

1
2
3
4
5
6
{% assign leftcurlybrace = "{" %}
{% assign rightcurlybrace = "}" %}

{{ leftcurlybrace }}% if user %{{ rightcurlybrace }}
  Hello {{ leftcurlybrace }}{{ leftcurlybrace }} user.name {{ rightcurlybrace }}{{ rightcurlybrace }}!
{{ leftcurlybrace }}% endif %{{ rightcurlybrace }}

effect:

1
2
3
{% if user %}
  Hello {{ user.name }}!
{% endif %}

At the end

Writing this article is quite tricky 😂


Creative Commons License Licensed under CC BY-SA 4.0

Comments