It is easy to check which JEKYLL_ENV is used in templates, but I didn't found a way to get its value in my *.js values, which I think should be very useful.
Any ideas on how to achieve this without using inlined tags?
I didn't found a way to access this variable in JS files, so the best way to still use it was utilising window object.
So I am setting variable in my template with <script> tag like that:
{% if jekyll.environment == "test" %}
<script>
window.testMode = true;
</script>
{% endif %}
and then later checking for window.testMode in JS.
Related
Currently, I use the following code to include another HTML file:
{% include "machines/opennebula/resources.html" %}
I am adding another folder to add another version of resources.html (specifically to support OpenStack when I want to swap to using that cloud platform):
machines/openstack/resources.html
I want to have the path change based on whichever is set in a config file (which I incorporate into other parts of the file I'm working on using):
{{ cloudPlatform }}
I tried:
{% include "machines/{{ cloudPlatform }}/resources.html" %}
This worked when using it in association with the script tag however it hasn't worked with Django's include statement. My question is how do I make something equivalent (that works in HTML) with Django?
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?
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')
I would like to render different html for desktop and mobile on jekyll. I need something like:
{% if is_mobile %}
<!-- mobile html -->
{% else %}
<!-- desktop html -->
(% endif %}
Obviously I could just use javascript in the browser to determine this but I don't want to render the extra html for mobiles.
Maybe I could create a jekyll plugin and register the liquid tag but how can I get access to the user agent? Is there anyway I can get access to HTTP request parameters?
In your layout you could use:
<div class="mobile">{% include mobile.html %}</div>
<div class="desktop">{% include desktop.html %}</div>
Then you should let js decide which one to show, as liquid has no access to the user agent.
Since you're using Jekyll I'm guessing you're not working with a backend which is typically how you'd process the User-Agent.
Perhaps your best approach is using accessing navigator.userAgent variable using plain-old Javascript.
More info here: Getting the User Agent with JavaScript
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.