/framework/vendor/smarty3/lib/libs/sysplugins/smarty_internal_smartytemplatecompiler.php

http://zoop.googlecode.com/ · PHP · 74 lines · 35 code · 5 blank · 34 comment · 8 complexity · 57a0623a9b81969bbf98a875fce5eaf6 MD5 · raw file

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