Check markdown file has value with Jekyll template - jekyll

I have a loop that goes through a folder of markdown files and displays the titles of each in a dropdown. I need to be able to filter the results of the loop based on whether or not a markdown file has an "office" value in it.
I currently have this:
<select name="practice-area" type="search" class="practice-areas-list select-table-filter" data-table="order-table">
<option value="default">Practice Areas</option>
{% for practice_area in site.practice_areas %}
{% unless practice_area.office %}
<option value="{{ practice_area.title }}">{{ practice_area.title }}</option>
{% endunless %}
{% endfor %}
<select>
where {% unless practice_area.office %} should be checking if the file has office in it. If so, pull title into list.
Sample Markdown File
---
title: page title
slug: page-title
office:
-22
---
Not sure of the proper Jekyll syntax for this to work correctly.

if tag contains a section of template which will only be run if the condition is true while unless would be run if the condition is false.
So to check if that key is present in your post front-matter, then you need to use the if statement:
{% if practice_area.office %}
...
{% endif %}

Realized I needed the opposite values so this works:
{% unless practice_area.office %}
{% endunless %}
Gives me all that don't have an office.

Related

Jekyll - Include multiple parameters in single include?

Is it possible to include multiple parameters in single include?
Single:
{% include card.html class=include.class1 %}
Multiple??
{% include card.html class=include.class1 && include.class2 %}
Or do I have to do class1=include.class1 class2=include.class2?
Multiple include parameters can be passed separated with a space param1=value1 param2=value2,e.g.:
{% include image.html url="http://jekyllrb.com"
max-width="200px" file="logo.png" alt="Jekyll logo"
caption="This is the Jekyll logo." %}
Then you can access them inside the include file prefixing it with include., for example:
{{include.file}} {{include.caption}}
As stated by #marcanuy,
One way is to use the capture function to include multiple values into a single parameter.
{% capture classes %} {{include.class1}} {{include.class2}} {% endcapture %}
{% include card.html class=classes %}
Here's my use case for this-
an include that has html for a radiobutton set, like so:
<label>{{include.label}}</label>
{% for option in include.options %}
<input type="radio" name="{{include.label}}" id="{{include.option}}" value="{{include.option}}" checked="checked"/><label for=" {{include.option}}">{{include.option}}</label>
{% endfor %}
that you call like this:
{% include radiobuttons.html label="favorite color" options="green", "blue", "orange", "red" %}

How to pass a frontmatter value into a for loop

I want to use a value in my frontmatter to specify a data file to loop through, but I can't get this to work.
I have a data file in _data/sidebars/sidebar1.yml. It contains:
- first
- second
- third
On a page I have this frontmatter:
---
title: My page
sidebar: site.data.sidebar.sidebar1
---
I want to use this code:
{% for entry in page.sidebar %}
* {{entry}}
{% endfor %}
However, this doesn't work. I've tried a number of things (involving assign and capture tags to define the page.sidebar content, but nothing seems to work).
The only thing that works is to do this:
{% if page.sidebar == "site.data.sidebars.sidebar1" %}
{% assign sidebar = site.data.sidebars.sidebar1 %}
{% endif %}
{% for entry in sidebar %}
* {{entry}}
{% endfor %}
However, I would like to avoid this extra code with the if statement, since it's easy to forget this and I would like to automate this more.
Any ideas on how to make this work?
You have a typo in your front matter. It's :
sidebar: site.data.sidebars.sidebar1
not
sidebar: site.data.sidebar.sidebar1
You can even be less verbose.
In your front matter : sidebar: sidebar1
In your code :
{% assign sidebar = site.data.sidebars[page.sidebar] %}
{% for entry in sidebar %}
{{ entry | inspect }}
{% endfor %}

Get data from data folder?

I need to get some data using a dynamic name from front matter:
{{ site.data.prd-[page.tag].title" }}
The above fails to get the string from the data folder and nothing is output.
Where am I going wrong?
I am not sure about what you are trying to do but how about make a dictionary type data in _data directory and search for a title that matches page.tag in desired pages?
_data.prd-tags.yml
For each item, we can access the key and value using index ([0]: key, [1]: value).
jekyll: "jekyll's title"
ruby: "ruby's title"
html: "html's title"
.
.
.
HTML fragment for searching and displaying a tag (if found)
Iterate over site.data.prd-tags and display title if an item that matches page.tag is found.
<div class="tag-title">
{% for tag in site.data.prd-tags %}
{% if tag[0] == page.tag %}
{{ tag[1] }}
{% else %}
Nothing found
{% endif %}
{% endfor %}
</div>

How to add a second tag list using pelican-bootstrap3

I would like to list the Lego sets I used to build my models just like tags but in a separate list. According to the pelican documentation this should be possible. But when I run pelican content I get
Tags Animal / Duplo / MOC
Sets 1 / 0 / 5 / 7 / 1
instead of
Tags Animal / Duplo / MOC
Sets 10571
I modified the pelican-bootstrap theme from Daan Debie by adding {% include 'includes/setlist.html' %} to article_info.html.
This is what my setlist.html file looks like:
{% if article.sets %}
<span class="label label-default">Sets</span>
{% for set in article.sets %}
{{ set }}
{% if not loop.last %}
/
{% endif %}
{% endfor %}
{% endif %}
This is what my markdown file looks like:
Title: Girafe
Date: 2015-11-29 14:22:20
Modified: 2015-11-29 14:22:27
Category:
Tags: Animal, Duplo, MOC
Sets: 10571
Slug: girafe
Authors: Yann Baumgartner
![Girafe][girafe]
[girafe]: {filename}/images/girafe.jpg "Girafe"
I read through all the pelican questions on stackoverflow but couldn't find an answer. I tried the following:
If I use the taglist code within setlist without changing any
variables then the tags are displayed correctly
Renaming the variable
name to set_numbers didn't work.
Removing set.url didn't work.
Am I missing something (a template file, a jinja2 filter)? Any hint would be much appreciated.
pelican doesn't process any non-standard metadata. It'll be left as a string. So, article.sets will be a single string containing "10571". If you loop over that, you'll get individual characters. You need to process it yourself via a plugin or inside your template like:
{% if article.sets %}
<span class="label label-default">Sets</span>
{% for set in article.sets.split(',') %}
{{ set|trim }}
{% if not loop.last %}
/
{% endif %}
{% endfor %}
{% endif %}
PS: Also, I'm not sure what you expect the set.url to be. Again, since pelican doesn't do anything special with your custom metadata, it will be basic string and it won't have an url.

How to remove white space from html source code

I am using django and python. In the template file, I have a drop-down list which is shown as below. It works. The only problem is that there is a lot of white space between in the source html code. Is there any way to remove the white space? Thanks.
{% for lang_ele in video.languages.all %}
{% ifequal lang_ele.lang display_language %}
{% for key, value in language_table.items %}
{% ifequal lang_ele.lang key%}
<option selected = "selected" value={{lang_ele.lang}}>{{value}}</option>
{% endifequal %}
{% endfor %}
{% else %}
{% for key, value in language_table.items %}
{% ifequal lang_ele.lang key%}
<option value={{lang_ele.lang}}>{{value}}</option>
{% endifequal %}
{% endfor %}
{% endifequal %}
{% endfor %}
The output html souce code looks like this:
<option value=de>German</option>
<option value=el>Greek</option>
<option value=hi>Hindi</option>
You can use the spaceless template tag. It:
Removes whitespace between HTML tags.
{% spaceless %}
<p>
Foo
</p>
{% endspaceless %}
Look toward middelware, eg "htmlmin". It processes the file at a time. In addition it has a decorator.
https://crate.io/packages/django-htmlmin
gzip will give the same effect. Probably would have cost to opt out of trimming.
Look towards django middelware or nginx gzip.
django.middleware.gzip.GZipMiddleware
http://wiki.nginx.org/HttpGzipModule
You use the controller/model logic in the template. This is wrong way.
I used custom helper function with stripping. Here is a an example I used too: https://web.archive.org/web/20140729182917/http://cramer.io/2008/12/01/spaceless-html-in-django/