Question

I'd like to use Smarty in conjuction with the Zend Framework, especially some of it's View Helpers. Now i got to the point, where i implemented a Zend_View that uses Smarty to display templates. I can assign values as usual. So far so good.

Now I would really like to use Zend View Helpers in Smarty. I asssigned the Zend_View object as "this" and tried this in the template:

{$this->layout()->setLayout('default')}

As this will print the return value of the setLayout() method (which is a Zend_Layout), there is an error:

Catchable fatal error: Object of class Zend_Layout could not be converted to string in /path/to/templates_c/089c3d67082722c7cabc028fa92a077f8d8b4af5.file.default.tpl.cache.php on line 27

This is clear to me, so I went into Smarty's core to fix this:

The generated code did look like this:

<?php 
    echo $_smarty_tpl->tpl_vars['this']
                     ->value->layout()
                     ->setLayout('default');
?>

And now it reads:

<?php
    $hack = $_smarty_tpl->tpl_vars['this']
                          ->value
                          ->layout()
                          ->setLayout('default');
    if( is_string($hack) ||
        ( is_object($hack) && method_exists($hack, '__toString') ) )
        echo $hack;
?>

Now this is probably the worst fix i can think of, for several reasons (Smarty compatibility loss, performance). Sadly, it's the only one. Is there a way to stop Smarty from trying to print the output of the expression? Also, i want the syntax to stay as intuitive as possible, and i don't want to write Smarty functions for all the Helpers, because I want to use this code with a third-party application (Pimcore) that might add new helpers.

Thanks in advance for any suggestions!

Was it helpful?

Solution

Some suggestions (only ideas, nothing so good):

  1. Create a __toString() in Zend_Layout who returns null or empty string (big/ugly/worse workaround).

  2. Create a variable modifier/filter who return null or empty string, so your call will be something like {$this->layout()->setLayout('default')|noreturn} (you can use it with other things too and noreturn can be called with an friendly name like definition or define to indicate the purpose of the instruction, but a workaround too)

  3. Using the assign to build a expression who set all this thing to another var (workaround too).

Maybe this can give you some good ideas =)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top