Parse::Plain

Template parsing engine
Download

Parse::Plain Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Andrew Alexandre Novikov
  • Publisher web site:
  • http://search.cpan.org/~andrewn/

Parse::Plain Tags


Parse::Plain Description

Template parsing engine Parse::Plain is a Perl class for parsing text-based templates. It was designed to use with HTML, XHTML, XML and other markup languages but usually can be used with arbitrary text files as well.Basic constructions in the templates are tags and blocks. Both must have names. Valid symbols for using in tag and block names are digits, latin letters, underscores, dashes, dots, semicolons, colons, commas, parentheses, asterisks, ampersands, slashes and caret symbols. An exclamation mark ('!') has special meaning and will be discussed later. All names are case sensitive.Tag is a string in form %%tagname%%. There may be any number of tags with the same name and any number of different tags in the template.Block is a construction that begins with line {{ blocknameand ends with symbols }}Block-start element must be on separate line. There may be no other symbols from the beginning of line to the block-end element. However you may have other text (except block-start) after block-end on the same line (same as having those symbols on next line). Symbols between block-start and block-end form block body. Blocks are especially useful for iterative elements like table rows. Blocks can be nested and tags are allowed within block body.There is also a special form of tag names. Let's say you have a block named myblock. Then in the template you can use tags named %%!myblock%% and they will be substituted to current value of myblock.You can also hide block from place in template where it is defined by prepending ! to it's name. You'll still be able to use this block with appropriate tag names (with '!'). See "EXAMPLES" section.There is a difference between source block and result block (as used in some method names). The source block is a chunk of text that is exactly as it appears in source template unless you have changed it using methods "block_src", "unshift_block_src", "push_block_src". The result block is a chunk of text that appears in the output and affected by calls to "parse" function on this block or may be modified with "block_res", "unshift_block_res", "push_block_res" methods as well. See description of these methods elsewhere in this document. To illustrate the difference: # source block named 'b' in template: # {{ b # -%%Y%%- # }} $val = $t->block_src('b'); # $val eq '-%%Y%%-' $val = $t->block_res('b'); # $val == undef # now let's modify source block $t->push_block_src('b', 'Z|'); # -%%Y%%-Z| $t->unshift_block_src('b', 'X'); # X-%%Y%%-Z| $val = $t->block_src('b'); # $val eq 'X-%%Y%%-Z|' $val = $t->block_res('b'); # $val == undef # now let's modify result block $t->parse('b', '1'); # result block: X-1-Z| $t->parse('b', '2'); # result block: X-1-Z|X-2-Z| $t->unshift_block_res('b', '|'); # result block: |X-1-Z|X-2-Z| $val = $t->block_src('b'); # $val eq 'X-%%Y%%-Z|' $val = $t->block_res('b'); # $val eq '|X-1-Z|X-2-Z|'SYNOPSIS # in user's code use Parse::Plain; my $t = new Parse::Plain('/path/to/filename.tmpl'); my $t = new Parse::Plain('/path/to/filename.tmpl', 1, 2); $t->set_tag('mytag', 'value'); # %%mytag%% set to value $t->push_tag('mytag', '-pushed'); # %%mytag%% set to value-pushed $t->set_tag({'mytag' => 'value'}); # %%mytag%% set to value $t->unshift_tag('mytag', 'unshifted-'); # %%mytag%% set to unshifted-value # set a callback for tags like %%url:http://host.com/doc.html%% $t->callback('url', sub { return SomePackage::GetUrl($_); }); $t->push_block_src('myblock', 'some text to append to the block source'); $t->unshift_block_res('myblock', 'some text to prepend to the block result'); $t->parse('myblock', {'blocktag' => 'block value'}); # parse block $t->parse('myblock', {'blocktag' => 'another block value'}); $t->parse; # parse whole document $t->output; # output parsed results to STDOUT $t->unparse; # reset parsed result to original template Requirements: · Perl


Parse::Plain Related Software