I'm creating a bird's eye view tutorial for Jekyll, to be hosted on Github pages (on my blog that runs on Jekyll). So, I want to put some code there. If I put the following:

{% for post in site.posts %}
{% if post.categories contains '<categoryname>' %}
    <h2>
      <a href="{{ post.url }}">{{ post.title }}</a>
    </h2>
{% endif %}
{% endfor %}

(all lines after tabspaces), it doesn't render as code, rather, it executes. How do I stop it from executing and render it as code?

share|edit|flag
   upvote
  flag
Have a look at the Code snippet highlighting section on jekyllrb.com/docs/templates and stackoverflow.com/questions/6761990/… –  Nick R Feb 26 at 14:39
   upvote
  flag
I tried this too. How do I highlight jekyll? Works on html though –  Ranveer Feb 26 at 14:48
add comment

2 Answers

up vote 2 down vote accepted

The {%...%} syntax used by Jekyll is part of the Liquid templating engine. To escape these tags, and so show them literally, you should use the raw tag.

You will probably want to combine this with the markdown syntax for code blocks. With Redcarpet you can use the triple backtick syntax. It doesn’t matter if you put the backticks inside the raw tags or the other way round:

{%raw%}
```
{% for post in site.posts %}
{% if post.categories contains '<categoryname>' %}
    <h2>
      <a href="{{ post.url }}">{{ post.title }}</a>
    </h2>
{% endif %}
{% endfor %}
```
{%endraw%}
share|edit|flag
   upvote
  flag
Wonderful! This works. –  Ranveer Feb 26 at 15:26
add comment

Enclose your code in backticks:

(tested with redcarpet markdown engine)

```
{% for post in site.posts %}
{% if post.categories contains '<categoryname>' %}
    <h2>
      <a href="{{ post.url }}">{{ post.title }}</a>
    </h2>
{% endif %}
{% endfor %}
```
share|edit|flag
   upvote
  flag
This didn't work :( –  Ranveer Feb 26 at 14:46
   upvote
  flag
What markdown engine are you using? –  zeis Feb 26 at 14:49
   upvote
  flag
The markdown engine is specified in your _config.yml. I have this line in mine: markdown: redcarpet –  zeis Feb 26 at 15:03
   upvote
  flag
Ah! I'm using rdiscount. So is it a problem with the engine @zeis? –  Ranveer Feb 26 at 15:09
1 upvote
  flag
Different engines may use different markers for code blocks. –  zeis Feb 26 at 15:15
add / show 3 more comments

Your Answer

 

Not the answer you're looking for? Browse other questions tagged or ask your own question.