How can I pass a variable to a jekyll layout? - jekyll

I don't understand how to pass a variable to a Jekyll layout. I have a markdown file with this header:
---
layout: post
next_tutorial: next_tutorial_name
---
And then, in the post layout, I have this:
---
layout: default
---
<article id="postBody">
<h1>{{ page.title }}</h1>
<p>{{ page.date | date_to_string }}</p>
{{ content }}
{% if site.next_tutorial %}
<h2>{{ site.next_tutorial }}</h2>
{% endif %}
</article>
But the h2 element never appears. I tried removing the if, but the result is the same. What I'm doing wrong?

What you are calling the "header of a markdown file" actually have a name in Jekyll, and this is called the Front Matter.
And the elements of the front matter can be accessed via the page variable, as pointed here and there.
So, your layout should read:
---
layout: default
---
<article id="postBody">
<h1>{{ page.title }}</h1>
<p>{{ page.date | date_to_string }}</p>
{{ content }}
{% if page.next_tutorial %}
<h2>{{ page.next_tutorial }}</h2>
{% endif %}
</article>

Related

Please give me a suitable Jekyll index.md?

This is the source code of the index I am currently using, but it will display the date in the title bar. I don’t know how to modify it. It can only be displayed: title.html
<ul class="posts">
{% for post in site.posts %}
<li>
    {% for post in site.posts %}
      {{ post.date | date_to_string }} {{ post.title }}
    {% endfor %}
</ul>
I think this is nothing for the index.md file, but for the layout.
The HTML-Title is defined by the title-tag inside the head
<head>
<title>{{ site.title }} - {{ page.title }}</title>
</head>
So you have to look check the layout definition.

how to manually process Liquid tags in Jekyll

I am creating a Jekyll theme where all user pages that implement the 'indexable' attribute in the front matter are rendered in the main landing page. So I have the 'frontpage layout:
---
layout: root
---
{% assign custom_pages = site.pages | where: 'indexable', true | sort: 'priority' | reverse %}
{% include header.html %}
{% for c_page in custom_pages %}
<div class="container {{ c_page.class | default: '' }}" >
{{ c_page.content }}
</div>
{% endfor %}
{% include footer.html %}
{% include javascripts.html %}
A sample page that will be processed:
---
layout: page
title: Us
permalink: /us/
indexable: true
priority: 10
class: us-page
---
<div class="row">
{% for member in site.data.members %}
<div class="col-sm-6">
<div class="card card-block">
<img src="{{ member.gravatar }}?s=256" alt="Avatar">
<h4 class="card-title text-xs-center">{{ member.name }}</h4>
<p class="card-text">{{ member.description | markdownify }}</p>
<p class="card-text">
{% for tag in member.expertise_areas %}
<span>{{ tag }}</span>
{% endfor %}
</p>
<a href="{{ member.blog }}" class="btn btn-primary" role="button" >Mi blog</a>
</div>
</div>
{% endfor %}
</div>
However the liquid tags are appearing unprocessed, like the same output {% raw %} would produce. Is there a way through I could do {{ c_page.content | magic_here }} in order to manually get rendered those tags?
EDIT. Screenshot:
EDIT2
Theme repository
Web implementation
Well, despite I still don't know whether the issue is in my code, I am posting how I managed to solve it. Basically I created a filter tag called liquefy which has been put in a .gem and whose main task is taking a text with markdown or/and liquid syntax as an argument which will be parsed and rendered.
Repo: https://github.com/sonirico/liquefy
Gem: https://rubygems.org/gems/liquefy

Jekyll - Change _config.yml variables in the post layout

In my config.yml I have:
...
showheader: "yes"
And in my default.html template I have a conditional to show the header include:
<!DOCTYPE html>
<html>
{% include head.html %}
<body>
{% if site.showheader == "yes" %}{% include header.html %}{% endif %}
<main class="page-main">
{{ content }}
</main>
{% include footer.html %}
</body>
</html>
In my post.html template I don't want to show the header.html so I try this:
---
layout: default
---
{{ site.showheader = "no" }}
<article class="post">
<header class="post-header">
<h1 class="post-h1" href="{{ post.url | prepend: site.baseurl }}">{{ page.title }}</h1>
<p class="post-meta">{{ page.date | date: "%b %-d, %Y" }}{% if page.author %} • {{ page.author }}{% endif %}{% if page.meta %} • {{ page.meta }}{% endif %}</p>
</header>
<p class="post-p">{{ content }}</p>
<footer class="post-footer">
<p class="rss-subscribe">subscribe via RSS</p>
</footer>
</article>
But it's simple don't work,and neither returned a error.
It is possible in Jekyll to change the value of a variable set in _config.yml from the post.html template layout?
It seems that you cannot reassign a site.variable value. {% assign site.showheader = false %} doesn't work if showheader: true is set in _config.yml.
If you want to show header depending on the page you're on, just set the variable in page front matter.
In index.html :
---
showheader: true
layout: default
---
In post :
---
showheader: false
layout: default
---
In _layouts/default.html
{% if page.showheader == true %}
header
{% endif %}

How can I omit content from Octopress/Jekyll's extracted excerpt?

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.

Jekyll Loop breaks on second iteration

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