NAME

Artemus - Template Toolkit


SYNOPSIS

 use Artemus;

 # normal variables
 %vars=(
        "copyright" => 'Copyright 2002',   # normal variable
        "number" => 100,                   # another
        "about" => '{-copyright} My Self', # can be nested
        "link" => '<a href="$0">$1</a>'    # can accept parameters
        );

 # functions as templates
 %funcs=(
        "random" => sub { int(rand(100)) }, # normal function
        "sqrt" => sub { sqrt($_[0]) }       # can accept parameters
        );

 # create a new Artemus instance
 $ah=new Artemus( "vars" => \%vars, "funcs" => \%funcs );

 # do it
 $out=$ah->process('Click on {-link|http://my.page|my page}, {-about}');
 $out2=$ah->process('The square root of {-number} is {-sqrt|{-number}}');


DESCRIPTION

Artemus is yet another template toolkit. Though it was designed to preprocess HTML, it can be used for any task that involves text substitution. These templates can be plain text, text with parameters and hooks to real Perl code. This document describes the Artemus markup as well as the API.

You can download the latest version of this package and get more information from its home page at

 http://www.triptico.com/software/artemus.html


THE ARTEMUS MARKUP

Simple templates

The simplest Artemus template is just a text substitution. If you set the 'about' template to '(C) 2000/2002 My Self', you can just write in your text

 This software is {-about}.

and found it replaced by

 This software is (C) 2000/2002 My Self.

Artemus templates can be nestable; so, if you set another template, called 'copyright' and containing '(C) 2000/2002', you can set 'about' to be '{-copyright} My Self', and obtain the same result. Though they can be nested nearly ad-infinitum, making circular references is unwise.

Templates with parameters

This wouldn't be any cool if templates where just text substitutions. But you can create templates that accept parameters just by including $0, $1, $2... marks inside its content. This marks will be replaced by the parameters used when inserting the call.

So, if you create the 'link' template containing

 <a href="$0">$1</a>

you can insert the following call:

 {-link|http://www.triptico.com|Angel Ortega's Home Page}

As you can see, you use the | character as a separator among the parameters and the template name itself.

Perl functions as templates

Anything more complicated than this would require the definition of special functions provided by you. To do it, you just add templates to the 'funcs' hash reference when the Artemus object is created which values are references to Perl functions. For example, you can create a function returning a random value by using:

 $funcs{'random'}=sub { int(rand(100)) };

And each time the {-random} template is found, it is evaluated and returns a random number between 0 and 99.

Functions also can accept parameters; so, if you define it as

 $funcs{'random'}=sub { int(rand($_[0])) };

then calling the template as

 {-random|500}

will return each time it's evaluated a random value between 0 and 499.

Aborting further execution from a function

If the abort-flag argument is set to a scalar reference when creating the Artemus object, template processing can be aborted by setting this scalar to non-zero from inside a template function.

Caching templates

If a template is expensive or time consuming (probably because it calls several template functions that take very much time), it can be marked as cacheable. You must set the 'cache-path' argument for this to work, and include the following special Artemus code inside the template:

 {-\CACHE|number}

where number is a number of days (or fraction of day) the cache will remain cached before being re-evaluated. Individual template functions cannot be cached; you must wrap them in a normal template if need it.

Documenting templates

Artemus templates can contain documentation in Perl's POD format. This POD documentation is stripped each time the template is evaluated unless you create the Artemus object with the contains-pod argument set.

See http://www.perldoc.com/perl5.8.0/pod/perlpod.html and http://www.perldoc.com/perl5.8.0/pod/perlpodspec.html for information about writing POD documentation.

Unresolved templates

If a template is not found, it will be replaced by its name (that is, stripped out of the {- and } and left there). Also, the names of the unresolved templates are appended to an array referenced by the unresolved argument, if one was defined when the Artemus object was created.

Predefined templates

if
 {-if|condition|text}

If condition is true, this template returns text, or nothing otherwise. A condition is true if is not zero or the empty string (the same as in Perl).

ifelse
 {-ifelse|condition|text_if_true|text_unless_true}

If condition is true, this template returns text_if_true, or text_unless_true otherwise.

ifeq
 {-ifeq|term1|term2|text}

If term1 is equal to term2, this template returns text, or nothing otherwise.

ifneq
 {-ifneq|term1|term2|text}

If term1 is not equal to term2, this template returns text, or nothing otherwise.

ifeqelse
 {-ifeqelse|term1|term2|text_if_true|text_unless_true}

If term1 is equal to term2, this template returns text_if_true, or text_unless_true otherwise.

\CACHE
 {-\CACHE|time}

Marks a template as cacheable and sets its cache time. See above.

\VERSION
 {-\VERSION}

Returns current Artemus version.

\BEGIN
\END
If you set these templates, they will be appended (\BEGIN) and prepended (\END) to the text being processed.


FUNCTIONS AND METHODS

new

 $ah=new Artemus(
        [ "vars" => \%variables, ]
        [ "funcs" => \%functions, ]
        [ "inv-vars" => \%inverse_variables, ]
        [ "include-path" => $dir_with_templates_in_files, ]
        [ "cache-path" => $dir_to_store_cached_templates, ]
        [ "abort-flag" => \$abort_flag, ]
        [ "unresolved" => \@unresolved_templates, ]
        [ "use-cr-lf" => $boolean, ]
        [ "contains-pod" => $boolean, ]
        [ "paragraph-separator" => $separator, ]
        [ "strip-html-comments" => $boolean, ]
        [ "AUTOLOAD" => \&autoload_func ]
        );

Creates a new Artemus object. The following arguments (passed to it as a hash) can be used:

vars
This argument must be a reference to a hash containing template - content pairs.

funcs
This argument must be a reference to a hash containing template name - code reference pairs. Each time one of these templates is evaluated, the function will be called with the template parameters passed as the function's arguments.

inv-vars
This argument must be a reference to a hash containing text - content pairs. Any occurrence of text will be replaced by content. They are called 'inverse variables' because they use to store variables that expand to Artemus markup, but can contain anything. This is really a plain text substitution, so use it with care (NOTE: this option is disabled by now until it works correctly).

include-path
If this string is set, it must point to a readable directory that contains templates, one on each file. The file names will be treated as template names. Many directories can be specified by separating them with colons.

cache-path
If this string is set, it must contain the path to a readable and writable directory where the cacheable templates are cached. See Caching templates for further information.

abort-flag
This argument must be a reference to a scalar. When the template processing is started, this scalar is set to 0. Template functions can set it to any other non-zero value to stop template processing.

unresolved
If this argument points to an array reference, it will be filled with the name of any unresolved templates. Each time a template processing is started, the array is emptied.

use-cr-lf
If this flag is set, all lines are separated using CR/LF instead of just LF (useful to generate MSDOS/Windows compatible text files).

contains-pod
If this flag is set, the (possible) POD documentation inside the templates are not stripped-out. Understand this flag as saying 'this template has pod as part of its content, so do not strip it'. See Documenting templates.

paragraph-separator
If this argument is set to some string, all empty lines will be substituted by it (can be another Artemus template).

strip-html-comments
If this flag is set, HTML comments are stripped before any processing.

AUTOLOAD
If this argument points to a sub reference, the subrutine will be executed when a template is unresolved and its return value used as the final substitution value. Similar to the AUTOLOAD function in Perl standard modules. The unresolved template name will be sent as the first argument.

armor

 $str=$ah->armor($str);

Translate Artemus markup to HTML entities, to avoid being interpreted by the parser.

unarmor

 $str=$ah->unarmor($str);

Translate back the Artemus markup from HTML entities. This is the reverse operation of armor.

strip

 $str=$ah->strip($str);

Strips all Artemus markup from the string.

params

 $str=$ah->params($str,@params);

Interpolates all $0, $1, $2... occurrences in the string into the equivalent element from @params.

process

 $str=$ah->process($str);

Processes the string, translating all Artemus markup. This is the main template processing method. The abort-flag flag and unresolved list are reset on each call to this method.


AUTHOR

Angel Ortega angel@triptico.com