I can't seem to get some very basic ruby to work within Jekyll. I have an ultra-simple loop to get my posts from inside /_posts :
<ul>
{% for post in site.posts %}
<li>
<a href="{{ post.url }}">
{{ post.title }}
</a>
</li>
{% endfor %}
</ul>
The posts inside _posts are called
2013-03-16-how-i-do.md
2012-01-01-hello-world.md
Inside _config.yml, I've set permalink: pretty.
What's odd is that it doesn't seem to even iterate through that loop at all— if I add some text inside the loop, even before the li, it doesn't post show up at all.
I'm currently using ruby-2.0.0-p0 — have there been any syntax changes that might be causing this?
Thank you!
First of all are you calling {{ content }} so that Jekyll knows where to run the query?
If you are then this is how I am calling my posts
{% for post in site.posts limit: 10 %}
<a href="{{post.url}}">{{ post.title }}
{{ post.date | date:"%B %d, %Y" }}
{% endfor %}
Related
here is my code so far:
{% for article in blogs["field-notes"].articles limit: 2 %}
<li>{{ article.title | link_to: article.url }}</li>
{% endfor %}
What I want to do is have the blog post featured image as the background of the li
You get the featured image using article.image or article.image.src, and you can also use the img_url filter to get the appropriate size of that image.
{% for article in blogs["field-notes"].articles %}
<li style="background-image:url({{ article.image.src | img_url: 'medium' }});">
{{ article.title }}
</li>
{% endfor %}
i convert my page from liquidluck to middleman and all what need is complete. Only the tag and year cloud i find not really information to finished this setup. Yet all tags and the year from the article is highlight in cloud. This option come from liquid when understand correct. But i find not information about Ruby Erb to convert this part in middleman.
{% block aside %}
<aside class="blog">
<ul>
{% for year, posts in resource.year.iteritems()|reverse %}
<li>
<a href="{{ content_url(site.prefix, year, 'index.html') }}"{% if year == post.date.year %} class="active"{% endif %}>
{{ year }}
<span>{{ posts|length }}</span></a>
</li>
{% endfor %}
</ul>
<ul>
{% for tag, posts in resource.tag.iteritems()|sort %}
<li><a href="{{ tag|tag_url }}"{% if tag in post.tags %} class="active"{% endif %}>{{ tag }}<span>{{ posts|length }}</span></a></li>
{% endfor %}
</ul>
</aside>
{% endblock %}
Has someone a advice to convert this part?
Thank you for help & Nice Day
Silvio
In Jekyll, a post is displayed in its layout thusly:
{{ content }}
Any generators that might have run on that content is displayed like so:
{{ content | toc_generate }}
Unfortunately, this does not work on index pages, as {{ content }} displays nothing on index pages. Instead, we are told to use a for loop:
{% for post in site.posts %}
{{ post.content }}
{% endfor %}
So, the question:
How can I get a generator to run on an index page, since I can't use {{ content }}?
My best guess...
{% for post in site.posts %}
{{ post | toc_generator }}
{% endfor %}
...does nothing.
Any help?
Try this :
{% capture content %}
{% for post in site.posts %}
<h2>{{ post.tile }}</h2>
{{ post.content }}
{% endfor %}
{% endcapture %}
{{ content | toc_generator }}
{{ content }}
Here's the scenario: I have a portfolio website running on Siteleaf (siteleaf.com - which uses liquid template) and I have a portfolio section with several projects as posts. The trick is that I only wish to bring the projects with the tag featured
I was able to bring the posts but only limit the number of posts that is shown, but not the tag that they are assigned. Here's the code I'm using:
<ul class="container work-gallery clearfix">
{% for post in site.pages['work'].posts limit:6 %}
<li class="col-md-6 thumb">
<a href="{{post.url}}" style="background-image: url('{{post.assets.first.url}}');">
<h4>{{post.title}}</h4>
</a>
</li>
{% endfor %}
</ul>
Is there a way to do it? Thanks for the help!
I'll bet you've since figured this out, because you're super close.
There are a few ways to do this (more info here and here). I'll assume for this example that you're using Siteleaf's default tags taxonomy (aka Tag Set).
<ul>
{% for post in taxonomy['tags']['featured'].posts limit:6 %}
<li>
<a href="{{post.url}}">
<h4>{{post.title}}</h4>
</a>
</li>
{% endfor %}
</ul>
You could then just add an if if you wanted to do something with the others.
Alternatively, if you wanted to really party, you could use the assign tag to store a variable, then use it as a condition for output (or for additional tags or variables).
{% assign featured_posts = '' %}
<ul>
{% for post in taxonomy['tags']['featured'].posts limit:6 %}
{% assign featured_posts = featured_posts | append:',' | append:post.id %}
<li>
<a href="{{post.url}}">
<h4>{{post.title}}</h4>
</a>
</li>
{% endfor %}
...
<ul>
{% for post in posts %}
{% unless featured_posts contains post.id %}
<li class="not-featured">
<a href="{{post.url}}">
<h4>{{post.title}}</h4>
</a>
</li>
{% endunless %}
{% endfor %}
</ul>
I am working with the jekyll static site builder, and I am having
difficulty performing the following:
{% for category in site.categories %}
<h2 id = "{{ category[0] }}"> {{ category[0] }} </h2>
{% for post in site.categories[{{ category }}] %}
<li> {{ post.title }}</li>
{% endfor %}
↩
{% endfor %}
I have a post category in my jekyll site called 'test', and I can
display posts from it with the following:
{% for post in site.categories.test %}
<li> {{ post.title }}</li>
{% endfor %}
However, i want to build an archive page automatically, and in order
to do this, I need to embed the category from the outer loop (the loop
that visits all the categories), and use it inside the inner loop to
access posts from that specific category. What do I need to do to get
the first snippet to work how I want it to?
EDIT: Alternatively, is there another way to get the results that I want?
When you do for category in site.categories ,
category[0] will give you the category name,
category[1] will give you the list of posts for that category.
That's the way Liquid handles iteration over hashes, I believe.
So the code you are looking for is this one:
{% for category in site.categories %}
<h2 id="{{ category[0] }}-ref">{{ category[0] }}</h2>
<ul>
{% for post in category[1] %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
<p>↩</p>
{% endfor %}
I've taken the liberty of fixing some markup issues - I've added <ul>...</ul> around the post link list, a <p> around the last link, a semi-colon after the 8617, and also fixed the id at the top (was missing the -ref part).
Regards!
How about...
{% for category in site.categories %}
<h2 id = "{{ category[0] }}"> {{ category[0] }} </h2>
<ul>
{% for post in site.posts %}
{% if post.category == category[0] %}
<li> {{ post.title }}</li>
{% endif %}
{% endfor %}
</ul>
↩
{% endfor %}
Sure, it's pretty inefficient and generates a bunch of extra whitespace, but it gets the job done.
[The original was missing the tags. Just added them. Also, to get ride of the whitespace, one can collapse everything from for post in site.posts to endfor onto a single line.]
{% for post in site.categories.category %}
- OR -
{% for post in site.categories.category[0] %}
Also, I'm not sure why kshep's example doesn't work...