View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
tateisu > Text-Template-Compact > Text::Template::Compact



Annotate this POD


Open  0
View/Report Bugs
Module Version: 0.1.12   Source  


Text::Template::Compact - text base template expand module


        use utf8;
        use Encode;
        use Text::Template::Compact;

        # create instance
        my $tmpl = new Text::Template::Compact();

        # load from file
        $tmpl->loadFile('template.html','utf8') or die $tmpl->error;

        # or load from text
        $tmpl->loadText('src1','hello, ${world}!','utf8') or die $tmpl->error;

        # prepare binding parameter
        my $param = { world=>'World' };

        # set default filter that used when output parameter or expression

        # set encoding that used when read non-utf8 value from parameter.

        # set string that used for print instead of undefined value.

        # print to filehandle

        # or get string output
        print Encode::encode('utf8',$tmpl->toString($param));


The Text::Template::Compact module is text base template expand module. This module compile control tag in source text and output it with parameter data that gived at runtime. Its main purpose is handling HTML templates in Web Application, but it also available for other situation such as mail,XML,etc,.



control tag

 ${ [label:] [% tagtype] tag-arg [;[label:] [% tagtype] tag-arg ...] [#filter]}


you may use expression in control statement such as print,eval,for,if to reference data in parameter object. supported operator and literarls are:

almost same as perl:

() ++ -- ** ! ~ unary+ unary- * / % + - << >> int defined length < > <= >= lt gt le ge == != <=> eq ne cmp eq ne cmp & | ^ && || // ?: = **= *= /= %= += -= <<= >>= &= |= ^= &&= ||= //= , print scalar join push pop shift unshift not and or xor

notation is changed from perl:
  • cat (same as . in perl)
  • repeat (same as x in perl)
  • makearray (same as [] in perl)
  • makehash (same as {} in perl)
other operators:
  • bool (same as not not )
  • nz (same as !=0 )
  • call coderef,args... (same as coderef->(args,...) in perl)
  • call obj,"method",args... (same as obj->"method"(args,...) in perl)
  • keyword that start with alphabet and underbar and follows alphabet,underbar,numeric.
  • "double quoted string" . if you want to write " in quoted string , please escape it to "".
  • numeric constant. (integer only. '.' is not supported)

expression for data reference

        # for example, the parameter data structure that passed to template is this:
        $VAR1 = {
                h=>{ a=>1,b=>2,c=>3, ge=>4},
                ge => 4,
                ge => 4,
        #then data reference and its value is:
        h.c     => means 3
        v.1     => means 'b'
        v.(1+2) => means 'd'
        h[v.1]  => means  2  
        v[h.c]  => means 'd' 
        ge      => error.  because 'ge' is operator, not keyword.
        $ge     => means 4. using $ to change interpret of keyword and operator.
        h["ge"] => means 4. using [] to avoid interpret of keyword and operator.

looks of expression for data reference are like as name , name.key , name[2].key , $name. each node that separated by . and [] indicate hash-key or array-index in parameter data structure.


reference hash-key or array-index of a by value of b. left term must be data reference.


reference hash-key or array-index of a by keyowrd of b. left term must be data reference. if right term is keyword , use its name as key or index. if right term is not keyword, use its value as key or index. if each term is data reference , concat the data path.


symbol $ is reference to whole of parameter data. and you can use $keyword to avoid conflict between keyword and operator .


symbol $$ is reference to whole of template object.


        ${v}            # referencde parameter data by key 'v'.
        ${"v"}          # string constant. not reference parameter data.
        ${2}            # numeric constant.not reference parameter data.
        ${va[2]}        # reffernce array by numeric subscript .
        ${va[-1]}       # negative subscript to reference end of array.
        ${va[v2]}       # use value of another data as subscript index of array.
        ${vh["v2"]}     # use string constatnt  to subscript key of hash.
        ${vh.v2}        # use keyword name      in right term of '.'
        ${va.-1}        # use expression value  in right term of '.'
        ${va.(1+2)}     # use expression value  in right term of '.'
        ${vh."v2"}      # use string  constatnt in right term of '.'
        ${va.2}         # use integer constatnt in right term of '.'
        ${va.v3}        # same as 'va.0'. if left term is array,keyword of right term is interpolated in numeric context.
        ${vh.(va.2)}    # same as ''. if term is data reference, each path willbe concat.
        ${$2.2}         # example of using $ in data reference
        ${$.2.2}        # example of using $ in data reference
        ${$[2].2}       # example of using $ in data reference
        ${$defined}     # keyword after $/$$ is interpolated as dara reference although its looks is operator.
        ${$"defined"}   # constant literal after $/$$ is interpolated as dara reference.
        ${$.defined }   # Error. bare 'defined' is interpolated as operator.


        ${ [%print] expr }
        ${ "売り上げ"."合計" }
        ${||"not available"}
        E<lt>a href="?who=${data#uri}"E<gt>${data#nobr}E<lt>/aE<gt>
        E<lt>textarea name="a"E<gt>${body#nobr}E<lt>/textareaE<gt>

Print expression to place of control tag. And you can omit part of '%print' in tag, such as ${expr}.


        ${%eval expr}
        ${%eval name="foo",caption="bar"}

Eval expression, but not print to outout. Also you can use operator 'print' in this tag.


        ${%evalperl "perlcode" [result [arg [arg ...]]] }

You can write perl code in "perlcode". please quote it. If you specify data reference at 'result', result of eval is stored to it. If you specify data reference at 'arg', its value willbe copied to local variable $a,$b,... brfore eval. reference of parameter object is copied to local variable $_ before eval.


        ${ [label:] %if expr } or ${ [label:] %ifc    "perlcode" [arg [arg ...]] }
        ${%elsif expr }or ${%elsifc "perlcode" [arg [arg ...]] }


        ${ [label:] %for item in array [index] [indexstart] }

item must have LValue.

array must have array reference.

index can be omit, or must have LValue.

indexstart can be omit, or must have numeric value.

loops for each element of array.

in the loop, element of index are copied to item and index.

If indexstart is specified, initial value of index will be changed.


        ${ [label:] %while
            [init init_expr]
            [precheck precheck_expr]
            [postcheck postcheck_expr]
            [step step_expr]
            [final final_expr]
        ${%while init i=0 precheck i<10 step i++} ${i} ${%end}
        ${%while init i=0 postcheck i<10 step i++} ${i} ${%end}
        ${%while precheck left-- } ...  ${%end}
        ${%while postcheck --left} ...  ${%end}
 pseudo code to explain timing of evaluate each expression 
                if(!precheck_expr) break;
                if(!postcheck_expr) break;


        ${%break [label] }
        ${%continue [label] }

These tags are used to exit block.


        ${%blockdefine blockname }
        ${%blockpaste blockname }



syntax highlighting: