NAME
Text::MustacheTemplate::Compiler - Simple mustache template compiler
SYNOPSIS
use Text::MustacheTemplate::Lexer;
use Text::MustacheTemplate::Parser;
use Text::MustacheTemplate::Compiler;
# change delimiters
# local $Text::MustacheTemplate::Lexer::OPEN_DELIMITER = '<%';
# local $Text::MustacheTemplate::Lexer::CLOSE_DELIMITER = '%>';
my $source = '* {{variable}}';
my @tokens = Text::MustacheTemplate::Lexer->tokenize();
local $Text::MustacheTemplate::Parser::SOURCE = $source; # optional for syntax error reporting
my $ast = Text::MustacheTemplate::Parser->parse(@tokens);
my $template = Text::MustacheTemplate::Compiler->compile($ast);
my $result = $template->({ variable => 'foo' });
print "result: $result\n"; # print "* foo";
DESCRIPTION
Text::MustacheTemplate::Compiler is a compiler for Mustache template.
This is low-level interface for Text::MustacheTemplate. The APIs may be change without notice.
METHODS
- compile($ast)
-
Compiles the abstract syntax tree (AST) from Text::MustacheTemplate::Parser into a Perl code reference (CodeRef) that renders the template when called.
Parameters:
Returns a CodeRef that takes a context scalar value and returns the rendered template string.
DESCRIPTION
Text::MustacheTemplate::Compiler takes the parsed AST and transforms it into executable Perl code that can efficiently render the template with a given context. This module handles the optimization of the compilation process and creates specialized code paths depending on the template structure.
The compiler performs several optimizations:
Direct interpolation of static text
Specialized handling for arrays, hashes, and lambda functions in section blocks
Optimized variable lookup with dot notation support
Proper handling of context stacking
INTERNALS
The compiler uses string eval to generate efficient Perl code that handles the template rendering. It creates a closure that has access to utility functions for HTML escaping, context resolution, etc.
The following internal optimizations are used:
Avoiding unnecessary concatenations for performance
Proper scope handling for nested sections
Special handling for lambda functions in templates
Context path resolution that follows Mustache specification
LICENSE
Copyright (C) karupanerura.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
AUTHOR
karupanerura <karupa@cpan.org>