From b12d2f3df3afcef043a92eb61cc76bd1c8e10b14 Mon Sep 17 00:00:00 2001 From: fabien Date: Tue, 20 Oct 2009 12:00:02 +0000 Subject: [PATCH] made a speed optimization for template inclusion and when including the same template over and over again git-svn-id: http://svn.twig-project.org/trunk@84 93ef8e89-cb99-4229-a87c-7fa0fa45744b --- lib/Twig/Environment.php | 32 ++++++++++++++++++++++++++------ lib/Twig/Node/Module.php | 1 - 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index ed1442a..98eb962 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -25,6 +25,8 @@ class Twig_Environment protected $parsers; protected $transformers; protected $filters; + protected $runtimeInitialized; + protected $loadedTemplates; public function __construct(Twig_LoaderInterface $loader = null, $options = array()) { @@ -33,11 +35,12 @@ class Twig_Environment $this->setLoader($loader); } - $this->debug = isset($options['debug']) ? (bool) $options['debug'] : false; - $this->trimBlocks = isset($options['trim_blocks']) ? (bool) $options['trim_blocks'] : false; - $this->charset = isset($options['charset']) ? $options['charset'] : 'UTF-8'; - $this->baseTemplateClass = isset($options['base_template_class']) ? $options['base_template_class'] : 'Twig_Template'; - $this->extensions = array(new Twig_Extension_Core()); + $this->debug = isset($options['debug']) ? (bool) $options['debug'] : false; + $this->trimBlocks = isset($options['trim_blocks']) ? (bool) $options['trim_blocks'] : false; + $this->charset = isset($options['charset']) ? $options['charset'] : 'UTF-8'; + $this->baseTemplateClass = isset($options['base_template_class']) ? $options['base_template_class'] : 'Twig_Template'; + $this->extensions = array(new Twig_Extension_Core()); + $this->runtimeInitialized = false; } public function getBaseTemplateClass() @@ -77,9 +80,26 @@ class Twig_Environment public function loadTemplate($name) { + if (!$this->runtimeInitialized) + { + $this->initRuntime(); + + $this->runtimeInitialized = true; + } + + if (isset($this->loadedTemplates[$name])) + { + return $this->loadedTemplates[$name]; + } + $cls = $this->getLoader()->load($name, $this); - return new $cls($this); + return $this->loadedTemplates[$name] = new $cls($this); + } + + public function clearTemplateCache() + { + $this->loadedTemplates = array(); } public function getLexer() diff --git a/lib/Twig/Node/Module.php b/lib/Twig/Node/Module.php index f48e49d..5212d07 100644 --- a/lib/Twig/Node/Module.php +++ b/lib/Twig/Node/Module.php @@ -148,7 +148,6 @@ class Twig_Node_Module extends Twig_Node implements Twig_NodeListInterface } $compiler - ->write("\$this->env->initRuntime();\n\n") ->subcompile($this->body) ->outdent() ->write("}\n\n") -- 1.7.2.5