smarty_internal_smartytemplatecompiler.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Smarty Template Compiler Base
  4. *
  5. * This file contains the basic classes and methodes for compiling Smarty templates with lexer/parser
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * @ignore
  13. */
  14. include ("smarty_internal_parsetree.php");
  15. /**
  16. * Class SmartyTemplateCompiler
  17. *
  18. * @package Smarty
  19. * @subpackage Compiler
  20. */
  21. class Smarty_Internal_SmartyTemplateCompiler extends Smarty_Internal_TemplateCompilerBase {
  22. /**
  23. * Lexer class name
  24. *
  25. * @var string
  26. */
  27. public $lexer_class;
  28. /**
  29. * Parser class name
  30. *
  31. * @var string
  32. */
  33. public $parser_class;
  34. /**
  35. * Lexer object
  36. *
  37. * @var object
  38. */
  39. public $lex;
  40. /**
  41. * Parser object
  42. *
  43. * @var object
  44. */
  45. public $parser;
  46. /**
  47. * Smarty object
  48. *
  49. * @var object
  50. */
  51. public $smarty;
  52. /**
  53. * array of vars which can be compiled in local scope
  54. *
  55. * @var array
  56. */
  57. public $local_var = array();
  58. /**
  59. * Initialize compiler
  60. *
  61. * @param string $lexer_class class name
  62. * @param string $parser_class class name
  63. * @param Smarty $smarty global instance
  64. */
  65. public function __construct($lexer_class, $parser_class, $smarty)
  66. {
  67. $this->smarty = $smarty;
  68. parent::__construct();
  69. // get required plugins
  70. $this->lexer_class = $lexer_class;
  71. $this->parser_class = $parser_class;
  72. }
  73. /**
  74. * Methode to compile a Smarty template
  75. *
  76. * @param mixed $_content template source
  77. * @return bool true if compiling succeeded, false if it failed
  78. */
  79. protected function doCompile($_content)
  80. {
  81. /* here is where the compiling takes place. Smarty
  82. tags in the templates are replaces with PHP code,
  83. then written to compiled files. */
  84. // init the lexer/parser to compile the template
  85. $this->lex = new $this->lexer_class($_content, $this);
  86. $this->parser = new $this->parser_class($this->lex, $this);
  87. if ($this->smarty->_parserdebug)
  88. $this->parser->PrintTrace();
  89. // get tokens from lexer and parse them
  90. while ($this->lex->yylex() && !$this->abort_and_recompile) {
  91. if ($this->smarty->_parserdebug) {
  92. echo "<pre>Line {$this->lex->line} Parsing {$this->parser->yyTokenName[$this->lex->token]} Token " .
  93. htmlentities($this->lex->value) . "</pre>";
  94. }
  95. $this->parser->doParse($this->lex->token, $this->lex->value);
  96. }
  97. if ($this->abort_and_recompile) {
  98. // exit here on abort
  99. return false;
  100. }
  101. // finish parsing process
  102. $this->parser->doParse(0, 0);
  103. // check for unclosed tags
  104. if (count($this->_tag_stack) > 0) {
  105. // get stacked info
  106. list($openTag, $_data) = array_pop($this->_tag_stack);
  107. $this->trigger_template_error("unclosed {" . $openTag . "} tag");
  108. }
  109. // return compiled code
  110. // return str_replace(array("? >\n<?php","? ><?php"), array('',''), $this->parser->retvalue);
  111. return $this->parser->retvalue;
  112. }
  113. }
  114. ?>