Пример #1
0
 public function testDynamic()
 {
     $preTime = time();
     $tpl = new Dwoo_Template_String('{$pre}{dynamic}{$pre}{/}', 10, 'testDynamic');
     $tpl->forceCompilation();
     $this->assertEquals($preTime . $preTime, $this->dwoo->get($tpl, array('pre' => $preTime), $this->compiler));
     sleep(1);
     $postTime = time();
     $this->assertEquals($preTime . $postTime, $this->dwoo->get($tpl, array('pre' => $postTime), $this->compiler));
     // fixes the init call not being called (which is normal)
     $fixCall = new Dwoo_Plugin_dynamic($this->dwoo);
     $fixCall->init('');
 }
Пример #2
0
 /**
  * returns the given template rendered using the provided data and optional compiler
  *
  * @param mixed $tpl template, can either be a Dwoo_ITemplate object (i.e. Dwoo_Template_File), a valid path to a template, or
  *                   a template as a string it is recommended to provide a Dwoo_ITemplate as it will probably make things faster,
  *                   especially if you render a template multiple times
  * @param mixed $data the data to use, can either be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array. if you're
  *                    rendering the template from cache, it can be left null
  * @param Dwoo_ICompiler $compiler the compiler that must be used to compile the template, if left empty a default
  *                                Dwoo_Compiler will be used.
  * @param bool $output flag that defines whether the function returns the output of the template (false, default) or echoes it directly (true)
  * @return string nothing or the template output if $output is false
  */
 public function get($_tpl, $data = array(), $_compiler = null, $_output = false)
 {
     // a render call came from within a template, so we need a new dwoo instance in order to avoid breaking this one
     if ($this->template instanceof Dwoo_ITemplate) {
         $clone = clone $this;
         return $clone->get($_tpl, $data, $_compiler, $_output);
     }
     // auto-create template if required
     if ($_tpl instanceof Dwoo_ITemplate) {
         // valid, skip
     } elseif (is_string($_tpl) && file_exists($_tpl)) {
         $_tpl = new Dwoo_Template_File($_tpl);
     } else {
         throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s first argument must be a Dwoo_ITemplate (i.e. Dwoo_Template_File) or a valid path to a template file', E_USER_NOTICE);
     }
     // save the current template, enters render mode at the same time
     // if another rendering is requested it will be proxied to a new Dwoo_Core(instance
     $this->template = $_tpl;
     // load data
     if ($data instanceof Dwoo_IDataProvider) {
         $this->data = $data->getData();
     } elseif (is_array($data)) {
         $this->data = $data;
     } elseif ($data instanceof ArrayAccess) {
         $this->data = $data;
     } else {
         throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s data argument must be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array', E_USER_NOTICE);
     }
     $this->globals['template'] = $_tpl->getName();
     $this->initRuntimeVars($_tpl);
     // try to get cached template
     $file = $_tpl->getCachedTemplate($this);
     $doCache = $file === true;
     $cacheLoaded = is_string($file);
     if ($cacheLoaded === true) {
         // cache is present, run it
         if ($_output === true) {
             include $file;
             $this->template = null;
         } else {
             ob_start();
             include $file;
             $this->template = null;
             return ob_get_clean();
         }
     } else {
         // no cache present
         if ($doCache === true) {
             $dynamicId = uniqid();
         }
         // render template
         $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler);
         $out = (include $compiledTemplate);
         // template returned false so it needs to be recompiled
         if ($out === false) {
             $_tpl->forceCompilation();
             $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler);
             $out = (include $compiledTemplate);
         }
         if ($doCache === true) {
             $out = preg_replace('/(<%|%>|<\\?php|<\\?|\\?>)/', '<?php /*' . $dynamicId . '*/ echo \'$1\'; ?>', $out);
             if (!class_exists('Dwoo_plugin_dynamic', false)) {
                 $this->getLoader()->loadPlugin('dynamic');
             }
             $out = Dwoo_Plugin_dynamic::unescape($out, $dynamicId, $compiledTemplate);
         }
         // process filters
         foreach ($this->filters as $filter) {
             if (is_array($filter) && $filter[0] instanceof Dwoo_Filter) {
                 $out = call_user_func($filter, $out);
             } else {
                 $out = call_user_func($filter, $this, $out);
             }
         }
         if ($doCache === true) {
             // building cache
             $file = $_tpl->cache($this, $out);
             // run it from the cache to be sure dynamics are rendered
             if ($_output === true) {
                 include $file;
                 // exit render mode
                 $this->template = null;
             } else {
                 ob_start();
                 include $file;
                 // exit render mode
                 $this->template = null;
                 return ob_get_clean();
             }
         } else {
             // no need to build cache
             // exit render mode
             $this->template = null;
             // output
             if ($_output === true) {
                 echo $out;
             }
             return $out;
         }
     }
 }
 public function get($_tpl, $data = array(), $_compiler = null, $_output = false)
 {
     if ($this->template instanceof Dwoo_ITemplate) {
         $proxy = clone $this;
         return $proxy->get($_tpl, $data, $_compiler, $_output);
     }
     if ($_tpl instanceof Dwoo_ITemplate) {
     } elseif (is_string($_tpl) && file_exists($_tpl)) {
         $_tpl = new Dwoo_Template_File($_tpl);
     } else {
         throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s first argument must be a Dwoo_ITemplate (i.e. Dwoo_Template_File) or a valid path to a template file', E_USER_NOTICE);
     }
     $this->template = $_tpl;
     if ($data instanceof Dwoo_IDataProvider) {
         $this->data = $data->getData();
     } elseif (is_array($data)) {
         $this->data = $data;
     } else {
         throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s data argument must be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array', E_USER_NOTICE);
     }
     $this->globals['template'] = $_tpl->getName();
     $this->initRuntimeVars($_tpl);
     $file = $_tpl->getCachedTemplate($this);
     $doCache = $file === true;
     $cacheLoaded = is_string($file);
     if ($cacheLoaded === true) {
         if ($_output === true) {
             include $file;
             $this->template = null;
         } else {
             ob_start();
             include $file;
             $this->template = null;
             return ob_get_clean();
         }
     } else {
         if ($doCache === true) {
             $dynamicId = uniqid();
         }
         $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler);
         $out = (include $compiledTemplate);
         if ($out === false) {
             $_tpl->forceCompilation();
             $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler);
             $out = (include $compiledTemplate);
         }
         if ($doCache === true) {
             $out = preg_replace('/(<%|%>|<\\?php|<\\?|\\?>)/', '<?php /*' . $dynamicId . '*/ echo \'$1\'; ?>', $out);
             if (!class_exists('Dwoo_plugin_dynamic', false)) {
                 $this->getLoader()->loadPlugin('dynamic');
             }
             $out = Dwoo_Plugin_dynamic::unescape($out, $dynamicId, $compiledTemplate);
         }
         foreach ($this->filters as $filter) {
             if (is_array($filter) && $filter[0] instanceof Dwoo_Filter) {
                 $out = call_user_func($filter, $out);
             } else {
                 $out = call_user_func($filter, $this, $out);
             }
         }
         if ($doCache === true) {
             $file = $_tpl->cache($this, $out);
             if ($_output === true) {
                 include $file;
                 $this->template = null;
             } else {
                 ob_start();
                 include $file;
                 $this->template = null;
                 return ob_get_clean();
             }
         } else {
             $this->template = null;
             if ($_output === true) {
                 echo $out;
             }
             return $out;
         }
     }
 }