How do conditionally check if a file exists in Sphinx autoclass template? - jinja2

I am attempting to conditionally include a graphviz file into my autoclass template. Only some of my objects have these files, which is why I want to see whether the file exists before attempting to include it.
{% if hasdoc('./../lib/program_data.{{ objname }}.dot') %}
.. graphviz:: ../../lib/program_data.{{ objname }}.dot
{% endif %}
I'm getting this error message:
Extension error (sphinx.ext.autosummary):
Handler <function process_generate_options at 0x7f8eb92a3880> for event 'builder-inited' threw an exception (exception: 'hasdoc' is undefined)
hasdoc is a helper function listed in the Sphinx template documentation, but it doesn't seem to be available. Maybe because this is an autoclass template? I also tried os.path.exists(), but got an error saying that os was not defined.
How do I make this work?

Related

Jekyll: Sort items in collections, while looping over collections

I want to loop over all of the collections in my Jekyll site, and within that I want to sort and list all of the pages in that collection.
Based on this stackoverflow answer, I can loop through all the collections and items:
{% for collection in site.collections %}
<h2>Items from {{ collection.label }}</h2>
<ul>
{% assign pages = site[collection.label] %}
{% for item in pages %}
<li>{{ item.title }}</li>
{% endfor %}
</ul>
{% endfor %}
For a specific collection, I can also sort the update field from the frontmatter:
{% assign sorted = site.programming | sort: 'update' %}
But if I try to apply this to the 1st example, it fails:
{% assign pages = site[collection.label] | sort: 'update' %}
This gives a rather generically useless error:
Liquid Exception: Liquid error (line 30): comparison of Array with Array failed in index.md
Error: Liquid error (line 30): comparison of Array with Array failed
Error: Run jekyll build --trace for more information.
I'm guessing that somehow site[collection.label] returns something different than site.programming, but I'm not sure what, or how to solve this.
EDIT: I tried using collection.docs instead of site[collection.label] and got the same error.
The cause of this error turned out not to be the difference between collection.docs and site[collection.label]. Rather, in one of the collections, there was an item where the frontmatter did not contain the update field. As a result, it couldn't sort by this field because not all items had it. Similarly, you get the same error if the types are not comparable in all cases. In this case, they were all supposed to be dates; if one was not a valid date format, it failed.
I still think this is a terribly ambiguous error message.

how to perform mathematical operations in django template

i want to print the value of current item in list(which is a integer) and its successor(not the list item) but the actual integer successor) at the same time..i am using
{% for i in hour %}{{ i }}-{{i+1}}{% endfor %}
but this gives me an error of "Could not parse the remainder: '+1' from 'i+1'"
Try: {{ i }}-{{ i|add:"1" }}
See https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#add
As far as I know there are three approaches:
Change to a different templating language that allows cleaner logic (Mako I believe though my knowledge is out of date)
Install a third party django package that allows you to do math in templates.
Create a template tag that accepts a value (i), does the calculation you want, and returns that value.
#3 is the one I would suggest.

Using jinja inside jinja

Here's what I want to do:
{% for disaster in disasters %}
{{disaster.name}}
When I try to open the HTML page I get:
Error during template rendering
With exception value as:
Exception Value:
Reverse for '{{disaster.link}}' not found. '{{disaster.link}}' is not a valid view function or pattern name.
How can I do the same task with or without jinja logic?
Have you tried removing the curly braces around disaster.link? I've only worked with Jinja once, but that stood out to me.

Why does {{ site.eggs.label }} not return "eggs" for the jekyll collection "eggs"?

config.yml defines collectio eggs
collections:
eggs:
output: true
A folder _eggs has a document with front matter
I can access the collection label so:
{{ site.collections[0].label }}
which returns "eggs" but not so
{{ site.eggs.label }}
which returns nothing as does this:
{{ site.eggs }}
The documentation about collections at https://jekyllrb.com/docs/collections/#liquid-attributes does not make much sense to me: "The collections are also available under site.collections, with the metadata you specified". In an issue at github the authors say, that the collections field was (silently?) dropped (https://github.com/jekyll/jekyll/issues/4392).
I am currently evaluating Jekyll and this causes doubts where it stable, has up to date docs and has other pitfalls ahead.
Do I misunderstand the docs? Why does the above access to collection metadata not work.
The metadata of each collection is available with site.collections, that means, it will return an array of the collections with its metadata.
If one access a collection directly, like site.eggs, there won't be metadata available, but an array of all the collection files, i.e. all the files in the _eggs folder.
example
To display the contents of site.eggs you can iterate over each file, consider having the following file in /_eggs/item.yml
---
title: "Jekyll is awesome"
---
Then you can display it in /index.yml like:
{% for egg in site.eggs %}
{{egg.title}}
{% endfor %}
Output:
Jekyll is awesome

How can I include macros / other templates with a FunctionLoader in Jinja2?

I'm trying to use a sandboxed Jinja2 environment to handle template customizations.
I've tried using both a DictLoader and FunctionLoader, but keep running into similar issues... I'm mostly concerned with FunctionLoader now.
I can't manage to include or import another template (which contains macros). The FuctionLoader's specified "load" function is never called for the referenced templates.
I've tried with no luck:
just expecting an import would hit the loader using basic 'import' and 'include' syntax
passing the loader into the context , seeing if it might pull in that way
passing a dict of templates into the context, also hoping it might pull in
a few more things , all of which I forgot
I'm sure there's got to be a way to support this - can someone point me in the right direction ?
The import syntax must use quoted strings.
Bad:
{% import utils %}
{% import utils.macros as macros %}
{% from utils.macros import macro_1 , macro_2 %}
Good:
{% import "utils" as utils %}
{% import "utils.macros" as macros %}
{% from "utils.macros" import macro_1 , macro_2 %}
The quoted string is passed into the FunctionLoader or used as the key with the DictLoader