Git Product home page Git Product logo

microtemplates's Introduction

microtemplates.py

A toy templating engine. Accompanying blog post: http://alexmic.net/building-a-template-engine/.

Why?

I just wanted to learn how to write a templating engine. It's not feature-complete or production-ready. It's a playground and it will be a work in progress.

Documentation

There are two types of tags, blocks and variables.

Variables

<div>{{my_var}}</div>

Blocks

There are three types of blocks โ€“ if, each and call.

Loops

{% each items %}
    <div>{{it}}</div>
{% end %}

{% each [1,2,3] %}
    <div>{{it}}</div>
{% end %}

it references the current item in the iteration and it is scoped to this item's attributes. To access attributes of the parent context use ... For example:

{% each items %}
    <div>{{..name}}</div><div>{{it}}</div>
{% end %}

Conditionals

Supported operators are: >, >=, <, <=, ==, !=. You can also use conditionals with things that evaluate to truth.

{% if num > 5 %}
    <div>more</div>
{% else %}
    <div>less or equal</div>
{% end %}

{% if items %}
    <div>we have items</div>
{% end %}

Callables

Callables can get passed positional or keyword arguments.

<div class='date'>{% call prettify date_created %}</div>
<div>{% call log 'here' verbosity='debug' %}</div>

Benchmarks

Using benchmarks.py and the templates files in templates/ here are the results:

microtemplates => run 1000 times, took 0.58 ms
django => run 1000 times, took 1.39 ms
jinja2 => run 1000 times, took 7.68 ms

microtemplates's People

Contributors

alexmic avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.