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
53 * array of vars which can be compiled in local scope
57 public $local_var = array();
62 * @param string $lexer_class class name
63 * @param string $parser_class class name
64 * @param Smarty $smarty global instance
66 public function __construct($lexer_class, $parser_class, $smarty)
68 $this->smarty
= $smarty;
69 parent
::__construct();
70 // get required plugins
71 $this->lexer_class
= $lexer_class;
72 $this->parser_class
= $parser_class;
76 * method to compile a Smarty template
78 * @param mixed $_content template source
80 * @return bool true if compiling succeeded, false if it failed
82 protected function doCompile($_content, $isTemplateSource = false)
84 /* here is where the compiling takes place. Smarty
85 tags in the templates are replaces with PHP code,
86 then written to compiled files. */
87 // init the lexer/parser to compile the template
88 $this->lex
= new $this->lexer_class(str_replace(array("\r\n", "\r"), "\n", $_content), $this);
89 $this->parser
= new $this->parser_class($this->lex
, $this);
90 if ($isTemplateSource) {
91 $this->parser
->insertPhpCode("<?php\n\$_smarty_tpl->properties['nocache_hash'] = '{$this->nocache_hash}';\n?>\n");
93 if ($this->inheritance_child
) {
94 // start state on child templates
95 $this->lex
->yypushstate(Smarty_Internal_Templatelexer
::CHILDBODY
);
97 if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
98 $mbEncoding = mb_internal_encoding();
99 mb_internal_encoding('ASCII');
104 if ($this->smarty
->_parserdebug
) {
105 $this->parser
->PrintTrace();
106 $this->lex
->PrintTrace();
108 // get tokens from lexer and parse them
109 while ($this->lex
->yylex() && !$this->abort_and_recompile
) {
110 if ($this->smarty
->_parserdebug
) {
111 echo "<pre>Line {$this->lex->line} Parsing {$this->parser->yyTokenName[$this->lex->token]} Token " .
112 htmlentities($this->lex
->value
) . "</pre>";
114 $this->parser
->doParse($this->lex
->token
, $this->lex
->value
);
117 if ($this->abort_and_recompile
) {
118 // exit here on abort
121 // finish parsing process
122 $this->parser
->doParse(0, 0);
124 mb_internal_encoding($mbEncoding);
126 // check for unclosed tags
127 if (count($this->_tag_stack
) > 0) {
129 list($openTag, $_data) = array_pop($this->_tag_stack
);
130 $this->trigger_template_error("unclosed {$this->smarty->left_delimiter}" . $openTag . "{$this->smarty->right_delimiter} tag");
132 // return compiled code
133 // return str_replace(array("? >\n<?php","? ><?php"), array('',''), $this->parser->retvalue);
134 return $this->parser
->retvalue
;