Using jinja inside jinja - html

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.

Related

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

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?

Ignore unknown functions in Jinja2

When rendering a Jinja2 template, either something like:
"hello {{world}}"
"hello {{get_world()}}"
I am interested in simply "passing along" any templated variables or functions that are not provided to Jinja. For example, if the variable {{world}} does not exist in the context, I want the output to keep {{world}}. If the the function {{get_world()}} does not exist in the context, I want to keep {{get_world()}}.
The closest I've gotten is with the following:
from jinja2 import Environment, BaseLoader, DebugUndefined
template = Environment(loader=BaseLoader, undefined=DebugUndefined).from_text("my template")
However, this above only works with the example "hello {{world}}", but not the example "hello {{get_world()}}". How can I achieve this functionality for both of these cases?

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.

How to insert the current date with jinja2

I am new to use Jinja2 and try to insert the current date in a document as a bottom line to tell users when the document was produced.
My current solution is
Produced on {{ utils.today|date('%x') }}
There are no error messages, but nothing is produced.
The solution need to be Jinja2 only, as I have no python process running - using Ginger (a Haskell program) to process the template.
Context Processors can be used to inject values into templates before rendering it.
In app.py:
import datetime
#app.context_processor
def inject_today_date():
return {'today_date': datetime.date.today()}
And add this in the html file:
<p>{{today_date}}</p>
Output: 2019-01-07
Jinja2 doesn't have native support for inserting the date. However, you can easily prepare the date in whatever library you use to render the template and pass the value to the rendering function.
Using jinja2 in Python:
import datetime
date = datetime.date.today()
template = Template('Produced on {{ date }}')
template.render(date=date)
If you are using Ginger, you would have the same template, just make sure to create the date in Haskell and render the template with that value.
You can also write or install a jinja2 extension that will make utilities for working with dates available in the template.
For example, after installing jinja2-time [1], your template would look like this:
Produced on {% now 'local' %}
[1]: untested code

Polymer template data binding - how to input 2 arguments into a function?

I successfully use a single argument in polymer to call a function (in Dart) -eg
{{arg1|myfunc}}
I can't get two arguments to work. I have tried several combinations, including the example shown on the polymer expressions page:
{{ {arg1,arg2} | myfunc}
and
String myfunc(String arg1, String arg2){
but my dart project won't even 'compile' (I get a 404 not found) with that.
What is the correct syntax please? Thanks, Steve
Thanks Anthony,
I just tried the obvious! Here is the syntax:
{{ myfunc(arg1,arg2) }}
Steve