Context processors

Almoststatic add a few commands to Jinja2 with the mechanism called context_processor (in terms of Jinja and Flask). You can use those commands into your template files and within your yaml contents.

The commands are:

enum_list

This simple add to templates the Python command enumerate which can be useful to get a unique id for each item of a list in template:

{% for i,item in enum_list(widget.tabs) -%}
<li id="{{widget.id}}{{i}}">{{ item.text|safe }}
</li>
{% endfor -%}

iif

Inline if a shortcode for if/else into templates. Syntax is iif(condition, if_true, if_false="").

Instead to write:

{{'active' if page.pagename == item.url else ""}}
{# or #}
{{'active' if page.pagename == item.url else "not-active"}}

Simply write:

{{iif(page.pagename == item.url,'active')}}
{# or #}
{{iif(page.pagename == item.url,'active','not-active')}}

if_in

Shortcode to check for list inclusion. Syntax is if_in(value, iterable, if_not="").

Try:

{{if_in('carousel-caption-bg', widget.tags)}}
{# or #}
{{if_in('carousel-caption-bg', widget.tags, 'my-class')}}

This is useful to add classes on arbitrary positions into widgets.

get_markdown

Transform a Markdown text into html, usually you don’t need to call it because is automatic called at each widget.text rendering.

{% set myvar = "# this is a title" %}
{{ get_markdown(myvar)|safe }}

get_media

get_media is a very important command.

Flask by default search media files into folder ./static using the command {{ url_for('static', filename='...') }}.

Almoststatic, to encapsulate all contents within the content folder replace the url_for command with get_media and this helps to configure media destination folder when writing static pages.

replace:

<link rel="stylesheet" href="{{url_for('static', filename='css/main.css')}}">

with:

<link rel="stylesheet" href="{{get_media('css/main.css')}}">

And put files into content/media directory.

But if you really need to use url_for command, you can write a special Flask app dedicated to write static files, see samples.

But there is more. get_media is recognized even into contents on yaml files:

- type: jumbotron
  id: jumbo1
  style: '
    background-image: url({{get_media("gradient1.jpg")}});
    background-size: cover contain;
    min-width: 100%;
    height: 16em;
    color: white;
    '

in this case we add some style to the widget and within the css we call an image from media folder.

get_url

Internal url’s of our site, can change from Flask app to static site, for this reason we have a function that build the url according to environment. The url is composed by a prefix, the url that is the same of path and name of pages and a suffix.

Internal url’s paths by default are all relatives, so if you write a static site, it can be navigated also from filesystem. For this reason prefix usually is an empty string, but if you need, you can force a prefix for the static site.

So:

<a href="{{get_url('index')}}">Index</a>

return index on flask and can return something like /mysite/index.html on static site.

Note that url’s passed to get_url don’t have leading or trailing dashes.

Note

With internal url’s you can’t use the markdown shortcut [Index]({{get_url('index')}}), but always the plain html version. This because it goes in conflict during rendering page and generate a runtime error.

embed

Do the same thing that can be done with Embedding and including. Can be useful to embed contents for not recognized areas of page (see Writing pages):

<aside>
  {% if page.aside %}
  {{embed(page.aside)|safe}}
  {% else %}
  {{include('include/aside.yaml')|safe}}
  {% endif %}
</aside>

In this case if page contain a list called aside, the content is rendered in the <aside></aside> section of page.

include

Like embed let embedding contents at the template level, include let including files at template level (and even within page texts.)

As you can see in the previous sample, the page.aside list is included in page, but if it is not present, the file include/aside.yaml is included.