Skip to main content

Template Processing Language.

Project description

1: Introduction

templ is the Template Processing Language.

templ is a (Turing complete) programming language, used for content generation from text-based template files and a simple but powerful processing language (templ itself) which is embedded directly in the template file.

templ can be used for:

  • Code generation.

  • Form letters.

  • Server-side scripting for web servers.

  • Customized markup with variant output formats.

  • Any other kind of dynamic text-based content generation.

1.1: Contact Information

This project is currently hosted on bitbucket, at https://bitbucket.org/bmearns/templ/. The primary author is Brian Mearns: you can contact Brian through bitbucket at https://bitbucket.org/bmearns.

2: Examples

The following show some simple example templates and they’re output, to give an idea of what can be done with templ.

2.1: Templ Basics

The following template shows some of the basic elements of templ templates:

Hello, World!
My Name is {set :NAME "templ"}. I am the TEMplate Processing Language.
Sometimes, {$ :NAME} likes to speak in the third person.

{$ :NAME} can do math:
    1 + 1 = {+ 1 1}
    1 + 2 = {+ 1 2}
    2 + 3 = {+ 2 3}
    3 + 5 = {+ 3 5}
    etc...

{$ :NAME} can operate on strings and lists:
    {implode "a" {' b n n s}}
    {str {cat {' a b c } {' d e f } }}

{$ :NAME} can do conditional processing:
    {if
        {== {+ 2 2} 5}
        "Oh No!"

        {== {+ 2 2} 4}
        "Phew!"

        "How did I get here?"
    }

{$ :NAME} can loop (and do trig):
{for :THETA {range 0 40 10} {
    echo "    sin(" {$ :THETA} ") = " {sin {rad {$ :THETA}}} {eol}}
}

{$ :NAME} can even do list comprehensions and user defined functions:
{v {set
    :MY-FUNC
    {lambda
        {' :THETA }
        {:
            {let :RADS}
            {$ :RADS {rad {$ :THETA}}}

            {echo "Processing theta=" {$ :THETA} "..." {eol}}

            %return value
            {+ {cos {$ :RADS}} {sin {$ :RADS}} }
        }
    }
}}{wrap "{" "}" {implode {glue "," {eol} "    "} {gen
    :T
    {range 40 80 10}
    {join ":" {$ :T} {:MY-FUNC {$ :T}}}
}}}

The output looks like this:

Hello, World!
My Name is templ. I am the TEMplate Processing Language.
Sometimes, templ likes to speak in the third person.

templ can do math:
    1 + 1 = 2
    1 + 2 = 3
    2 + 3 = 5
    3 + 5 = 8
    etc...

templ can operate on strings and lists:
    bananas
    [a, b, c, d, e, f]

templ can do conditional processing:
    Phew!

templ can loop (and do trig):
    sin(0) = 0.0
    sin(10) = 0.173648177667
    sin(20) = 0.342020143326
    sin(30) = 0.5


templ can even do list comprehensions and user defined functions:
Processing theta=40...
Processing theta=50...
Processing theta=60...
Processing theta=70...
{40:1.40883205281,
    50:1.40883205281,
    60:1.36602540378,
    70:1.28171276411}

2.2: Code Generation - A Sine Lookup Table

The following template shows an example of how to use templto generate C-code, in this case a sine lookup table.

{v
    {set :SIZE 10}
}const double sine_lut[{get :SIZE}] =
\{
{for i {range {get :SIZE}} {::
    {let :THETA}
    {$ :THETA {mult
        {$ i}
        {div 360 {$ :SIZE}}
    }}
    {spit {'
        "    "
        {round
            {sin {rad {$ :THETA}}}
            4
        }
        ,
        {\t}
        "// i = "
        {get i}
        ", theta = "
        {$ :THETA}
        " deg"
        {eol}
    }}
}}\};

The output looks like this:

const double sine_lut[10] =
{
    0.0,    // i = 0, theta = 0 deg
    0.5878, // i = 1, theta = 36 deg
    0.9511, // i = 2, theta = 72 deg
    0.9511, // i = 3, theta = 108 deg
    0.5878, // i = 4, theta = 144 deg
    -0.0,   // i = 5, theta = 180 deg
    -0.5878,        // i = 6, theta = 216 deg
    -0.9511,        // i = 7, theta = 252 deg
    -0.9511,        // i = 8, theta = 288 deg
    -0.5878,        // i = 9, theta = 324 deg
};

2.3: Embedded Data

The next example shows how templ allows you to easily embed data directly in the template file that uses it, allowing you to keep just one file under version control, for instance.

{v
    %Embedded data
    {$ :DATA {'
        %   Name            Year    Month (-1)      Date
        {'  "Alan T."     1912    05              23 }
        {'  "John V."     1903    11              28 }
        {'  "Claude S."   1916    03              30 }
        {'  "George B."   1815    10              2  }
        {'  "George B."   1815    10              2  }
        {'  "Ada L."      1815    11              15 }
        {'  "Charles B."  1791    11              26 }
        {'  "Donald K."   1938    0               10 }
        {'  "Dennis R."   1941    8               9  }
    }}
}{for :ROW {$ :DATA} {:
    {$ :STAMP {stamp {slice 1 {$ :ROW}}}}
    {$ :NOW {stamp}}
    {$ :AGE {floor {div {- {$ :NOW} {$ :STAMP}} {* 60 60 24 365.25}}}}
    {echo {@ 0 {$ :ROW}} ", age " {$ :AGE} " years." {eol} }
}}

It produces this:

Alan T., age 100 years.
John V., age 109 years.
Claude S., age 96 years.
George B., age 197 years.
George B., age 197 years.
Ada L., age 197 years.
Charles B., age 221 years.
Donald K., age 75 years.
Dennis R., age 71 years.

2.4: Programmatic Invocation

The real power of templ comes from the programmatic interface, which allows you to predefine symbols, and even executables (functions, macros and operators) in python, which are then accessible from the template. Because, although templ is Turing complete, and you could do all of your processing directly in the template (or a separate included template), doing advanced data processing in python can help keep your template files simpler.

3: Complete Contents

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

templ-1.2.0.0.zip (164.6 kB view details)

Uploaded Source

Built Distributions

templ-1.2.0.0.win32.zip (169.1 kB view details)

Uploaded Source

templ-1.2.0.0.win32.exe (291.9 kB view details)

Uploaded Source

templ-1.2.0.0-py2.7.egg (161.8 kB view details)

Uploaded Source

File details

Details for the file templ-1.2.0.0.zip.

File metadata

  • Download URL: templ-1.2.0.0.zip
  • Upload date:
  • Size: 164.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for templ-1.2.0.0.zip
Algorithm Hash digest
SHA256 cbb5d1f98c1e11b7fbb4dcb20f3231b2fd9b880bf2115c3cd9dd597326b30f43
MD5 329c7339082efa7cb202a1cc0cf4e35c
BLAKE2b-256 b628f55c6c02ead31b864c14fd4636a0b3106dfb296ff959b168755e7dd80d64

See more details on using hashes here.

File details

Details for the file templ-1.2.0.0.win32.zip.

File metadata

  • Download URL: templ-1.2.0.0.win32.zip
  • Upload date:
  • Size: 169.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for templ-1.2.0.0.win32.zip
Algorithm Hash digest
SHA256 35b5e2504486a4b07d106e4e0346d03947e8e8777b99dd7f34cc90256bb43fd5
MD5 d61387a06909dc3f4d63ba169e9a943f
BLAKE2b-256 aecf3e43176225dc16c640674a30c4951587169f3909ab2461d20035f83138e6

See more details on using hashes here.

File details

Details for the file templ-1.2.0.0.win32.exe.

File metadata

  • Download URL: templ-1.2.0.0.win32.exe
  • Upload date:
  • Size: 291.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for templ-1.2.0.0.win32.exe
Algorithm Hash digest
SHA256 0d79bb8971b1321cc23cf0a77ed3b81cdf7300300b6b5f29ce617cbac344ede8
MD5 2f2b447057a34865cb895d0be6deae99
BLAKE2b-256 d845dc86269de6d426a105422d82083b64c535fdac0885a8b91fc352faafe839

See more details on using hashes here.

File details

Details for the file templ-1.2.0.0-py2.7.egg.

File metadata

  • Download URL: templ-1.2.0.0-py2.7.egg
  • Upload date:
  • Size: 161.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for templ-1.2.0.0-py2.7.egg
Algorithm Hash digest
SHA256 8c500198c373b6ae464a4b9132c1b14c8b58172aa6aee1f0d28938e42b9d2f42
MD5 9f15eef370ed653fbf7834dbd593ddc6
BLAKE2b-256 13650861f9157d65d59b0fd6a8feb95ca313d23b93f819aa2b86658a73a3f04f

See more details on using hashes here.

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