This is probably an easy question, but how do I show previews of my posts on the default page? I am using the Jekyll Bootstrap theme Tom.
This also works at least as of 1.0.0, is built in and is simple to use.
<ul>
{% for post in site.posts %}
<li>
{{ post.title }}
<p>{{ post.excerpt }}</p>
</li>
{% endfor %}
</ul>
See here.
Looking through the functions here, I found strip_html and truncatewords.
Here's a sample "posts list" with 75 words of preview text.
<ul >
{% for post in site.posts limit 4 %}
<li><span>{{ post.date | date_to_string }}</span> » {{ post.title }}</li>
{{ post.content | strip_html | truncatewords:75}}<br>
Read more...<br><br>
{% endfor %}
</ul>
I liked the <!--more--> comment approach from WordPress, so I wrote something along those lines:
_plugins/more.rb:
module More
def more(input, type)
if input.include? "<!--more-->"
if type == "excerpt"
input.split("<!--more-->").first
elsif type == "remaining"
input.split("<!--more-->").last
else
input
end
else
input
end
end
end
Liquid::Template.register_filter(More)
Your post would then look like this:
---
layout: post
title: "Your post title"
published: true
---
<p>This is the excerpt.</p>
<!--more-->
<p>This is the remainder of the post.</p>
You can then use it in your templates like so:
Show excerpt (everything above the <!--more--> comment):
<summary>{{ post.content | more: "excerpt" }}</summary>
Show remainder (everything after the <!--more--> comment):
<article>{{ post.content | more: "remaining" }}</article>
Any argument other than excerpt or remaining will simply show the whole post.
This is an old question, still would like to add a workaround for the formatting issue, as raised in #Talon876 's answer here.
At the end of each post adding closing tags like </em>,</strong> or </b> might not be so neat, but it maintains formatting while showing the excerpts.
For example:
<ul >
{% for post in site.posts limit 4 %}
<li><span>{{ post.date | date_to_string }}</span> » {{ post.title }}</li>
{{ post.content | strip_html | truncatewords:75}}
</em></strong> <!-- This is what does the task-->
<br>
Read more...<br><br>
{% endfor %}
</ul>
Related
I'm using Octopress 2.0 for blogging, which uses the Jekyll static site generator.
To mark an excerpt of a blog post for the front page, you can insert <!--more--> into the post, and the content up to that point will be used as the excerpt..
For some posts, even though I want an excerpt, I want to exclude certain things from it (maybe a table of contents that only makes sense on the post, or some extra notes that aren't useful for an excerpt).
Is there a way in Octopress/Jekyll/Liquid to use the <!--more--> method to generate an excerpt, but also mark some small amount of content to be left out it?
Here is a brief example. Take this post:
---
layout: post
title: "Example Post"
---
This is the first paragraph. It will be included in the excerpt.
[Jump to third paragraph](#para3). This paragraph should **not** be in the excerpt.
This is the second paragraph. It will be included in the excerpt.
<!--more-->
<a name="para3"></a>This is the third paragraph. It won't be in the excerpt.
I want a way to have the generated excerpt for this post be:
This is the first paragraph. It will be included in the excerpt.
This is the second paragraph. It will be included in the excerpt.
Edit : I now understand what you're trying to do.
I assume that you're using the default octopress markdown : rdiscount.
Let's got the filter way :
In Gemfile add gem 'nokogiri'
In your post, the idea is to add a span.secondary to the part that has sometimes to be stripped. content
...
remove_secondary_content_from_excerpt : true
---
This is the first paragraph. It will be included in the excerpt.
[[Jump to third paragraph](#para3). This paragraph should **not**
be in the excerpt.](class:secondary)
This is the second paragraph. It will be included in the excerpt.
<!--more-->
### This is the TEXT title
This is **the text**
In _includes/article.html
...
</header>
{% endunless %}
{% if index %}
<div class="entry-content">
{% if post.remove_secondary_content_from_excerpt == true %}
{% capture secondary_content %}{{ post.excerpt | excerpt_get_secondary_content }}{% endcapture %}
{{ post.excerpt | remove: secondary_content }}
{% else %}
{{ post.excerpt }}
{% endif %}
</div>
{% capture excerpted %}{{ content | has_excerpt }}{% endcapture %}
{% if excerpted == 'true' %}
<footer>
<a rel="full-article" href="{{ root_url }}{{ post.url }}">{{ site.excerpt_link }}</a>
</footer>
{% endif %}
{% else %}
<div class="entry-content">
<!-- example on how to use it in post page -->
{% if page.remove_secondary_content_from_excerpt == true %}
{% capture secondary_content %}{{ page.excerpt | excerpt_get_secondary_content }}{% endcapture %}
{{ page.excerpt | remove: secondary_content }}
{{ secondary_content }}
{{ page.content | markdownify | remove: page.excerpt }}
{% else %}
{{ content }}
{% endif %}
</div>
{% endif %}
In _plugins/octopress_filters.rb
...
module OctopressLiquidFilters
def excerpt_get_secondary_content(input)
require 'nokogiri'
doc = Nokogiri::HTML(input)
# as excerpt can surrounded by one <p> (when no double newline in it)
# or with multiple <p> when a double newline is found
multiparagraph = doc.css("p").length > 1
if multiparagraph
# look for parent <p>
xpathString = "span.secondary/.."
end
# look only for the span element
xpathString = "span.secondary"
else
secondary = doc.css(xpathString)
secondary.to_s
end
...
Install Nokogiri bundle update
I hop your rake generate will make you happy.
I am using jekyll with Github pages for my website.
I am trying to make some posts not visible in the home but they can be linked from another post.
In the frontmatter I tryed to add a field visible like this:
---
layout: post
title:
excerpt:
visible:1
---
And then in the index.html file I did a if check:
<div class="posts">
{% for post in paginator.posts %}
{% if post.visible== 1 %}
<div class="post">
<h1>
<a href="{{ post.url }}">
{{ post.title }}
</a>
</h1>
<span class="post-date">{{ post.date | date_to_string }}</span>
<a class="subtitle" href="{{ post.url }}">
{{ post.excerpt }}
</a>
</a>
</div>
{% endif %}
{% endfor %}
</div>
The idea is that when I set 0 in the visible field, the post won't be visible in the home. Unfortanely this is not working, do you have any hints? Thanks
This works for me:
---
layout: post
title: About Lumen
published: false
---
See [About]({{ site.baseurl }}/about)
If you want to exclude a post/page from pagination you can add hidden: true to the YAML frontmatter. https://github.com/jekyll/jekyll-paginate/issues/6
Try to change your front-matter from visible:1 to visible: 1.
I just tried to reproduce your example on my machine, and I found that Jekyll seems to picky about the blanks in the front-matter.
With visible: 1, your example works for me.
With visible:1, Jekyll outputs the following error message while building the site:
YAML Exception reading C:/foo/bar.md: (): could not find expected ':' while scanning a simple key at line 5 column 1
...but it still finishes building and the generated site works, except that the post is not visible.
You need to modify the _layout/home.html file (In your case, it might be the index.html file).
Try to use an if-endif statement,like this:
{%- for post in site.posts -%}
{% if post.hide == null or post.hide == false %}
<li>
{%- assign date_format = site.minima.date_format | default: "%b %-d, %Y" -%}
<span class="post-meta">{{ post.date | date: date_format }}</span>
<h3>
<a class="post-link" href="{{ post.url | relative_url }}">
{{ post.title | escape }}
</a>
</h3>
</li>
{% endif %}
{%- endfor -%}
Then, hiding a post by hide: true. For example:
published: true
title: Some title
layout: post
hide: true
I want to make an archive page with the example generator from the Jekyll documentation. The generator works fine but I don't know to implement the layout correctly. I am using the following file so far:
{% assign cat = page.category %}
<div class="category-archive">
<div>
<span class="title">Category archive for {{ cat }}</span>
</div>
<div>
{{ cat }}
<ul class="posts">
{% for post in site.categories.cat %}
<li><span>{{ post.date | date_to_string }} - </span> {{ post.title }}</li>
{% endfor %}
</ul>
</div>
</div>
How can I use the current category from page.category like with a variable I am trying to use here?
TL;DR
I want to use a liquid variable at site.categories.*
The correct syntax for the loop is
{% for post in site.categories[cat] %}
I figured it out myself!
The line
{% for post in site.categories.cat %}
can be written like:
{% for post in site.categories.[page.category] %}
It didn't know about the use of these brackets!
I'm looping through two products - on the post view page I pull in a secondary post (in the example, a related recipe) which parses just fine on the first product page - on the second product page just {{ post.content }} won't parse. I can hack it with {{ post.content | markdownify }} - but I'd like to know why it's breaking. Here's the relevant code:
{% for post in site.categories.recipe %}
{% if post.products contains page.title and post.featured %}
<div class="row">
<div class="four columns">
<h4>{{ post.title }}</h4>
<ul>
<li>Serves {{ post.serves }}</li>
<li>Prep: {{ post.time }}</li>
<li>Share</li>
</ul>
{{ post.content }}
...
<!-- All tags are closed, the rest just isn't relevant -->
{% endif %}
{% endfor %}
Please find my solution with counter
<pre>
{% assign counter=0 %}
{% for post in site.posts%}
{% if post.category == 'blog' and counter < 2 %}
{% assign counter=counter | plus:1 %}
{{post.content}}
{% endif %}
{% endfor %}
</pre>
The markdownify filter is probably making it work because there might be special characters that aren't encoded in the content you're pulling from. I always forget to make my & into &.
If you're using the default Markdown interpreter Maruku, here's a list of the entities that might be giving you problems and their encoded equivalent. http://maruku.rubyforge.org/entity_test.html and more info on Maruku. http://maruku.rubyforge.org/maruku.html
I have a network of nodes, some of which are related to one another. I'm using Jekyll to power a website, and was hoping to use liquid tags to map those relationships.
So, the way I've been trying to do this is as follows.
A, which is in the category of 1, is related to B
B, which is in the category of 2, is related to A, and C
When I visit the page for A, I'd like to see B listed as being related.
I've defined the YAML front matter as such:
title: A
category: 1
tags:
- B.html
title: B
category: 2
tags:
- A.html
- C.html
My liquid template looks like:
<h2>{{ page.title }} <span class="label important">{{ page.category }}</span></h2>
<p>Last edited: {{ post.date | date_to_string }}</p>
<p><em>{{ content }}</em></p>
<h4>Related</h4>
<ul>
{% for post in site.tag.{{ post.url }} %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
To me, that looks like it should work. In reality, it doesn't.
Suggestions are welcome!
Also, relevant Github pages are here:
https://raw.github.com/salmonhabitat/salmonhabitat.github.com/master/_posts/2011-12-12-bycatch.md
https://raw.github.com/salmonhabitat/salmonhabitat.github.com/master/_posts/2011-12-12-accidental.md
https://github.com/salmonhabitat/salmonhabitat.github.com/blob/master/_layouts/post.html
What I was intending to happen was for 'Accidental injury' to show up under 'Marine bycatch's' Related nodes...
The first problem is that posts are basically "blind" to other posts in jekyll. It's impossible to the url (or title) of one post from inside another post in jekyll, with only the id of the former. Your site.tag.{{ post.url }}, while creative, would not work :) .
First, your front matter needs to be (unfortunately) a bit more complicated to be able to accomplish that:
title: A
category: 1
related_posts:
- title: B
href: /2010/01/01/B.html
- title: C
href: /2011/11/11/C.html
Notice that I've changed the name from "tags" to "related_posts". I feel it's more clear this way.
Now you can try this code:
<h2>{{ post.title }} <span class="label important">{{ post.category }}</span></h2>
<p>Last edited: {{ post.date | date_to_string }}</p>
<p><em>{{ content }}</em></p>
{% if post.related_posts %}
<h4>Related</h4>
<ul>
{% for related_post in post.related_posts %}
<li>{{ related_post.title }}</li>
{% endfor %}
</ul>
{% endif %}
While this is a bit more verbose than your version, it has an advantage - you can specify your own title in related posts, without being forced to use B or C.
Let me know if you have problems with this, and good luck!