Can jekyll use GET parameters? - jekyll

I would like to make a categories page.
{% for post in site.categories[CATEGORY_NAME] %}
<li>{{ post.title }} ({{post.date|date:"%-d %B %Y"}})</li>
{% endfor %}
Is it possible to use a page parameter to fill in CATEGORY_NAME? Then I could have one file category.html which could serve as the index page for multiple categories (i.e. category.html?name=food and category.html?name=animals.
I've found a few plugins that handle this, but it seems like overkill to require a plugin.
https://github.com/zroger/jekyll-categories
http://blog.nitrous.io/2013/08/30/using-jekyll-plugins-on-github-pages.html
Here's the most related forum post I could find.
https://groups.google.com/forum/#!topic/jekyll-rb/y-dq-63Uvy4
If I can't do this without a plug in, is there a good reason?

I think the correct answer is that Jekyll pages must be compiled to html before they are served. This is not possible if the liquid language takes a parameter.

Related

ignore a line in a markdown file with Jekyll

Is there a way to ignore a text line in a markdown document from the Jekyll engine?
On the main README.md, I have a link to my generated pages url ala,
View the [Docs as a Website](https://gitpages.mycompany.com/myrepo/) which links to our enterprise equivalent of github.io pages powered by Jekyll reading the /docs/ folder.
For obvious reasons, I would like to not show this on the pages site as the viewer is already there and it ends up in an endless loop if users were to keep clicking it.
Is there a way to have it show on the code-view readme.md but not on the rendered jekyll version?
Solution :
If you want Jekyll to skip processing lines (or even a single character) into the baked /_site/ output, use the Liquid {% comment %} tag:
{% comment %}
Character or lines for Jekyll to skip.
{% endcomment %}
Example :
Before:
Code w/o {% comment %} + HTML render
After:
Code w/ {% comment %} + HTML render
Explanation :
If a markdown.md page has Jekyll Front Matter at the top, then it will be processed by Jekyll into a markdown.html page (see Jekyll's docs for more info).
Pages processed by Jekyll can contain Liquid code (specifically Jekyll's implementation of Liquid).
Liquid features a {% comment %} tag. And it works for Jekyll.
From Liquid's documentation of the comment tag:
Comment
Allows you to leave un-rendered code inside a Liquid template. Any
text within the opening and closing comment blocks will not be
printed, and any Liquid code within will not be executed.
If Jekyll processes your markdown.md page, it will process all Liquid statements, and will totally omit the {% comment %} tag + {% endcomment %} tag + and everything in between from the output file.
The text wrapped by a {% comment %} tag does not specifically need to contain Liquid for Jekyll to exclude it. Everything inside will be omitted from the output page: e.g. <html> elements, other code, plaintext, etc.
Word of Caution :
Jekyll will still throw an error if you have improper Liquid syntax, even if it is inside a comment tag.
The following results in an error, and Jekyll will not build:
{% comment %}
Character or lines for Jekyll to skip.
{% assign abc
{% endcomment %}
To prevent this, either ensure (1) all the code inside your comment tag is valid Liquid, or (2) prevent Jekyll from evaluating the code by wrapping it inside a {% raw %} tag:
{% comment %}
{% raw %}
Character or lines for Jekyll to skip.
{% assign abc
{% endraw %}
{% endcomment %}
Then everything inside the comment will be successfully excluded from the /_site/ files Jekyll outputs.
For more information, see Liquid's documentation of the raw tag.
Alternatively:
If you just want to link from the site's GitHub repository -> to the site generated by Jekyll + GitHub Pages
Log in
Go to https://github.com/ user-or-org / repository-name
Click the "Edit" button (above "Clone or download" and below "Settings")
Add the URL to your GitHub Pages website in the 2nd text field that prompts "Website for this repository (optional)", and click "Save"
Remove the URL from your README.md

Jinja2 Dependencies During Render

I am trying to build a dependency graph from a render of a template and I am running into a bit of trouble trying to get good information out of jinja.
I want to be able to render a template and get back a list/set of all of the files that were used to render the template. For example:
# template.html
{% extend base.html %}
{% for partial in partials %}
{% include partial %}
{% endfor %}
And have it render and find out what files were used.
# deps.py
base_path = os.path.dirname(os.path.realpath(__file__))
jinja_env = jinja2.Environment(
loader=jinja2.FileSystemLoader(base_path))
template = jinja_env.get_template('template.html')
template.render({
"partials": [
"test1.html",
"test2.html",
],
})
# ???
looking_for = ['base.html', 'test1.html', 'test2.html']
I have checked out the AST tree and meta.find_referenced_templates(ast) but it only works when using a constant string for the include path.
Tried a custom extension looking at the tokens, but that has the same issues where I can see the variable name, but cannot get the values of the variable since it is done during the parsing/compiling phase.
Also tried overriding the {% include %} but wasn't sure how to do this correctly.
By using a custom loader I could get the values, but only if they have not been loaded before since the environment caches the loaded templates. (This solution may work if I disable the caching but then it has significant performance impact on rendering.)
How can I keep track of all the extend/include dependencies that are used for a single template render?

Performance hits from loading Django static tag multiple times

Unless I am doing things wrong, it seems like if you have nested templates (i.e., {% include %} a template within a template), you will sometimes need to call {% load static %} in multiple "layers" of the nest. For example, say I have templateA.html:
{% load static %}
<a href={% static "some/path" %}>Some Link</a>
{% include 'templateB.html' %}
And then in `templateB.html, I have:
{% load static %}
<a href={% static "some/other/path" %}>Some Other Link</a>
As far as I can tell from testing, I must include {% load static %} in both templates, because templateB.html does not know that I have already loaded the {% static %} tag.
My question is this:
Assuming that it is necessary to load the {% static %} tag twice (or more times depending on the amount of nesting), is there going to be a performance hit from this extra loading?
I am not sure what Django does under the hood when you load this tag, but my intuition is that you don't want to be loading and reloading static files. (Since we are talking about an open source project, I did actually try to look under the hood myself at how this templatetag is implemented, but it proved to be a little beyond my comprehension...).
Also, this question assumes that it is necessary to always load the tag this way. If there is something I am missing, I would be very interested to learn more. Thank you!
You have to write the tag in every template. In case of performance, you need not to worry as it never reloads or loads a separate new copy of static files.
There is no overhead. load static does not "load and reload static files"; it just makes available the (already-loaded) code in the staticfiles templatetags library for use in your template.
By using load you adding tags and filters from some app into the context for the current template. It just calls parser.add_library() for parser and updates the list of tags and filters for this particular template. You can check this method, and it gets called from load tag
If you don't want to load something you can add it in the builtins. For Django 1.9 you can configure it like this
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'builtins': ['django.templatetags.static'],
},
},
]
and for older versions
from django.template.loader import add_to_builtins
add_to_builtins('django.templatetags.static')

Aldryn NewsBlog - one particular blog instance multiple times

I am using aldryn-newsblog, and would like to include the top three first blog posts to my home/index page, along with other elements like a gallery slider and a newsletter sign up.
How can I render the first three blog post from the aldryn news-blog into the tpl_home.html template used on my home/index page?
This is the default tpl_home.html template:
{% extends "fullwidth.html" %}
{% block body_class %}tpl-home{% endblock %}
This is the default article_list.html:
{% extends "aldryn_newsblog/two_column.html" %}
{% load i18n cms_tags %}
{% block newsblog_content %}
{% render_placeholder view.config.list_view_placeholder language placeholder_language %}
<div class="aldryn-newsblog-list">
{% for article in article_list %}
{% include "aldryn_newsblog/includes/article.html" %}
{% empty %}
<p class="well">{% trans "No items available" %}</p>
{% endfor %}
</div>
{% endblock %}
{% block newsblog_footer %}
<div class="aldryn-newsblog-pagination">
{% include "aldryn_newsblog/includes/pagination.html" %}
</div>
{% endblock %}
I have basically tried to copy the content of the article_list.html file to the tpl_home.html, as well as changing the aldryn_blog/two_column.html
from {% extends "aldryn_newsblog/base.html" %}
to {% extends "base.html" %}
But all I get is the "No items available" error, from the article_list.html.
I have also tried to add the blog instance to the home page using the django-CMS GUI, but keep getting this error "An application instance using this configuration already exists.".
Is there a way to include one particular blog instance multiple times on different sites?
This is a little old at this point, but for anybody who stumbles across it:
You don't need to touch {% extends <anything> %} -- anytime you use the extends tag, it's going to pull in the entire page that follows it, which will either result in an error, an extremely funky page, or just generally undesired results. Depending on your setup, there are two methods to accomplish what you're trying to do:
1. Using the Aldryn setup.
As far as articles_list.html goes, that's simply one piece of your blog page's puzzle: The one that lists the articles. What you're looking for can be done entirely through the front-end editing, with the "Latest Articles" plugin. Below is the structure laid out by default for my tpl_home.html:
This doesn't need to go into the "Header" section -- you can arrange the order of appearance however you like.
It's a perfect plugin for your needs, as you can specify the number of selected articles to be shown once you've clicked on the plugin, as well as which blog (if you have more than one) you'd like them to come from:
You should see this when you click on the plugin.
As for the gallery slider: I've never used it, but I do have the Aldryn Gallery package installed, and it has the option displayed in the link below, so I believe that's the route you'd take to get that on your page. It can be installed through the "Manage Addons" page on your site's dashboard.
For a newsletter, Aldryn Mailchimp can also be installed via the "Manage Addons" page, but you'll first need to sign up for a free Mailchimp account on their site (linked to in the package details) to get an API key (this must be entered on the package's installation page before the installation will initiate). I've never personally used Mailchimp, but most newsletter services require you to give them $$$. Mailchimp kinda rocks, because it offers a free usage level that should be quite capable of handling your site's needs (12,000 emails a month are allowed). It also offers a ton of features for managing your campaigns/formats/other stuff (I haven't really looked into it). It's email submission form should be available as a plugin, just like "Recent Articles" and "Gallery," once installed.
Now onto...
2. Not Using Aldryn.
However you're using Aldryn-NewsBlog, the normal plugins should still be available, and it can be downloaded on github. How to use them, exactly, depends on your admin setup -- but you should still be able to use the "Latest Articles" plugin, with the same methods, if you have the package properly installed.
Same for your gallery slider: Download and install Aldryn-Gallery from GitHub, and you should be good to go with the slider option.
With Mailchimp, you can just go to their site, and they'll explain how to integrate their service into virtually anything from there. I don't have enough repo points to post more than two links, but evidently iframes in snippits don't count as links (so take that, stackexchange rules!) Run the snippit for a Mailchimp newsletter install video:
<iframe src="//fast.wistia.net/embed/iframe/5ou4sscmze" allowtransparency="true" frameborder="0" scrolling="no" class="wistia_embed" name="wistia_embed" allowfullscreen="allowfullscreen" mozallowfullscreen="mozallowfullscreen" webkitallowfullscreen="webkitallowfullscreen"
oallowfullscreen="oallowfullscreen" msallowfullscreen="msallowfullscreen" width="600" height="400"></iframe>

Dynamic Links in jekyll

currently I'm working on static website, so I'm using jekyll to generate it. To have a nice structure and fancy URLs, I use permalinks.
permalink: /impressum/
So for example the impressum.html is rendered to impressum/index.html. And in my HTML i can simply link to that file with
<a href="/impressum">
That works for me very well. But you know, I'm a programmer. What if I want for example to change the URL to /imprint/? Well, I can change the permalink without any problems. But what's about all the other links on the site? Yeah, sure, I can use the search & replace function of my editor to change the linked URLs and check the whole site with a site checker for broken links, but that's not the fancy way I want to go. That's why I tried to create some kind of global variables with the permalink.
_config.yml:
lnk_impressum: /impressum/
impressum.html
---
layout: layout
title: Your New Jekyll Site
permalink: {{ site.lnk_impressum }}
---
But that does not work. I get this error:
Generating... error: no implicit conversion of Hash into String. Use --trace to view backtrace
So what's wrong or is there a better way?
It doesn't seem to be possible to place Liquid tags inside the YAML Frontmatter or _config files, per this SO answer.
Something else you could try is based on the approach used by the documentation pages for Bootstrap, which uses a Page Variable they call slug that provides a unique, unchanging reference to each page.
For instance, if you'd like to place a link to your impressum.html page (for which the permalink could change), you can place this code on another page, such as index.html:
{% for mypage in site.pages %}
{% if mypage.slug == 'impressum' %}
Link to Impressum page
{% endif %}
{% endfor %}
Then, in the YAML frontmatter for each of your pages, place code similar to the following:
---
slug: impressum
permalink: /my-permalink-to-impressum/
---
To change your permalinks in the future, you would just make the change to the Page Variable permalink in each page. The URLs referenced in your other pages would be automatically updated by Jekyll, as long as the slug variable remains unchanged.