Bricklayer::Templater::Parser - The parsing engine for Bricklayer::Templater


Bricklayer-Templater documentation  | view source Contained in the Bricklayer-Templater distribution.

Index


NAME

Top

    Bricklayer::Templater::Parser - The parsing engine for Bricklayer::Templater

Methods

Top

NAME

Top

Bricklayer::Templater::Parser - A generic parsing module.

SYNOPSIS

Top

use Bricklayer::Templater::Parser;

my $template_text; my $start_tag_prefix; my $end_tag_prefix;

my @tokens = Template::Parser::parse_text($template_text,$start_tag_prefix,$end_tag_prefix);

REQUIRES

Top

Perl 5.8 Exporter

DESCRIPTION

Top

The parser package has a simple and single job to do, and that is to take a block of text and turn it into an ordered array of tokens containing either a text block or a single tag or tag block. Each token will contain both the block and a flag noting what kind of token it is (text, tag_block or tag). The package then returns the array.

METHODS

Top

parse_text()

The parse_text() function takes the template text, a start tag prefix, and an end tag prefix, parses the appropriate tags and returns a token tree in the form of an array of hashes, each hash with a variable number of elements, depending on its type.

For Example:

    %text_token = (
                    type => "text", 
                    block => $block,	
                );

    %block_tag_token = (
                    type => "block_tag", 
                    tagname => $tagname,
                    block => $block,
                    attributes => \%attributes,
                );	

    %tag_token = (
                    type => "tag", 
                    tagname => $tagname,
                    attributes => \%attributes,
                );

The attributes value is a reference to an attributes hash in the form of:

    %attributes = (
    				"attribute_name" => "attribute_value",
    			);

Further Notes: The token tree returned by parse_text is not recursive, thus the tags inside a tag_block will not be processed, and you will need to call the parse_text() function again when dealing with those tags.

To build a complete tree, call the parse_text() function iteratively like so:

	sub parse {
		my $template_text = shift;
		my $start_tag_prefix = shift;
		my $end_tag_prefix = shift;
		my @token_tree;

    my @tokens = Parser::parse_text($template_text,$start_tag_prefix,$end_tag_prefix);

    foreach my $token (@tokens) {
        if ($token->{type} eq 'block_tag') {
            my @sub_tokens;
            @sub_tokens = parse($token->{block},$start_tag_prefix,$end_tag_prefix);
            $token->{block} = \@sub_tokens;
        }
        push @token_tree, $token;
    }
    return @token_tree;
	}

parse_attributes

parses the tag attributes

mytrim

trims whitespace for us

AUTHOR

Top

(c) 2004 Jason Wall, <jason@walljm.com>, www.walljm.com (c) 2004 Jeremy Wall, <jeremy@marzhillstudios.com>, jeremy.marzhillstudios.com


Bricklayer-Templater documentation  | view source Contained in the Bricklayer-Templater distribution.