Skip to main content

Slim templates syntax for Jinja2 templates

Project description

Slim templates syntax for Jinja2.

This release breaks compatibility. Parsing = for Jinja Output is complex. I dropped it in favor of directly using {{ jinja_output }}.

This release(1.0) onwards, the backward syntax compatibility will be maintained.


pip install slimish_jinja

Examples of using it as Jinja2 extension(, with Flask( and standalone( and are bundled.

If you want to use it for any other purpose, the lexer - and parser - are simple enough. lexer reads the input by lines and generates tokens. parser implements a hand rolled recursive descent parser.

For quick reference, this slim::

    / Inline static content.
    - block title
        |Slimish-Jinja Example
    / Self closing tag with attributes.
    meta name="keywords" content="template language"
    %script type="text/javascript" src="{{ ASSET_URL }}"
    / Empty jinja tag.
    @block script

/ 'id' and 'class' shortcut.
    / Nested static content.
    |This is my header.
    / 'div' with 'id' and 'class' shortcut.
    / Empty html tag.
    p Dynamic {{ content }}
        |Nested dyanmic  {{ content }}
        Left indent is preserved in text blocks.
        |<a href="">Google</a>
    / Dynamic attributes.
    ul class="{{ user_class }}"
    / Jinja tag.
    - for user in users
        li {{ }}
        - if user.last_name
        li {{ user.last_name }}
        - elif user.middle_name
        li {{ user.middle_name }}
    - else
        li No user found.

converts to::

<!doctype html>
    {% block title %}
        Slimish-Jinja Example
    {% endblock %}
    <meta  content="template language" name="keywords"/>
    {% block script %}{% endblock %}
<body id="home" class="fluid liquid">
    This is my header.
    <div id="contents" class="main">
    <p>Dynamic {{ content }}</p>
        Nested dyanmic {{ content }}        Left indent is preserved in text blocks.
        <a href="">ThoughtNirvana</a>
    <ul  class="{{ user_class }}">
    {% for user in users %}
        <li>{{ }}</li>
        {% if user.last_name %}
        <li>{{ user.last_name }}</li>
        {% elif user.middle_name %}
        <li>{{ user.middle_name }}</li>
        {% endif %}
    {% else %}
        <li>No user found.</li>
    {% endfor %}

Doctype delcarations.

<!DOCTYPE html>

<!DOCTYPE html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "">

<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "">

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "">

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">

Static content

/ Inline static content.
h1 This is my header

/ Nested static content.
  |This is my header

/ Multiline static content.
  |Span mutliple lines.
     Left indent preserved in the output.

Dynamic content

/ Inline
h1 {{ user.title }}

/ Nested
  {{ user.title }}

/ Mixed with text.
  |The user name is {{ }}

Jinja tags

- for user in users
  li {{ }}
  - if {{ user.lastname }}
    li {{ user.lastname }}
- else
  li No users found

Dynamic contents in attribute

  a href="{{ user.url  }}" {{ }}

id and classname shortcuts

#contents.main.liquid => <div id="contents" class="main liquid">

body#home.left => <body id="home" class="left">

Code comments.

/ Single slash comments.
/ Not part of the output.
p This is content.

Empty html tags

%div => <div></div>

Empty jinja tags.

@block title => {%block title %}{% endblock %}

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slimish_jinja-1.1.2.tar.gz (10.1 kB view hashes)

Uploaded Source

Built Distribution

slimish_jinja-1.1.2-py3-none-any.whl (10.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page