Liquid conditional rendering - jekyll

I have blog posts to which I add tags in the YFM like this:
tags: amp
I want Liquid to check whether the tag == amp and if so, to add a link in the blog-post.html layout file. I tried the code below:
{% if page.tags == "amp" %}
link
{% endif %}
But nothing gets outputted

The tags attribute in the YFM should actually be stored as an array since there can be multiple tags, as seen in the docs.
tags: [amp, foo, bar]
When checking the tags, use the contains liquid filter.
{% if page.tags contains 'amp' %}
link
{% endif %}

Related

Counting items in Django templates

Is it possible to tally items being listed as part of the django template within the html?
For example, I have a django template with the following code snippet in it:
<div>
{% for thing in thing_list %}
{% if thing.status == "n" %}
<a>{{ thing.status.count }}</a>
{% endif %}
{% endfor %}
</div>
This django template displays all of the things in a list, and I can call each attribute of the thing and display it, so I know I have access to all of the fields.
I want to count then number of "things" and display that number as text. My current attempt above isn't working. Does anyone have any suggestions?
As Willem says, you should do this in the view. Rather than passing a list of all Things and then checking their status in the template with if, you should filter your Things when querying them from the database in the first place:
thing_list = Thing.objects.filter(status='n')
Then you can do just {{ thing_list.count }} in the template.

How to make Django HTML IF statement to include all child paths of current url

I can use django html logic (not sure what it is called) to post something on the page if the url is exactly something (see below). How do I get the if statement to be true for all child pages. E.g. if the path is '/chat/', then '/chat/1/1', '/chat/members', etc. Is it possible to use the name space somehow?
HTML
{% if request.get_full_path in "/chat/" %}
<p>found it</p>
{% endif %}
URL Namespace
url(r'^chat/', include("chat.urls",namespace='chat')),
App URL Namespace
url(r'^$', chat_view, name='chats'),
You can do it like this!
{% url 'chat:chats' as first_url %}
{% if first_url not in request.path%}
<p>You are not in the chats namespace</p>
{% endif %}

In Jekyll, how can I create a tag archive for pages?

I'm using tags on pages in Jekyll, like this:
---
title: Sample page
permalink: /sample/
tag: news
---
On my homepage, I have different sections that aggregate pages by tag, like this:
{% assign counter = '0' %}
{% for page in site.pages %}
{% for tag in page.tags %}
{% if tag == "news" and counter < '3' %}
{% capture counter %}{{ counter | plus:'1' }}{% endcapture %}
<li>{{page.title}}</li>
<div class="summary">{{page.summary}}</div>
{% endif %}
{% endfor %}
{% endfor %}
This loop limits the news-tagged pages to 3, but I might have 10+ pages with the tag of "news." I want to include a "View all" tag at the bottom, so that users can see a complete list of all pages matching that tag.
I realize I could manually create a page and add similar code but without a limit to get all the pages. However, that's kind of tedious. I'd rather have Jekyll auto-generate a tag archive by default. I think Jekyll has a concept of generators, but I'm not sure how to implement a tag page generator.
How can I dynamically generate tag archive pages without creating and entering code on each page?
check out jekyll-archives plugin. i just found out about this and am able to generate categories and tags pages. it does increase compile time coz of the number of categories and tags i have but it does what i need it to do. https://github.com/jekyll/jekyll-archives
Actually you do not need a plugin to do that. Check this out: http://codinfox.github.io/dev/2015/03/06/use-tags-and-categories-in-your-jekyll-based-github-pages/

Pass an include parameter inside a liquid call

I have a page say index.html and a file with liquid code called tags. I want to pass a parameter which I specify in index.html to a liquid call in tags. Namely,
index.html
<div>
{% include tags param="site.categories" %}
</div>
Tags
{% assign tags_list = {{ include.param }} %}
...
The {% assign tags_list = {{ include.param }} %} does not work for some reason. Is it possible to do so because this allows me to use tags file for multiple purposes? Instead of writing liquid calls in every page Iwould be able to just do {% include tags param="something" %}. Thanks.
Well, {% include tags param="site.categories" %} is not passing the site.categories hash but the "site.categories" string.
The right syntax is :
{% include tags.html param=site.categories %}

Overriding YAML frontmatter variables from Liquid

I would like my outermost Liquid template file to reference a JavaScript file only when the page requires it. I created a page variable, page.slideshow, which controls this:
default.html:
{% if page.slideshow %}
<script type="text/javascript" src="/js/slideshow.js"></script>
{% endif %}
Now I have a template that "inherits" from default.html, which allows the page to specify in its YAML frontmatter a list of images (page.images). Only when this list has more than one element do I want slideshow.js to be included in the page.
My attempt at this looks something like:
other.html
---
layout: default
---
{% assign imagecount = page.images | size %}
{% if imagecount > 1 %}
{% assign page.slideshow = true %}
{% endif %}
However, this does not seem to affect the rendering of default.html, because the script tag is not included in the final page.
As a workaround, I can modify the frontmatter of each page that lists multiple images to explicitly set slideshow: Yes, or change default.html to check page.slideshow or (page.images | size) > 0 but I'd prefer to structure it closer to what I attempted above.
I've had a play about and you can't really do it the way you've suggested. It seems page variables just cant be modified once they're set. You might be able to set it with Jekyll plugins, but that's a whole other thing.
The cleanest way I have found to do it is to put all the logic in your main, outermost template.
{% assign imageCount = page.images | size %}
{% if page.slideshow or imageCount > 1 %}
{{ page.images | size }}
<script src="/js/slideshow.js"></script>
{% endif %}