How can I include macros / other templates with a FunctionLoader in Jinja2? - 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

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?

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 access `site.data.header["foo.bar.baz"]` if `foo.bar.baz` is a FrontMatter variable

I want something like:
---
my_var: "foo.bar.baz"
---
{{- site.data.header[page.my_var] -}}
Unfortunately, it doesn't work…
I know that I can use site.data.header.foo.bar.bazor site.data.header["foo"]["bar"]["baz"], but it's not that.
Also I know that I can split page.my_var to substrings and use them then as site.data.header["foo"]["bar"]["baz"]. But this increases Jekyll build time.
I guess I need some Ruby plugin, but don't know one and I don't know Ruby to write one.
If you know such plugin or can help me write one or know some native workaround, this would be sooo great!
Help :)
Also I know that I can split page.my_var to substrings and use them then as site.data["foo"]["bar"]["baz"]. But this increases Jekyll build time.
I'm not sure you can do this. And even if you can, I'm not sure that there is a real performance impact.
But you can do this :
_data/foo.yml
bar:
baz: 1
biz: 2
buz: 3
beer:
baz: 1
biz: 2
buz: 3
Then :
---
my_var: "foo.bar.baz"
---
{% assign keys = page.my_var | split: "." %}
{% assign object = site.data %}
{% for key in keys %}
{% assign object = object[key] %}
{% endfor %}
{{ object }}
What are hoping to do has been blocked by design.
site.data["foo.bar.baz"] implies that you need to have a data file named foo.bar.baz.yml or foo.bar.baz.json, ... etc.
But Jekyll sanitizes data files' names and therefore the resulting object would only have a key named "foobarbaz"
A plugin that purposely removes this sanitization can written but it wouldn't appear to be a benign or safe to end-users. So the chances of finding such a plugin in the wild is slim..

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

can I use ajax datatable in Django with runtime custom query

I really want to use datatable for big data with runtime custom query .I have tried django-datatable but seem , it have only static custom query. thanks
I took a quick look at django-datatable; the only thing it seems to do if provide a template tag to render entire DB tables to HTML responses.
{% load table_tags %}
...
{% render_table people %}
If you want custom DB queries in django, the normal way is to use the ORM, i.e.
#!/usr/bin/env python
from models import Person
from django.shortcuts import render_to_response
...
def some_view(...):
a_people = Person.objects.filter(name__startswith="A")
return render_to_response(<template>, context={"people":a_people})
// HTML template
...
{% for person in people %}
<tr><td>{{ person.id }}</td><td>{{ person.name }</td></tr>
{% endfor %}
...
This allows you all freedom to define a dynamic DB query in your python code.