Example #1
0
 /**
  * {@inheritdoc}
  */
 public function render($viewName, Model $model, NotificationCenter $notificationCenter, $output = true)
 {
     Profile::start('Renderer', 'Generate HTML');
     $templateName = $viewName . '.' . static::$templateFileExtension;
     $dwoo = new Dwoo($this->compiledPath, $this->cachePath);
     $dwoo->getLoader()->addDirectory($this->functionsPath);
     Profile::start('Renderer', 'Create template file.');
     $template = new Dwoo_Template_File($templateName);
     $template->setIncludePath($this->getTemplatesPath());
     Profile::stop();
     Profile::start('Renderer', 'Render');
     $dwooData = new Dwoo_Data();
     $dwooData->setData($model->getData());
     $dwooData->assign('errorMessages', $notificationCenter->getErrors());
     $dwooData->assign('successMessages', $notificationCenter->getSuccesses());
     $this->setHeader('Content-type: text/html', $output);
     // I do never output directly from dwoo to have the possibility to show an error page if there was a render error.
     $result = $rendered = $dwoo->get($template, $dwooData, null, false);
     if ($output) {
         echo $result;
     }
     Profile::stop();
     Profile::stop();
     return $output ? null : $rendered;
 }
 public function __construct($file, $cacheTime = null, $cacheId = null, $compileId = null, $includePath = null)
 {
     global $THEME;
     $name = explode(':', $file, 2);
     // this is a mahara special resource, resolving path
     if (count($name) == 2) {
         list($file, $includePath) = $this->resolveFileName($name, $includePath);
     }
     parent::__construct($file, null, null, null, $includePath);
 }
Example #3
0
 public function testIncludePath()
 {
     // no include path
     $tpl = new Dwoo_Template_File('test.html');
     $this->assertEquals('test.html', $tpl->getResourceIdentifier());
     // include path in constructor
     $tpl = new Dwoo_Template_File('test.html', null, null, null, TEST_DIRECTORY . DIRECTORY_SEPARATOR . 'resources');
     $this->assertEquals(TEST_DIRECTORY . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'test.html', $tpl->getResourceIdentifier());
     // set include path as string
     $tpl->setIncludePath(TEST_DIRECTORY . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR);
     $this->assertThat($tpl->getResourceIdentifier(), new DwooConstraintPathEquals(TEST_DIRECTORY . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test.html'));
     // set include path as array
     $tpl->setIncludePath(array(TEST_DIRECTORY . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'subfolder2', TEST_DIRECTORY . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR));
     $this->assertThat($tpl->getResourceIdentifier(), new DwooConstraintPathEquals(TEST_DIRECTORY . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'subfolder2' . DIRECTORY_SEPARATOR . 'test.html'));
 }
 /**
  * Convert a Mahara plugin template file path into a normal template file path with extra search paths.
  *
  * @param string $pluginfile The plugintype, name, and name of file, e.g. "blocktype:clippy:index.tpl"
  * @param int $cacheTime Not used.
  * @param int $cacheId Not used.
  * @param int $compileId Not used.
  * @param array $includePath The paths to look in.
  * @throws MaharaException
  */
 public function __construct($file, $cacheTime = null, $cacheId = null, $compileId = null, $includePath = null)
 {
     global $THEME;
     $parts = explode(':', $file, 3);
     if (count($parts) !== 3) {
         throw new SystemException("Invalid template path \"{$file}\"");
     }
     // Keep the original string for logging purposes
     $dwooref = $file;
     list($plugintype, $pluginname, $file) = $parts;
     // Since we use $plugintype as part of a file path, we should whitelist it
     $plugintype = strtolower($plugintype);
     if (!in_array($plugintype, plugin_types())) {
         throw new SystemException("Invalid plugintype in Dwoo template \"{$dwooref}\"");
     }
     // Get the relative path for this particular plugin
     require_once get_config('docroot') . $plugintype . '/lib.php';
     $pluginpath = call_static_method(generate_class_name($plugintype), 'get_theme_path', $pluginname);
     // Because this is a plugin template file, we don't want to include any accidental matches against
     // core template files with the same name.
     $includePath = array();
     // First look for a local override.
     $includePath[] = get_config('docroot') . "local/theme/{$pluginpath}/templates";
     // Then look for files in a custom theme
     foreach ($THEME->inheritance as $theme) {
         $includePath[] = get_config('docroot') . "theme/{$theme}/{$pluginpath}/templates";
     }
     // Lastly look for files in the plugin itself
     foreach ($THEME->inheritance as $theme) {
         $includePath[] = get_config('docroot') . "{$pluginpath}/theme/{$theme}/templates";
         // For legacy purposes also look for the template file loose under the theme directory.
         $includePath[] = get_config('docroot') . "{$pluginpath}/theme/{$theme}";
     }
     // Now, we instantiate this as a standard Dwoo_Template_File class.
     // We're passing in $file, which is the relative path to the file, and
     // $includePath, which is an array of directories to search for $file in.
     // We let Dwoo figure out which one actually has it.
     parent::__construct($file, null, null, null, $includePath);
 }
Example #5
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;
         }
     }
 }
Example #6
0
 public function testIncludeParent()
 {
     $tpl = new Dwoo_Template_File(TEST_DIRECTORY . '/resources/subfolder/inctest.html');
     $tpl->forceCompilation();
     $this->assertEquals("43", $this->dwoo->get($tpl, array(), $this->compiler));
 }
 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;
         }
     }
 }
Example #8
0
 public function testSubTemplatesMultiInc()
 {
     $tpl = new Dwoo_Template_File(TEST_DIRECTORY . '/resources/templateUsage.html');
     $tpl->forceCompilation();
     $this->assertEquals("\n" . 'noparamoutput' . "\n", $this->dwoo->get($tpl, array(), $this->compiler));
     $this->assertEquals("\n" . 'noparamoutput' . "\n", $this->dwoo->get($tpl, array(), $this->compiler));
 }
Example #9
0
 public function testTemplateGetSet()
 {
     $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
     $dwoo->setCacheTime(10);
     $tpl = new Dwoo_Template_String('foo');
     $tpl2 = new Dwoo_Template_File('./resources/test.html');
     $this->assertEquals(false, $tpl->getResourceIdentifier());
     $this->assertEquals('string', $tpl->getResourceName());
     $this->assertEquals('file', $tpl2->getResourceName());
     $this->assertEquals(hash('md4', 'foo'), $tpl->getUid());
 }
Example #10
0
 public function testExtendsMultiple()
 {
     $tpl = new Dwoo_Template_File(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'extend2.html');
     $tpl->forceCompilation();
     $this->assertThat($this->dwoo->get($tpl, array('foo' => 'bar'), $this->compiler), new DwooConstraintStringEquals("foo\nchild1\ntoplevelContent1child2\nbar\nFOObartoplevelContent2\nbaz"));
 }