smarty_internal_smartytemplatecompiler.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. require_once("smarty_internal_parsetree.php");
  12. /**
  13. * Class SmartyTemplateCompiler
  14. */
  15. class Smarty_Internal_SmartyTemplateCompiler extends Smarty_Internal_TemplateCompilerBase {
  16. // array of vars which can be compiled in local scope
  17. public $local_var = array();
  18. /**
  19. * Initialize compiler
  20. */
  21. public function __construct($lexer_class, $parser_class, $smarty)
  22. {
  23. $this->smarty = $smarty;
  24. parent::__construct();
  25. // get required plugins
  26. $this->lexer_class = $lexer_class;
  27. $this->parser_class = $parser_class;
  28. }
  29. /**
  30. * Methode to compile a Smarty template
  31. *
  32. * @param $_content template source
  33. * @return bool true if compiling succeeded, false if it failed
  34. */
  35. protected function doCompile($_content)
  36. {
  37. /* here is where the compiling takes place. Smarty
  38. tags in the templates are replaces with PHP code,
  39. then written to compiled files. */
  40. // init the lexer/parser to compile the template
  41. $this->lex = new $this->lexer_class($_content, $this);
  42. $this->parser = new $this->parser_class($this->lex, $this);
  43. if (isset($this->smarty->_parserdebug)) $this->parser->PrintTrace();
  44. // get tokens from lexer and parse them
  45. while ($this->lex->yylex() && !$this->abort_and_recompile) {
  46. if (isset($this->smarty->_parserdebug)) echo "<pre>Line {$this->lex->line} Parsing {$this->parser->yyTokenName[$this->lex->token]} Token " . htmlentities($this->lex->value) . "</pre>";
  47. $this->parser->doParse($this->lex->token, $this->lex->value);
  48. }
  49. if ($this->abort_and_recompile) {
  50. // exit here on abort
  51. return false;
  52. }
  53. // finish parsing process
  54. $this->parser->doParse(0, 0);
  55. // check for unclosed tags
  56. if (count($this->_tag_stack) > 0) {
  57. // get stacked info
  58. list($_open_tag, $_data) = array_pop($this->_tag_stack);
  59. $this->trigger_template_error("unclosed {" . $_open_tag . "} tag");
  60. }
  61. // return compiled code
  62. // return str_replace(array("? >\n<?php","? ><?php"), array('',''), $this->parser->retvalue);
  63. return $this->parser->retvalue;
  64. }
  65. }
  66. ?>