Jekyll - Using relative URLs for post.url - html

I've recently upgraded to jekyll 1.0 and as a result post links now have a leading '/'.
Setting relative permalinks to true or false doesn't seem to change the generation of {{post.url}} at all, they always seem to come out with a leading slash.
I understand that I could use base_url, but I pass on the completed project to an organisation that ends up hosting it wherever (I don't know the URLs).
My config file that used to work was simply:
permalink: articles/:title
Any help would be great!

I'm seeing the same thing in Jekyll 1.0.3 install. Seems like a bug. Either way, a work around is to use a Liquid Filter to remove the first slash.
{{ post.url | remove_first:'/'}}
With the following pagination layout:
{% for post in paginator.posts %}
<div class="postWrapper">
<h2>{{ post.title }}</h2>
<div class="postDate">{{ post.date | date:"%B %d, %Y" }}</div>
<div class="postContent">{{ post.content }}</div>
</div>
{% endfor %}
And your same _config.yml setting:
permalink: article/:title
Links are generated without the leading slash (e.g. The Title).
Just be aware that if it is a bug and it gets fixed, you'll have to adjust your code to drop the 'remove_first' filter. Otherwise, it'll strip the slash in the middle of your link and break it that way.

We noticed the same thing, and I tracked it down to the addition of baseUrl being exposed to liquid templates. In 0.12.1 baseUrl was not configurable in _config.yml and was defaulted to ''.
In 1.0.0 you could set it in the config and it defaults to '/' which is why you're seeing this. I don't believe it is a bug as it is still present in current (1.4.3) versions.

Related

How to use Liquid include variable within a for loop?

I have this Liquid template which looks like this:
# /_includes/slideshow.html
{% for image in {{ include.images }} %}
...
{% endfor %}
which I'm trying to use with a YAML file (for my Jekyll site) like this:
# /index.md
{% include slideshow.html images='site.data.homepage_images' %}
The reason I see this failing is because my include variable {{ include.images }} resolves to a string within the for loop. Is there a different way to accomplish this? I'm still rather new to Liquid, YAML, Jekyll, and well, web development altogether, so any help in doing this is much appreciated!
(Note: the problem goes away if I replace {{ include.images }} with site.data.homepage_images.)
Additionally, the reason why I'm doing this (and why that crude fix isn't the solution I'm looking for) is for the ability to inject my image slideshow elsewhere around my site. It'd save a lot of code to abuse my include variable in this way.
Correct syntax in for loop is : {% for image in include.images %}

Jekyll Post Excerpt: How to know if there was an auto generated excerpt?

If I understand correctly Jekyll takes the first paragraph as an excerpt unless you use one of the various methods mark or specify one manually.
In my case, I want to be able to distinguish in the templates whether there was no excerpt or not so I can effectively do this
{% if post.excerpt %}
{{ post.excerpt }}
{% else %}
{{ post.content }}
{% endif %}
Effectively if there was no excerpt use the entire post. As it is, since Jekyll auto generates excerpts the test will always fail.
I suppose one solution so to go to every post that has no excerpt and add <!-- more --> at the very bottom of the post but that's very error prone as in if I forget I'll get the wrong result. I'd prefer to make the default be if I didn't manually mark an excerpt then the entire post appears on the home page.
To put it another way I'm trying to port from Wordpress to Jekyll. Wordpress's behavior is that no excerpt = insert entire post.
Is that possible in Jekyll? Is there some flag or variable I can check in the templates on whether or not an excerpt was manually specified vs auto generated?
There is an alternative solution with Liquid. You need to check, if the excerpt separator is present in the post:
{% if post.content contains site.excerpt_separator %}
{{ post.excerpt }}
<p>Read more</p>
{% else %}
{{ post.content }}
{% endif %}
I don't know any method to tell if an excerpt is manual or generated. Maybe writing a plugin to analyze the raw file's front-matter can be an option (but that would not work on Github Pages for example).
But I may have a solution for this:
I'd prefer to make the default be if I didn't manually mark an excerpt then the entire post appears on the home page.
According to the documentation, you can set excerpt_separator for every page (you can also set it at once in defaults).
Try setting a value which you know will never appear in your posts. If Jekyll doesn't find the separator, it won't separate, so the generated excerpt will be the entire post.
Example:
---
title: Some title
excerpt_separator: "CANTFINDME!"
---
Post line 1
Post line 2
The generated excerpt will be the entire post:
<p>Post line 1</p>
<p>Post line 2</p>

Removing pages from build and nav?

I need to be able to exclude certain files from the build. I am aware I can do this in the config file.
I also need a way to turn off a section of the website in the nav.
So I thought about having a flag in a data file, if it's false do not include a link to the section in the nav.
But how can I also use the same flag to prevent the section from being built?
Or is it easier to specify in config and check this flag in the nav?
To exclude files from builds, add this line to your _config.yml:
keep_files: [folder, "file.ext"]
The folder and the file.ext will be left untouched by Jekyll and will be included on builds.
OR
exclude: ["file.md", "otherfile.html"]
Both files won't be included on you site built by Jekyll at all.
Here:
I also need a way to turn off a section of the website in the nav.
I'm not sure what you meant, but I guess you can probably do that with if statements:
In a post or a page front matter, add a variable indicating the section to exclude:
---
# your front matter settings
foo: bar # variable and value
---
Then, to your template, add:
{% if page.foo %}
<div>this will display</div>
{% endif %}
or
{% if page.foo %}
<div>this will display</div>
{% else %}
<p>this will display when the above doesn't</p>
{% endif %}
Hope to have helped! :)
Seems like a config problem to me. I would put it in the config and have your includes and nav check the config values.

Using Liquid tags in a Jekyll page, not a layout

I want to use liquid tags in a page on a Jekyll site. I have used them successfully in layout files, but when I use them in a page they are not parsed by Liquid.
The page is in html format not Markdown. The page has valid YAML front-matter that is being successfully used by the layout file. Here's the code for the page that isn't parsing:
---
layout: default
title: Media
id: media
order: 2
---
<section id="photos">
<h2>Photographs</h2>
<div id="galleries">
{% for set in site.flickr-sets %}
<div class="gallery" data-set="{{ set }}"></div>
{% endfor %}
</div>
</section>
Is there any obvious reason why this isn't working? I really need to be able to access the site global variable...
EDIT
It seems this issue isn't confined to just that page. I tried creating a new page and using some liquid syntax and got the same result. It's also any liquid syntax not just tags.
In the layout file that these pages use I include the content of the page using {{ page.content }} rather than just {{ content }}. Could that be relevant?
{{ content }} works and it's different than {{ page.content }}
{{ content }} it's parsing all liquid syntax :)
Hope that helps.
So it seems that the answer is that as I suspected. I tested the same code using a new layout file that just called {{ content }} and it rendered correctly. I'm assuming this means that when Jekyll builds it stores raw content in the page object. This is why pages with only html (or Markdown) were being rendered correctly, but any Liquid syntax was not being parsed.
Although this technically answers the question, I still haven't figured out how to solve my problem! It would be useful if there was some sort of filter I could add to {{ page.content }} to make it parse the Liquid syntax.
I know this may be a little late, but I dug up something called {{ page.output }} which is the rendered content of the page.

Jekyll not generating pages in subfolders

I use GitHub Pages and created some pages in a sub folder. It seems to be not generating pages I created in sub folder. All other pages work fine. The directory structure is like this:
/
/index.html
/_config.yaml
/_includes
/_layouts
/_posts
/tag
/tag/personal.html
/tag/videos.html
The pages inside the /tag directory are not generated by Jekyll. Also, usually GitHub sends an email if Jekyll build fails, but did not, in this case. Also, if I do any other changes it works, so the build is apparently not failing.
The /tag/personal.html is here:
---
layout: default
title: Tag-personal
permalink: /tag/personal/index.html
tagspec: personal
---
<div id="tagpage">
<h1>Posts tagged personal</h1>
{% include tags.html %}
</div>
and /_includes/tags.html is here:
{% for tag in post.tags %}
{% if tag == page.tagspec %}
{% assign ispostviable = true %}
{% endif %}
{% endfor %}
<ul class="posts">
{% for post in site.posts %}
{% if ispostviable == true %}
<li><a href="{{ post.url }}"></li>
{% endif %}
{% endfor %}
</ul>
PS: I use GitHub Pages and have no access to a Jekyll instance at my development machine (Windows).
Joshua Powell provided step-by-step directions in reply to a similar question on Github.
Edit _config.yml to add the following line (or expand the array, if it exists)
include: ['_pages']
where _pages is the name of the folder in which you wish to keep your files. (This also works for nested folders if you explicitly add them, e.g., ['_pages', '_pages/foo'].)
Move your pages into that folder. (These pages may be HTML, Markdown, or whatever else Jekyll renders when it’s placed in the root folder.)
Give them front matter with an appropiate permalink including a trailing slash, e.g., permalink: "/about/".
I found the culprit. It was that In Jekyll v1.0, absolute permalinks for pages in subdirectories were introduced. Until v1.1, it is opt-in. Starting with v1.1, however, absolute permalinks became opt-out, meaning Jekyll defaults to using absolute permalinks instead of relative permalinks.
The pages were being generated at /tag/tag/personal.html and so on.
There were two solutions:
Specify relative_permalinks: false in _config.yaml
Make permalinks relative to the subdirectory.
I chose the first option.