3 * Smarty Internal Plugin Smarty Template Compiler Base
4 * This file contains the basic classes and methods for compiling Smarty templates with lexer/parser
14 include 'smarty_internal_parsetree.php';
17 * Class SmartyTemplateCompiler
20 * @subpackage Compiler
22 class Smarty_Internal_SmartyTemplateCompiler extends Smarty_Internal_TemplateCompilerBase
60 * array of vars which can be compiled in local scope
64 public $local_var = array();
69 * @param string $lexer_class class name
70 * @param string $parser_class class name
71 * @param Smarty $smarty global instance
73 public function __construct($lexer_class, $parser_class, $smarty)
75 $this->smarty = $smarty;
76 parent::__construct();
77 // get required plugins
78 $this->lexer_class = $lexer_class;
79 $this->parser_class = $parser_class;
83 * method to compile a Smarty template
85 * @param mixed $_content template source
87 * @return bool true if compiling succeeded, false if it failed
89 protected function doCompile($_content)
91 /* here is where the compiling takes place. Smarty
92 tags in the templates are replaces with PHP code,
93 then written to compiled files. */
94 // init the lexer/parser to compile the template
95 $this->lex = new $this->lexer_class($_content, $this);
96 $this->parser = new $this->parser_class($this->lex, $this);
97 if ($this->inheritance_child) {
98 // start state on child templates
99 $this->lex->yypushstate(Smarty_Internal_Templatelexer::CHILDBODY);
101 if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
102 $mbEncoding = mb_internal_encoding();
103 mb_internal_encoding('ASCII');
108 if ($this->smarty->_parserdebug) {
109 $this->parser->PrintTrace();
110 $this->lex->PrintTrace();
112 // get tokens from lexer and parse them
113 while ($this->lex->yylex() && !$this->abort_and_recompile) {
114 if ($this->smarty->_parserdebug) {
115 echo "<pre>Line {$this->lex->line} Parsing {$this->parser->yyTokenName[$this->lex->token]} Token " .
116 htmlentities($this->lex->value) . "</pre>";
118 $this->parser->doParse($this->lex->token, $this->lex->value);
121 if ($this->abort_and_recompile) {
122 // exit here on abort
125 // finish parsing process
126 $this->parser->doParse(0, 0);
128 mb_internal_encoding($mbEncoding);
130 // check for unclosed tags
131 if (count($this->_tag_stack) > 0) {
133 list($openTag, $_data) = array_pop($this->_tag_stack);
134 $this->trigger_template_error("unclosed {$this->smarty->left_delimiter}" . $openTag . "{$this->smarty->right_delimiter} tag");
136 // return compiled code
137 // return str_replace(array("? >\n<?php","? ><?php"), array('',''), $this->parser->retvalue);
138 return $this->parser->retvalue;