Giter Site home page Giter Site logo

tempy's Introduction

tempy - Simple Python templates

tempy is a tiny (~200 SLOC) python template engine for text processing tasks. It exposes the full functionality of Python in your templates by passing everything to compile() and eval() with minimal processing.

Requirements

  • Python 2.6+ (2.6, 2.7, 3.2, 3.3, 3.4)

Usage

The function render compiles and renders a template. Any keyword arguments passed to it will be used as local variables:

>>> import tempy
>>> tempy.render('Hello {{name}}!', name='World')
'Hello World!'

The function compile compiles a template into a function for later use:

>>> import tempy
>>> func = tempy.compile('Hello {{name}}!', args=['name'],
...                      defaults=['Bill'])
>>> func('World')
'Hello World!'
>>> func()
'Hello Bill!'

The resulting function signature can be controlled using the arguments args, varargs, varkw and defaults. args is a list of argument names, varargs and varkw are the names of the * and ** arguments, and defaults is a list if default argument values. If defaults has n elements, they correspond to the last n elements listed in args.

Template Syntax

Inline Expressions

Inline expressions are of the form {{...}}. Any python expression that can be evaluated using eval is allowed:

>>> tempy.render('{{ 1 + 1 }}')
'2'

Control Structures

The syntax for inline expressions can also be used for control structures such as if, for, def and so forth. However, they have to be explictly closed using the special keyword end:

>>> tempy.render('{{for x in range(10):}}{{x}} {{end}}')
'0 1 2 3 4 5 6 7 8 9 '

Note, though, that special keywords like continue and pass are only valid in code blocks:

>>> tempy.render('''{{for x in range(10):-}}
... <%- if x == 2: continue -%>
... {{x}} {{end}}''')
'0 1 3 4 5 6 7 8 9 '

Code Blocks

Blocks of python code embedded within <%...%> will be executed:

>>> tempy.render('<% x = 42 %>x is {{x}}')
'x is 42'

Note that python indentation rules apply within code blocks:

>>> tempy.render('''<%
... out = []
... for x in range(10):
...   out.append(str(x))
... %>{{' '.join(out)}}''')
'0 1 2 3 4 5 6 7 8 9'

Escaping

To escape a code block or inline statement, place a backslash character (\) before the start of a code block or inline statement token. For example:

>>> tempy.render(r'\<%x = 42%>')
'<%x = 42%>'

Whitespace Control

Placing a minus sign (-) at the start or end of a code block or inline statement will remove all whitespace before or after that block respectively:

>>> tempy.render('{{if True:-}}  42  {{end}}')
'42  '
>>> tempy.render('{{if True:}}  42  {{-end}}')
'  42'
>>> tempy.render('{{if True:-}}  42  {{-end}}')
'42'

License

MIT License

tempy's People

Contributors

pyokagan avatar

Watchers

 avatar James Cloos 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.