Source

templ / misc / demo.templ

The default branch has multiple heads

{v 
    {$ NAME templ}
}Welcome to the {$ NAME} demo template!

{$ NAME} is the TEMplate Processing Language, used for content generation
from text-based template files and a simple but powerful processing language
({$ NAME} itself). {$ NAME} can be used for:
{v {for EXAMPLE {'
        "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."
    }
    {:
        {echo "    * "}
        {echo {$ EXAMPLE}}
        {echo "
"}
    }
}}
The {$ NAME} language is just a simple text-based language, but it is Turing
Complete and comes equipped with a number of useful functions. You could,
theoretically, use it as a general scripting language, though it really is
best suited for it's natural purpose, processing templates.

Still, it's very powerful for this. For instance, you could easily generate a
sine lookup table for C:

/**********************************************/
{v {set SIZE 10}
}const double sine_lut[{get SIZE}] =
\{
{for i {range {get SIZE}} {:
    {echo {glue
        "    "
        {round 
            {sin {div
                {mult {get i} 2 {pi}}
                {get SIZE}
            }}
            4
        }
        , {tab} "// i = " {get i}
        {lnbrk}
    }}
}}
\};
/**********************************************/

One of the nice things about {$ NAME} is that you can embed data directly into
your template file so that you only need to keep one file under version
control:

###############################################
{v {set YEAR 2012}
}{v
{set DATA {'
    %   Name            Month       Year        Score
    {'  "Alan T."       June        1912        61  }
    {'  "John B."       December    1924        53  }
    {'  "John von N."   December    1903        47  }
    {'  "Claude S."      April       1916        59  }
}}
}{
   for ROW {get DATA}
   {spit
<<<User "{at 0 {get ROW}}" (age {
    sub {get YEAR} {at 2 {get ROW}}
    }) has a score of: {at 3 {get ROW}}
>>>
    }
}###############################################

Alternatively, you can invoke {$ NAME} from python and pass in your own data
to prepopulate variables. This lets you generate code based on data stored in
other files, other formats, or even remote locations like databases or network
services.

By the way, if you're using the {$ NAME} demo program, you can see what this
template file actually looks like (unprocessed) by preceding the filename with
a + character. Or you could always just open it in a text editor.

{ dont 
% "dont" is kind of like a multi-line comment. So if you're not editing with
% Vim, you can ignore the rest of the file.
"
 vim: set ft=templ tw=78:
" }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.