I would like to know if it's possible to add if statement in Front Matter to be selective about using attributes depending on page.url
The pages are generated from .md file virtually from a plugin using a base template so I need to add some conditions in Front Matter if possible.
This is a typical Front Matter
---
...
title: blah
description: blah blah
image: some-image.png
---
I want to do the following:
---
...
title: blah
description: blah blah
{% if page.url == "page1" %}
image: page1-image.png
{% else %}
image: general-image.png
{% endif %}
---
Jekyll does not parse Liquid in Front Matter out of the box but there are plugins out there that can parse Liquid in Front Matter for you.
That said, I think your use-case can be easily solved within the page's "layout" itself..
{% capture image %}
{% if page.url == "page1" %}
page1-image.png
{% else %}
general-image.png
{% endif %}
{% endcapture %}
{{ image | strip }}
Related
I've got a jekyll site with two pages (page1.html and page2.html), they both use the same layout. This layout print some information about some other pages in a given subdirectory. For example
/_layouts/test.html
{% for p in site.pages %}
{{ p.title }}
{% endfor %}
/page1.html
---
layout: test
---
this page lists the title of my books...
This would print the title of every page in my site, but I want it to print only the title of pages in the subdirectory /books, so I would change the layout page to
{% for p in site.pages | where: 'dir','/books/' %}
{{ p.title }}
{% endfor %}
This works fine, but I would like another page to use the same layout and list the content of my comics (that are inside /comics folder) instead of my books, so I would change the structure of my site in the following way:
/_layouts/test.html
{% for p in site.pages | where: 'dir','/{{ page.directory_to_scan }}/' %}
{{ p.title }}
{% endfor %}
/page1.html
---
layout: test
directory_to_scan: books
---
this page lists the title of my books...
/page2.html
---
layout: test
directory_to_scan: comics
---
this page lists the title of my comics...
However this does not work and no title at all is printed.
You can't mix tags and filters (except for assign). Moreover, there's no need to enclose variables to a filter within double braces:
---
directory_to_scan: '/comics/'
---
And the layout would use:
{% assign my_pages = site.pages | where: 'dir', page.directory_to_scan %}
{% for p in my_pages %}
{{ p.title }}
{% endfor %}
So, I've eventually solved by assembling the string before the loop, using the append filter.
{% assign d = '/' | append: page.directory_to_scan | append: '/' %}
{% for p in site.pages | where: 'dir',d %}
{{ p.title }}
{% endfor %}
I have a Jekyll collection which is not being output, but elements of which are being displayed on a single page, like this:
{% for element in site.collection %}
{{ element.content }}
{% endfor %}
I would like to be able to do something like this:
{% for element in site.collection %}
{{ element.content }}
{% for front_matter in element %}
<!-- do stuff -->
{% endfor %}
{% endfor %}
This is possible with YAML hashes in YAML _data files, but doesn't work here because, it seems {{ element }} on its own is identical to {{ element.content }}. There doesn't seem to be any variable designated for this either like, {{ element.front_matter }}. Is it possible to loop through the front matter of an element in a jekyll collection?
I know that the ideal way to do this would be to include all the front_matter I want to loop through in a variable, like:
---
front_matter:
foo: bar
bar: foo
---
But, as I'm trying to configure these pairings (foo and bar) to be easily updatable through prose.io, they can't be nested under other values. If there is a
way around this with prose though, I would accept that answer.
Much appreciated!
It is possible to loop through the variables of an element in a Jekyll collection:
{% for items in site.my_collection %}
{% for item in items %}
{{ item }}: {{ items[item] }}
{% endfor %}
{% endfor %}
But it is important to remember that there is other metadata that is also available and will be included in the iteration, like path, url, etc and your front matter, e.g. for _my_collection/something.md:
next:
path: _my_collection/something.md
output: <p></p>
url: /my_collection/something.html
id: /my_collection/something
content: <p></p>
collection: my_collection
relative_path: _my_collection/something.md
excerpt: <p></p>
previous:
draft: false
categories:
slug: something
ext: .md
tags:
date: 2017-05-23 14:43:57 -0300
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 %}
There's any way to check if the current page is the homepage?
I want use h1 tag for the logo image only when the current page is the website base url.
You can use page.url to check if the current page is your index page:
{% if page.url == "/index.html" %}
<h1>...</h1>
{% endif %}
Another option to manage this is adding page IDs to the yml frontmatter
{% if page.id == 'index' %}
content
{% endif %}
Along #Christopher's comment the best is if you test the page.layout. Because if permalink is set or in other circumstances the page.url can be "/index.html", "/index", or just simply "/". Therefore I think it's more robust:
{% if page.layout == 'home' %}
<h1>logo</h1>
{% else %}
<h2>smaller logo</h2>
{% endif %}
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.