示例#1
0
/**
 * Evaluates the given string as if it was a template
 *
 * Although this plugin is kind of optimized and will
 * not recompile your string each time, it is still not
 * a good practice to use it. If you want to have templates
 * stored in a database or something you should probably use
 * the Dwoo_Template_String class or make another class that
 * extends it
 * <pre>
 *  * var : the string to use as a template
 *  * assign : if set, the output of the template will be saved in this variable instead of being output
 * </pre>
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from the use of this software.
 *
 * @author     Jordi Boggiano <*****@*****.**>
 * @copyright  Copyright (c) 2008, Jordi Boggiano
 * @license    http://dwoo.org/LICENSE   Modified BSD License
 * @link       http://dwoo.org/
 * @version    1.0.0
 * @date       2008-10-23
 * @package    Dwoo
 */
function Dwoo_Plugin_eval(Dwoo_Core $dwoo, $var, $assign = null)
{
    if ($var == '') {
        return;
    }
    $tpl = new Dwoo_Template_String($var);
    $clone = $dwoo->get(null);
    $out = $clone->get($tpl, $dwoo->readVar('_parent'));
    if ($assign !== null) {
        $dwoo->assignInScope($out, $assign);
    } else {
        return $out;
    }
}
示例#2
0
/**
 * Inserts another template into the current one
 * <pre>
 *  * file : the resource name of the template
 *  * cache_time : cache length in seconds
 *  * cache_id : cache identifier for the included template
 *  * compile_id : compilation identifier for the included template
 *  * data : data to feed into the included template, it can be any array and will default to $_root (the current data)
 *  * assign : if set, the output of the included template will be saved in this variable instead of being output
 *  * rest : any additional parameter/value provided will be added to the data array
 * </pre>
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from the use of this software.
 *
 * @author     Jordi Boggiano <*****@*****.**>
 * @copyright  Copyright (c) 2008, Jordi Boggiano
 * @license    http://dwoo.org/LICENSE   Modified BSD License
 * @link       http://dwoo.org/
 * @version    1.1.0
 * @date       2009-07-18
 * @package    Dwoo
 */
function Dwoo_Plugin_include(Dwoo_Core $dwoo, $file, $cache_time = null, $cache_id = null, $compile_id = null, $data = '_root', $assign = null, array $rest = array())
{
    if ($file === '') {
        return;
    }
    if (preg_match('#^([a-z]{2,}):(.*)$#i', $file, $m)) {
        // resource:identifier given, extract them
        $resource = $m[1];
        $identifier = $m[2];
    } else {
        // get the current template's resource
        $resource = $dwoo->getTemplate()->getResourceName();
        $identifier = $file;
    }
    try {
        $include = $dwoo->templateFactory($resource, $identifier, $cache_time, $cache_id, $compile_id);
    } catch (Dwoo_Security_Exception $e) {
        return $dwoo->triggerError('Include : Security restriction : ' . $e->getMessage(), E_USER_WARNING);
    } catch (Dwoo_Exception $e) {
        return $dwoo->triggerError('Include : ' . $e->getMessage(), E_USER_WARNING);
    }
    if ($include === null) {
        return $dwoo->triggerError('Include : Resource "' . $resource . ':' . $identifier . '" not found.', E_USER_WARNING);
    } elseif ($include === false) {
        return $dwoo->triggerError('Include : Resource "' . $resource . '" does not support includes.', E_USER_WARNING);
    }
    if (is_string($data)) {
        $vars = $dwoo->readVar($data);
    } else {
        $vars = $data;
    }
    if (count($rest)) {
        $vars = $rest + $vars;
    }
    $clone = $dwoo->get(null);
    $out = $clone->get($include, $vars);
    if ($assign !== null) {
        $dwoo->assignInScope($out, $assign);
    }
    foreach ($clone->getReturnValues() as $name => $value) {
        $dwoo->assignInScope($value, $name);
    }
    if ($assign === null) {
        return $out;
    }
}
示例#3
0
    public function testHtmlFormat()
    {
        $tpl = new Dwoo_Template_String("<html><body><div><p>a<em>b</em>c<hr /></p><textarea>a\n  b</textarea></div></body><html>");
        $tpl->forceCompilation();
        $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
        $dwoo->addFilter('html_format', true);
        $this->assertEquals(str_replace("\r", '', <<<SNIPPET

<html>
<body>
\t<div>
\t\t<p>
\t\t\ta<em>b</em>c
\t\t\t<hr />
\t\t</p><textarea>a
  b</textarea>
\t</div>
</body>
<html>
SNIPPET
), $dwoo->get($tpl, array(), $this->compiler));
    }
示例#4
0
 public function testCustomClassPluginInstanceAsModifier()
 {
     $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
     $dwoo->addPlugin('CustomClassPlugin', array(new custom_class_plugin_obj(), 'call'));
     $tpl = new Dwoo_Template_String('{$foo=4}{$foo|CustomClassPlugin:5}');
     $tpl->forceCompilation();
     $this->assertEquals('20', $dwoo->get($tpl, array(), $this->compiler));
 }
示例#5
0
 public function testClearCacheOnTemplateClass()
 {
     $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
     $dwoo->setCacheTime(10);
     $tpl = new Dwoo_Template_String('foo{$foo}bar', null, 'cachetest2');
     $tpl->forceCompilation();
     $this->assertEquals("foo1bar", $dwoo->get($tpl, array('foo' => 1)));
     $this->assertEquals(true, $dwoo->isCached($tpl));
     $this->assertEquals("foo1bar", $dwoo->get($tpl, array('foo' => 1)));
     $this->assertEquals(false, $tpl->clearCache($dwoo, 10));
     $this->assertEquals(true, $tpl->clearCache($dwoo, -1));
     $this->assertEquals(false, $dwoo->isCached($tpl));
 }
示例#6
0
 public function testCallingMethodOnProperty()
 {
     $tpl = new Dwoo_Template_String('{getobj()->instance->Bar("hoy")}');
     $tpl->forceCompilation();
     $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
     $dwoo->addPlugin('getobj', array(new PluginHelper(), 'call'));
     $this->assertEquals('HOY', $dwoo->get($tpl, array(), $this->compiler));
 }