I have a simple Jekyll website and I want to have collection offers in my custom directory _i18n/pl/_offers/ so it will look like this:
_i18n/pl/_offers/2022-04-11-my-super-offer.md
_i18n/pl/_offers/2022-04-11-another-offer.md
_i18n/pl/_offers/2022-04-11-best-offfer-ever.md
When i create collection like this:
collections:
offers:
directory: _i18n/pl
output: true
This loop doesn't work:
{% for offer in site.offers %}
<p>{{ offer.title }}</p>
{% endfor %}
collections_dir: _i18n/pl works, but in the future there will be more collections and i don't want to have them all in _i18n/pl so each collection should have defined different directory.
Related
I'm currently trying to loop over a subfolder in Jekyll but I don't know whether this is possible.
My folder structure looks like this:
_includes
_layouts
_pages
folder_1
folder_2
index.html
For example, I want to reach folder_1 by a loop how can I do this?
{% for page in pages.folder_1 %}
//xyz
{% endfor %}
What I want is that I've a static page and on this page I want to display the title and the description of each page in one of the subfolders.
Could you please help me?
Avoid naming main folder with an underscore unless you are using the _posts folder.
Then filter posts or pages checking their path:
{% assign folder1 = site.pages | where_exp: "item" , "item.path contains 'folder1'"%}
{% for item in folder1 %}
{{item.title}}
{% endfor %}
I am using Prose.io as my CMS for github. In which I have set the root directory to /_posts. In /_posts I have made a folder "staticpages", this contains some markdown files with text. Can I loop through these files? I can't seem to figure out how.
So my file tree looks like:
root
|
_posts/
|
staticpages/
|
myfile.md
And I want to:
{% for pages in posts.staticpages %} {{ page.title }} {% endfor %}
Here you go:
In your _config.yml you specify
defaults:
- scope:
path: "_posts/staticpages"
values:
static: "true"
and in your layout file (or page) you filter the posts and loop with:
{% assign posts = site.posts | where:"static", "true" %}
{% for post in posts %}...
This works very well for me...
You can browse to a folder by setting it in the URL.
If the default URL is:
http://prose.io/#<account>/<repository>/
Then you can add the branch and folder relative path in the URL:
http://prose.io/#<account>/<repository>/tree/master/_posts/staticpages
In a Jekyll powered page, I have a set of files located in:
_includes/stuff/
I put those files there so that I can include them in other Markdown pages using:
{% include stuff/example.txt %}
This works as expected.
However, I also want to copy those files to the generated page so that I can link to them and that people can follow those links to download them. But by definition, stuff stored in directories starting with an underscore are not copied by Jekyll.
Another approach also didn't work. I put the files in an own top folder called stuff. This copies the folder to the final site. However, I'm not able to include a file from this folder. It seems include_relative only allows including files below the current one. For example, the following don't work:
{% include_relative stuff/example.txt %}
{% include_relative /stuff/example.txt %}
{% include_relative ../stuff/example.txt %}
Any ideas how I can achieve including and copying at the same time?
This works from index.html
{% include_relative example.txt %} for example.txt
{% include_relative stuff/example.txt %} for stuff/example.txt
{% include_relative /stuff/example.txt %} for stuff/example.txt
stuff/example.txt
class Toto
def dototo
myvar = "toto"
end
end
index.html
{% assign codeurl = "stuff/example.txt" %}
{% highlight ruby %}
{% include_relative {{codeurl}} %}
{% endhighlight %}
link to code
if codeurl == "/stuff/example.txt" this generates a link relative to site root
this may need {{site.baseurl}} prepended if your site is not at the root
of a domain (eg: user.github.io/repository)
link to code
For security reasons, this will not work :
{% include_relative ../stuff/example.txt %}
Just to avoid directory traversal
{% include_relative ../../../../../../../../../../../../etc/pwd %}
If you want to put you files in _includes/stuff you will need to do an include: [ /_includes ] in _config.yml, that will include all files in _includes as static files. Not very clean as you cannot filter subdiretories like include: [ /_includes/stuff ] to import only your stuff files.
Note : a dirty trick allows you to import only _includes/stuff/*.txt but I think it's really dirty.
# _config.yml
include:
- "_includes"
- "stuff"
- "*.txt"
exclude:
- "_includes/*.*"
I have the following header on a jekyll post with a single author "usman" which generates the this article. I would like to have something like "authors: usman, someone_else" so a colleague can also contribute to the article. Is this possible? How would I set it up.
---
layout: post
title: The pitfalls of building services using Google App Engine -- Part I
date: 2013-02-24 08:18:17
author: usman
categories:
- System Admin
tags:
- GAE
---
I took a look at the post template for the theme I am using and it has the following line:
{% assign author = site.authors[page.author] %}
This will obviously only support one author, Is there a way to get both authors of the page? i.e. page.author[0] for example?
If you want to specify multiple authors in your YAML Frontmatter, then you are going to want to use YAML's list syntax like you did with categories and tags, like this:
author:
- usman
- someone_else
This will be useful for dynamically injecting author information into each of the posts.
As for allowing multiple people to contribute to the same article, I don't think this has anything to do with Jekyll or what is specified in the Frontmatter. This is an issue of having your Jekyll content hosted in a shared place (such as on GitHub, like many do) where both you and your collaborator can both work on the file. That being said, be aware that you may run into nasty merge conflicts if you work on that same markdown file in parallel.
Update
This is an update based on the OP's edits to the original question.
A simple hacked approach would be to set your author tag like this:
author: Usman and Someone_Else
This doesn't give you much flexibility though. A better solution, which would require you to modify the template you are using, would be to do something like the following:
First, setup your YAML Front Matter so that it can support multiple authors.
authors:
- Usman
- Someone_else
Now, you modify the template to go through the authors specified in the YAML Front Matter.
<p>
{% assign authorCount = page.authors | size %}
{% if authorCount == 0 %}
No author
{% elsif authorCount == 1 %}
{{ page.authors | first }}
{% else %}
{% for author in page.authors %}
{% if forloop.first %}
{{ author }}
{% elsif forloop.last %}
and {{ author }}
{% else %}
, {{ author }}
{% endif %}
{% endfor %}
{% endif %}
</p>
Resulting HTML:
If no authors are specified:
<p>No author</p>
If one author is specified:
<p>Usman</p>
If two authors are specified:
<p>Usman and Someone_Else</p>
If more than two authors are specified:
<p>Usman, Bob, and Someone_Else</p>
The Jekyll Bootstrap project has a sample blog post in the directory _posts/core-samples/ .
I assume, posts (files) in sub directories are handled the same way as posts in the root directory. Is this correct?
If so, I will add a "stage" sub directory, exclude it, so I can park posts and publish them by moving them.
Accidentally found it in the post - yaml section:
Instead of placing posts inside of folders, you can specify one or
more categories that the post belongs to. When the site is generated
the post will act as though it had been set with these categories
normally. Categories (plural key) can be specified as a YAML list or a
space-separated string.
So sub directories == categories
As the different is only on post.path so I would agree to your statement:
posts(files) in sub directores are handled the same way as posts in
the root diretory
You can park your posts in the directory _posts/core-samples/ and publish them like this:
{% for post in site.posts %}
{% if post.path contains 'core-samples' %}
..your code
{% endif %}
{% endfor %}
As a working sample you may see how this code publish these parked posts in their section.
I ended up here because I wanted to create the following structure:
index.html
_animals
cats
my-cat.html
...
dogs
my-dog.html
...
I created that structure, then in _config.yml:
collections:
animals:
output: true
permalink: /animal/:title.html
Finally, to get just the dogs in index.html:
<div id='dogs'>
{% for a in site.animals %}
{% if a.path contains 'dogs' %}
<a href='{{ a.url }}'>{{ a.title }}</a>
{% endif %}
{% endfor %}
</div>
NB: that this approach requires that the directory containing all the records (_animals in my example) can't be named _posts, as the latter is a special name in Jekyll.
Actually what that statement says is to put _posts folder inside a sub directory.
And then that sub directory will be treated as category.