0.0.1 • Published 9 years ago

jinjer v0.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
9 years ago

Jinjer

A JavaScript template engine inspired by Django/Jinja syntax.

IMPORTANT DISCLAIMER: at the moment the project is at a very early stage of development and it's just a proof of concept!! It's not ready yet to do anything serious!!

Introduction

Jinjer is a semantic templating language (included of all tools to render it) which takes inspiration by powerful Python's Django and Jinja template languages, but with an original flavor!

It's clean and easy to use: it could be mixed with canonical HTML markup without making it too spicy!

The main features of Jinjer are:

  • Readable: the syntax could be mixed with HTML without loosing its semantic taste and readability.
  • Less-logic, not logic-less: the built-in syntax allows simple logic and arithmetical operations in order to let templates look intuitive and cleaner, without bloating them with ugly markup.
  • Template inheritance: it makes possible to reuse similar layout for different templates.
  • Extensible: the basic syntax could be expanded registering custom template tags or filters.
  • High performance: templates sources will be compiled in pure JavaScript code for best runtime performance.

How to use

You can both use Jinjer on server-side (Node.js):

var jinjer = require('jinjer');

...or client-side (browser):

<script src="jinjer.min.js"></script>

You can render plain strings, file-based templates or even pre-compiled templates:

jinjer.renderString('Hello {{ name }}', { name: "Emanuele" });

// or:

jinjer.render('hello.jnj', { name: "Emanuele" });

// or:

var template1 = jinjer.compileString('Hello {{ name }}');
template1.render({ name: "Emanuele" });
template1.render({ name: "Jinjer" });

// or:

var template2 = jinjer.compile('hello.jnj');
template2.render({ name: "Emanuele" });
template2.render({ name: "Jinjer" });

Syntax Overview

Jinjer could look very similar to Jinja:

{% extends "layout.html" %}
{% block body %}
  <ul>
  {% for user in users %}
    <li><a href="{{ user.url }}">{{ user.username }}</a></li>
  {% endfor %}
  </ul>
{% endblock %}

But Jinjer has some spicy touches:

<p>{{ object.name }}</p>

<!-- is equivalent to: -->

{% let field_name="name" %}
<p>{{ object[field_name] }}</p>

The accessors could be used with any object and could be concatenate:

{% if myCat.owner.name == cats[5].owner.name %}
  <p>These cats have the same owner!</p>
{% endif }

Functions in templates are used through template tags, as in Django template language:

<p>Total people: {% add crew.count guests.count %}</p>

<!-- which is equivalent to: -->

<p>Total people: {{ crew.count + guests.count }}</p>

Filters are included in Jinjer too:

<p>Today is: {{ date|toTimezone:'utc' }}</p>

<!-- or: -->

<p>{{ post.content|safe }}</p>

License

The MIT License (MIT)

Copyright (c) 2016 Emanuele Bertoldi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.