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 %}
Related
while writing an application in django, I've encountered a problem. I want to make page-number links, with current page not being a link. So in template I do this:
{% for i in pages %}
{% if i == curr_page %} {{ i }}
{% else %} {{ i }}
{% endif %}
Only problem? Jinja doesn't seem to notice two numbers being equal. I've changed the 2nd line to {% if i != curr_page %} {{i}}!={{curr_page}} and got "... 5!=6 6!=6 7!=6 ...".
What should I do?
Because they are not of same data type. In your view, cast them to int before passing to context dict.
pages = list(map(int, pages))
curr_page = int(curr_page)
Short Version:
Why does the following code not produce an output when navbox.next_article is the string '2018-01-05-man-command'?!
{% capture np %} {{ site.posts | where:"post","navbox.next_article contains post.title" }} {% endcapture %}
The next post is {{ np.title }}
Details
My post 2018-01-05-man-command.md has a YAML front matter:
---
layout : post
title : 'Man Command'
tags : [RHCSA, RHCSA_mod, Using Essential Tools, Man Command]
categories: [RHCSA]
navbox:
# prev_article:
next_article: 2018-01-05-understanding-globbing-and-wildcards
---
This is accessed by the _includes/post.html file through:
{% unless include.excerpt %}
{{ post.content }}
{% include navbox.html navbox=page.navbox %}
{% endunless %}
This is used by the _layout/post.html which sets the layout for the post:
{% include post.html post=page link_title=false %}
My navbox.html contains:
{% assign navbox = include.navbox %}
{% capture np %} {{ site.posts | where:"post","navbox.next_article contains post.title" }} {% endcapture %}
The next post is {{ np.title }}
However, all I get when I run bundle exec jekyll serve is:
The next post is
Why does that line not work? I'm new to jekyll so it's possible I've made a blunder somewhere that's intuitive to most. Please tell me what I can fix.
I believe that the capture tag only captures strings, not posts. See here for more info.
I'm not convinced that a where filter supports the contains syntax you're using. See here for more info.
On top of that, where returns an array. You have to get the first item from that array.
You need to fix these issues. Use an assign instead of a capture to store a post. And change your where filter to not use the contains syntax, which isn't valid. (Unless it's been added since the issue I just linked.)
Here is how I've done it:
{% assign post = site.posts | where:"url", targetUrl | first %}
I wanted to create a simply include which simplifies creating a carousel. So I was looking for the syntax to give a list of image urls as a parameter. However I couldn't find anything about this.
{% include carousel.html images=WHAT HERE? %}
You can create a list as a string then convert it into an array inside your snippet. This is the way I would do it.
{% assign urls = 'url1.jpg,url2.jpg,url3.jpg' %}
{% include carousel.html images=urls %}
Of course this is the same as:
{% include carousel.html images='url1.jpg,url2.jpg,url3.jpg' %}
carousel.html
{% assign images = include.images | split: ',' %}
{% for image in images %}
{{ image }}
{% endfor %}
I'm trying to see if it's possible to iterate a variable in Jekyll for a testimonial block I'm implementing for a Jekyll site. Basically, I'd like to have an icon be multiplied by the number dictated in my collection. Is this even possible with liquid markdown? Here's a snippet:
{% assign star = "<i class="icon-star"></i>" %}
{% assign star = star | times:{{ testimonials.stars }} %}
I'm thinking there's better ways to do this, but I was curious what I could get away with front matter.
To do it iterating, you can use a for loop appending the desired string to a variable:
{% assign star = '<i class="icon-star"></i>' %}
{% assign result = '' %}
{% for time in testimonials.stars %}
{% assign result = result | append: star%}
{% endfor %}
The key is to use a for block. You don't have to write multiple assign statements.
The following will render a star per the front matter key rating in any page using the code.
Modify the for bock as required.
{% assign star = '<i class="icon-star"></i>' %}
<div class="rating">
{% for count in (1..page.rating)) %}
{{ star }}
{% endfor %}
</div>
Ref: docs
I want to show a single content entry (from one content type) on a page and I want to access this page directly with a link.
So far I've created the content type "posts" (with wagon generate ...). It contains the fields "title","date" and "body". On the page "posts", all title's of the posts are listed and when you click on one of the title, you should be redirected to the subpage "post" which contains the rest of the content (depending on which post you selected).
posts.liquid:
{% extends parent %}
{% block main %}
{% for post in contents.posts%}
<li>{{ post.date }} - {{ post.titel }} </li>
{% endfor %}
{% endblock %}
This lists all posts.
post.liquid:
{% extends parent %}
{% block main %}
<h2>{{post.title}}</h2>
<h3>{{post.date}}</h3>
<p>{{post.body}}</p>
{% endblock %}
And this shoul be the template for the rest of the content on a single page.
How can I link the list elemnts to the correct post?
I'm using wagon to develop the site local.
I found a solution to my problem.
To access only one specific entry in the content type posts, you need to create a template which holds the content with the correct layout.
This means you need a file named "content-type-template.liquid" and this file has to be placed in a folder (in my case named "post") to define the parent:
/posts.liquid # Holds a list of all Posts
/post/content-type-template.liquid # Holds the layout for only one post
Also in the top of content-type-template.liquid you need to define the content-type and the slug:
---
title: Post Template
content_type: post
slug: content_type_template
---
The fields of the content type are now reachable with the following syntax:
{% extends parent %}
{% block main %}
<h2>{{post.title}}</h2>
<h3>{{post.date}}</h3>
<p>{{post.body}}</p>
{% endblock %}
If the content type is for example named "product", you need to rename everything above called "post" with "product".
Finally you can reach a single entry with it's slug.
{% extends parent %}
{% block main %}
{% for post in contents.posts%}
<li>{{ post.date }} - {{ post.titel }} </li>
{% endfor %}
{% endblock %}
Here are some links that helped me:
http://www.tommyblue.it/2011/02/28/how-to-build-a-website-with-locomotive-cms-from-scratch/
http://doc.locomotivecms.com/references/api/objects/content-entry