how to displayliquid code in Jeckyll post - jekyll

Hello I am trying to make a post using jeckyll and as part of my post I would like to show some liquid code. The post should display the IF statement as part of the post text (example below).
{% if customer and customer.tags contains 'Wholesale' %}
{% endif %}
I have tried to post this as
{% highlight liquid %}
{% if customer and customer.tags contains 'Wholesale' %}
{% endif %}
{% endhighlight %}
and also
{% highlight markdown %}
{% if customer and customer.tags contains 'Wholesale' %}
{% endif %}
{% endhighlight %}
but anything I try seems to be still executing the liquid code.
Is there a way to display the IF statement my post?

Try wrapping your liquid code in {% raw %} {% endraw %} like this:
{% highlight liquid %}
{% raw %}
{% if customer and customer.tags contains 'Wholesale' %}
{% endif %}
{% endraw %}
{% endhighlight %}
The raw tag will disable any liquid processing and output your code as desired.

Related

Jekyll nested include with for loop

I'd like to use a {% for %} loop in an included file, to avoid repeating logic to create the loop array (it's an assign with multiple where_exps).
But I'd like to use different content depending on where I include the loop, so sometimes I will do this:
{% for i in a %}
<li>{{ i.name }}</li>
{% endfor %}
and sometimes:
{% for i in a %}
<loc>{{ i.url }}</loc>
{% endfor %}
How can I achieve this? So far I have to put each of the inner contents in their own template, so I would have files like below, but I'd like to avoid the extra template files, and just keep that content in the appropriate main file:
html_template.html:
<li>{{ i.name }}</li>
xml_template.xml:
<loc>{{ i.url }}</loc>
page_loop.html:
{% assign a = "logic I don't want to repeat" %}
{% for i in a %}
{% include {{ include.inner_template }} %}
{% endfor %}
html_main.html:
{% include page_loop.html inner_template="html_template.html" %}
xml_main.xml:
{% include page_loop.html inner_template="xml_template.xml" %}
It would probably be another more elegant (?) solution developing a plugin, but quickly modifying your code, in _includes/page_loop.html:
{% assign a = "something" %}
{% for i in a %}
{%if include.type == "name"%}
<li>{{ i.name }}</li>
{%else if include.type == "url"%}
<loc>{{ i.url }}</loc>
{%endif %}
{% endfor %}
Then each time you include page_loop.html pass an additional parameter specifying which type of output you want:
{% include page_loop.html type="name" %}
or
{% include page_loop.html type="url" %}

Django Multi-Conditional Statement

I have the following code working in a Django HTML template. However, it’s quite repetitive. How can this code be simplified?
It translates to, "If you're not staff, you get to see nav.html. If you are staff, you only get to see nav.html if you're on these 4 pages."
{% if not request.user.is_staff %}
{% include ‘nav.html’ %}
{% else %}
{% if request.get_full_path == ‘/one/’ %}
{% include ‘nav.html’ %}
{% if request.get_full_path == ‘/two/’ %}
{% include ‘nav.html’ %}
{% if request.get_full_path == ‘/three/’ %}
{% include ‘nav.html’ %}
{% if request.get_full_path == ‘/four/’ %}
{% include ‘nav.html’ %}
{% endif %}
{% endif %}
From your view, you can send a list containing the paths for which to show nav.html. Something like,
allowed_paths = ['/one/', '/two/', '/three/', '/fourth/']
In your template, you can simply do,
{% if not request.user.is_staff or request.get_full_path in allowed_paths %}
{% include 'nav.html' %}
{% endif %}

How to check if a value is present in a list?

I would like to get the list of posts having a specific tag in their front matter (*)
I tried the code below, which iterates over all the tags of the current post pages (the current tag is t), then iterates over all the posts (p) to check if they have this tag (and just outputs the title, for debugging reasons):
{% for t in page.tags %}
{% for p in site.posts %}
{% if t in p.tags %}
{{ p.title }}
{% endif %}
{% endfor %}
{% endfor %}
The line {% if t in p.tags %} seems to fail (I come form a Python background so I gave it a try) and I cannot find the in operator in liquid. Does it exits?
(*) I am mentioning what I want to achieve in case there is a more straightforward way to do that but I am still interested in the general question.
Following your example it can be done with the contains tag:
contains can also check for the presence of a string in an array of
strings.
{% if product.tags contains "outdoor" %}
This product is great for
using outdoors!
{% endif %}
So to get the list of posts having a specific tag in their front matter (in this case the posts with the tag mytag):
{% assign posts_with_mytag = site.posts | where_exp:"item",
"item.tags contains 'mytag'" %}
{% for post in posts_with_mytag %}
{{post.title}}
{% endfor %}
Following-up on #maracuny's answer, the corrected code from my question for completeness:
{% for t in page.tags %}
{% for p in site.posts %}
{% if p.tags contains t %}
{{ p.title }}
{% endif %}
{% endfor %}
{% endfor %}

Jekyll (Liquid): Setting post state in included templates

Maybe this is a case of a Python programmer trying to work with Ruby and maybe this is a "feature" -- I don't know. For the life me I can't figure out how to set state on posts during the rendering process.
I have a _layout that just calls include twice:
{% include templateA %}
{% include templateB %}
templateA walks the posts and renders some of them on the basis of some_condition.
{% for post in site.posts %}
{% if some_condition %}
<!-- Render the post -->
{% assign post.rendered = true %}
{% endif %}
{% endfor %}
templateB attempts to walk the posts and render the rest:
{% for post in site.posts %}
{% unless post.rendered %}
<!-- Render the post -->
{% endif %}
{% endfor %}
This does not work as expected. I have also tried the {% assign post[rendered] = true %} syntax. No errors are thrown; just silent failure.
Where am I failing here? Is my mental model for the rendering process just totally wrong? Thanks!
Your first though was nearly ok. The unless tag does the contrary of if.
But you cannot change jekyll's variables, they are "freezed".
So, this might work :
templateA
{% for post in site.posts %}
{% if same_condition %}
<!-- Render the post -->
{% endif %}
{% endfor %}
With the same condition unless will render posts not rendered by the first loop.
templateB
{% for post in site.posts %}
{% unless same_condition %}
<!-- Render the post -->
{% endunless %}
{% endfor %}

include_relative post.path jekyll

I'm trying to put a number of related posts on a single page. The problem is that the included posts are all textile pages, using some {% highlight %} tags. When I try to include then via post.content, they don't get textilized (e.g, I see "{% highlight..." on the page).
I've tried a few different things:
This never gets textilized:
{% for post in site.tags.my_tag %}
{{ post.content | textilize }}
{% endfor %}
This (based on http://nateeagle.com/2011/08/31/jekyll-not-parsing-includes/) returns no content:
{% for post in site.tags.apidocs %}
{% capture included_post %}
{{% include_relative post.path %}}
{% endcapture %}
{{ included_post | textilize }}
{% endfor %}
Any ideas?
Bradley
Try
{% include_relative {{post.path}} %}