Development

Changeset 22543

You must first sign up to be able to contribute.

Changeset 22543

Show
Ignore:
Timestamp:
09/28/09 14:14:10 (4 years ago)
Author:
fabien
Message:

[templating] changed the interface of the loaders and renderers to only accept sfTemplateStorage instances (strings were previously allowed)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • components/templating/trunk/doc/04-Template-Loaders.markdown

    r22215 r22543  
    140140>abstract base class, which provides a debugger interface. 
    141141 
    142 The ~`load()`~ method must return one of the following: 
    143  
    144   * A string representing the template; 
    145  
    146   * A `sfTemplateStorage` instance. 
     142The ~`load()`~ method must return a `sfTemplateStorage` instance. 
    147143 
    148144### Template ~Storages|storage~ 
     
    185181            } 
    186182 
    187             return $rows[0][0]
     183            return new sfTemplateStorageString($rows[0][0])
    188184          } 
    189185        } 
     
    233229        if (method_exists($this, $method = 'getTemplate'.$template)) 
    234230        { 
    235           return $this->$method(); 
     231          return new sfTemplateStorageString($this->$method()); 
    236232        } 
    237233 
  • components/templating/trunk/doc/05-Template-Renderers.markdown

    r22215 r22543  
    4343    interface sfTemplateEngineInterface 
    4444    { 
    45       function evaluate($template, array $parameters = array()); 
     45      function evaluate(sfTemplateStorage $template, array $parameters = array()); 
    4646 
    4747      function setEngine(sfTemplateEngine $engine); 
     
    5353>automatically call the engine methods. 
    5454 
    55 The `$template` variable can be any of the following: 
    56  
    57   * A string representing the template; 
    58  
    59   * A `sfTemplateStorage` instance. 
     55The `$template` must be a `sfTemplateStorage` instance. 
    6056 
    6157>**NOTE** 
     
    6864 
    6965    [php] 
    70     public function evaluate($template, array $parameters = array()) 
     66    public function evaluate(sfTemplateStorage $template, array $parameters = array()) 
    7167    { 
    7268      if ($template instanceof sfTemplateStorageFile) 
     
    7874        return ob_get_clean(); 
    7975      } 
    80       else if (is_string($template) || $template instanceof sfTemplateStorageString) 
     76      elseif ($template instanceof sfTemplateStorageString) 
    8177      { 
    8278        extract($parameters); 
     
    106102    class ProjectTemplateRenderer extends sfTemplateRenderer 
    107103    { 
    108       public function evaluate($template, array $parameters = array()) 
     104      public function evaluate(sfTemplateStorage $template, array $parameters = array()) 
    109105      { 
    110106        if ($template instanceof sfTemplateStorageFile) 
     
    126122In the `evaluate()` method, we get the content of the template on the 
    127123filesystem if the template is an instance of `sfTemplateStorageFile`. If not, 
    128 the template is a string or an object with a `__toString()` method. 
     124the template is an object with a `__toString()` method that returns the 
     125content of the template. 
    129126 
    130127>**NOTE** 
     
    151148      } 
    152149 
    153       public function evaluate($template, array $parameters = array()) 
     150      public function evaluate(sfTemplateStorage $template, array $parameters = array()) 
    154151      { 
    155152        if ($template instanceof sfTemplateStorageFile) 
     
    157154          $this->tal->setTemplate((string) $template); 
    158155        } 
    159         else if (is_string($template) || $template instanceof sfTemplateStorageString) 
     156        elseif ($template instanceof sfTemplateStorageString) 
    160157        { 
    161158          $this->tal->setSource((string) $template); 
  • components/templating/trunk/lib/sfTemplateEngine.php

    r22215 r22543  
    9292 
    9393    // renderer 
    94     if ($template instanceof sfTemplateStorage && $template->getRenderer()) 
     94    if ($template->getRenderer()) 
    9595    { 
    9696      $renderer = $template->getRenderer(); 
  • components/templating/trunk/lib/sfTemplateLoaderCache.php

    r22215 r22543  
    4646   * @param string $renderer The renderer to use 
    4747   * 
    48    * @return string|Boolean false if the template cannot be loaded, the loaded template otherwise 
     48   * @return sfTemplateStorage|Boolean false if the template cannot be loaded, a sfTemplateStorage instance otherwise 
    4949   */ 
    5050  public function load($template, $renderer = 'php') 
  • components/templating/trunk/lib/sfTemplateLoaderChain.php

    r22215 r22543  
    5252   * @param string $renderer The renderer to use 
    5353   * 
    54    * @return string|Boolean false if the template cannot be loaded, the loaded template otherwise 
     54   * @return sfTemplateStorage|Boolean false if the template cannot be loaded, a sfTemplateStorage instance otherwise 
    5555   */ 
    5656  public function load($template, $renderer = 'php') 
     
    5858    foreach ($this->loaders as $loader) 
    5959    { 
    60       if (false !== $content = $loader->load($template, $renderer)) 
     60      if (false !== $ret = $loader->load($template, $renderer)) 
    6161      { 
    62         return $content; 
     62        return $ret; 
    6363      } 
    6464    } 
  • components/templating/trunk/lib/sfTemplateLoaderFilesystem.php

    r22215 r22543  
    4444   * @param string $renderer The renderer to use 
    4545   * 
    46    * @return string|Boolean false if the template cannot be loaded, the loaded template otherwise 
     46   * @return sfTemplateStorage|Boolean false if the template cannot be loaded, a sfTemplateStorage instance otherwise 
    4747   */ 
    4848  public function load($template, $renderer = 'php') 
  • components/templating/trunk/lib/sfTemplateLoaderInterface.php

    r22215 r22543  
    2626   * @param string $renderer The renderer to use 
    2727   * 
    28    * @return string|Boolean false if the template cannot be loaded, the loaded template otherwise 
     28   * @return sfTemplateStorage|Boolean false if the template cannot be loaded, a sfTemplateStorage instance otherwise 
    2929   */ 
    3030  function load($template, $renderer = 'php'); 
  • components/templating/trunk/lib/sfTemplateRendererInterface.php

    r22215 r22543  
    2323   * Evaluates a template. 
    2424   * 
    25    * @param mixed $template   The template to render 
    26    * @param array $parameters An array of parameters to pass to the template 
     25   * @param sfTemplateStorage $template   The template to render 
     26   * @param array             $parameters An array of parameters to pass to the template 
    2727   * 
    2828   * @return string|false The evaluated template, or false if the renderer is unable to render the template 
    2929   */ 
    30   function evaluate($template, array $parameters = array()); 
     30  function evaluate(sfTemplateStorage $template, array $parameters = array()); 
    3131 
    3232  /** 
  • components/templating/trunk/lib/sfTemplateRendererPhp.php

    r22215 r22543  
    2323   * Evaluates a template. 
    2424   * 
    25    * @param mixed $template   The template to render 
    26    * @param array $parameters An array of parameters to pass to the template 
     25   * @param sfTemplateStorage $template   The template to render 
     26   * @param array             $parameters An array of parameters to pass to the template 
    2727   * 
    2828   * @return string|false The evaluated template, or false if the renderer is unable to render the template 
    2929   */ 
    30   public function evaluate($template, array $parameters = array()) 
     30  public function evaluate(sfTemplateStorage $template, array $parameters = array()) 
    3131  { 
    3232    if ($template instanceof sfTemplateStorageFile) 
     
    3838      return ob_get_clean(); 
    3939    } 
    40     else if (is_string($template) || $template instanceof sfTemplateStorageString) 
     40    else if ($template instanceof sfTemplateStorageString) 
    4141    { 
    4242      extract($parameters); 
  • components/templating/trunk/lib/sfTemplateStorage.php

    r22215 r22543  
    4242  public function __toString() 
    4343  { 
    44     return $this->template; 
     44    return (string) $this->template; 
    4545  } 
    4646 
  • components/templating/trunk/test/unit/sfTemplateEngineTest.php

    r22215 r22543  
    1515require_once dirname(__FILE__).'/lib/SimpleHelper.php'; 
    1616 
    17 $t = new lime_test(35); 
     17$t = new lime_test(33); 
    1818 
    1919class ProjectTemplateEngine extends sfTemplateEngine 
     
    5151    if (isset($this->templates[$template.'.'.$renderer])) 
    5252    { 
    53       return $this->templates[$template.'.'.$renderer]
     53      return new sfTemplateStorageString($this->templates[$template.'.'.$renderer])
    5454    } 
    5555 
     
    192192$t->is($engine->render('foo', array('foo' => 'foo', 'bar' => 'bar')), 'bar-foo-', '->render() supports render() calls in templates'); 
    193193 
    194 $loader->setTemplate('foobar.php', new stdClass()); 
    195 try 
    196 { 
    197   $engine->render('foobar'); 
    198   $t->fail('->render() throws a RuntimeException if the template cannot be rendered'); 
    199 } 
    200 catch (RuntimeException $e) 
    201 { 
    202   $t->pass('->render() throws a RuntimeException if the template cannot be rendered'); 
    203 } 
    204  
    205 $loader->setTemplate('foobarbar.php', '<?php echo $this->extend("foobar") ?>'); 
    206 try 
    207 { 
    208   $engine->render('foobarbar'); 
    209   $t->fail('->render() throws a RuntimeException if the template cannot be rendered'); 
    210 } 
    211 catch (RuntimeException $e) 
    212 { 
    213   $t->pass('->render() throws a RuntimeException if the template cannot be rendered'); 
    214 } 
    215  
    216194class CompilableTemplateLoader extends sfTemplateLoader implements sfTemplateLoaderCompilableInterface 
    217195{ 
     
    229207class FooTemplateRenderer extends sfTemplateRenderer 
    230208{ 
    231   public function evaluate($template, array $parameters = array()) 
     209  public function evaluate(sfTemplateStorage $template, array $parameters = array()) 
    232210  { 
    233211    return 'foo'; 
  • components/templating/trunk/test/unit/sfTemplateRendererPhpTest.php

    r22215 r22543  
    1414sfTemplateAutoloader::register(); 
    1515 
    16 $t = new lime_test(4); 
     16$t = new lime_test(2); 
    1717 
    1818$renderer = new sfTemplateRendererPhp(); 
     
    2020// ->evaluate() 
    2121$t->diag('->evaluate()'); 
    22 $t->ok(false === $renderer->evaluate(new stdClass()), '->evaluate() returns false if it is not able to render the template'); 
    2322 
    2423$template = new sfTemplateStorageString('<?php echo $foo ?>'); 
    2524$t->is($renderer->evaluate($template, array('foo' => 'bar')), 'bar', '->evaluate() renders templates that are instances of sfTemplateStorageString'); 
    2625 
    27 $template = '<?php echo $foo ?>'; 
    28 $t->is($renderer->evaluate($template, array('foo' => 'bar')), 'bar', '->evaluate() renders templates that are simple strings'); 
    29  
    3026$template = new sfTemplateStorageFile(dirname(__FILE__).'/fixtures/templates/foo.php'); 
    3127$t->is($renderer->evaluate($template, array('foo' => 'bar')), 'bar', '->evaluate() renders templates that are instances of sfTemplateStorageFile'); 
  • components/templating/trunk/test/unit/sfTemplateRendererTest.php

    r22215 r22543  
    2424  } 
    2525 
    26   public function evaluate($template, array $parameters = array()) 
     26  public function evaluate(sfTemplateStorage $template, array $parameters = array()) 
    2727  { 
    2828  }