Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
View_Smarty
  • Hi, i forked the fuel project and created a simple Smarty_View class extends from the original View. So you can use your Smarty_View class same as the origi View. Of course you have to use the default smarty curly brackets, and your templates have to be a tpl extension. heres my fork: https://github.com/b3ha/fuel
  • There's a fuel-parser package that support pluggable parsers (https://github.com/fuel/parser). It comes with support for Dwoo, Twig, Simpletags, Moustache and Jade. Extending it is very simple. Maybe you could fork the repo, and add Smarty support to it instead of building something stand-alone?
  • Yap, youre right, thx. I forked it and added Smarty configurations. Maybe it worth a pull request.
    Here it is: https://github.com/b3ha/parser
  • Looks fine. Send a pull request, I'm pretty sure Jelmer will accept it.
  • I've used PHP exclusively in my views since coming to Fuel and I must admit that the code is pretty cluttered. I read somwhere (??) that PHP's native performance is superior to ANY template engine.
    Prior to Fuel, I was a big fan of Smarty. Just wondering, is there much of a performance hit with Smarty's use?
  • Great job!
    Shouldn't there be something like:
    'include' => PKGPATH.'parser'.DS.'vendor'.DS.'Smarty'.DS.'Smarty.class.php',
    instead of:
    'include' => APPPATH.'vendor'.DS.'Smarty'.DS.'libs'.DS.'Smarty.class.php',
    in config file?
  • Michael Gorynia wrote on Sunday 3rd of July 2011:
    Great job!
    Shouldn't there be something like:
    'include' => PKGPATH.'parser'.DS.'vendor'.DS.'Smarty'.DS.'Smarty.class.php',
    instead of:
    'include' => APPPATH.'vendor'.DS.'Smarty'.DS.'libs'.DS.'Smarty.class.php',
    in config file?

    Thanks.
    I was also thinking about the PKGPATH, but im trying to oriented to the parser config (almost all the libs are in the APPPATH). Anyway its not a bad idea, but imho the Smarty lib should not be a part of the package.
  • Paul Boco wrote on Sunday 3rd of July 2011:
    I've used PHP exclusively in my views since coming to Fuel and I must admit that the code is pretty cluttered. I read somwhere (??) that PHP's native performance is superior to ANY template engine.
    Prior to Fuel, I was a big fan of Smarty. Just wondering, is there much of a performance hit with Smarty's use?

    Its simple, becouse Smarty add another abstraction layer. Smarty compiling, render... all need extra work, but! your business and view logic will be separated, it makes simply better code.
  • @Vanitas, PKGPATH is for template engines included in the package, APPPATH for template engine you'll have to add yourself.
    I agree with @b3ha that Smarty should be in APPPATH, we will never include it by default in the package. As to speed, template engines come in two categories: compilers and interpreters. The compilers (like for example Smarty) run some checks on template timestamps, to see if a template needs to be compiled. This takes some time and slows your application down a bit. Most people have this active during development. The end result is a PHP file, which (depending on the quality of the compiler) is as fast as a plain PHP view file. In production, this behaviour is usually disabled (you don't change your code in production) so the delay is minimal. Interpreters, like for example Simpletags, parse and process the view file at runtime. This method is only useful for simple operations, like some regex based replacements. More complex operations would cause an unacceptable delay. The biggest advantage of a template engine lies in the fact that you have a better front-end/back-end separation (front-end developers can't access your PHP core), front-end developers don't have to learn PHP, you'll get cleaner and easier to read view files, and some provide more highlevel functionality which would require a lot of PHP to achieve the same.
  • 1) Cloned a fresh copy of Fuel
    2) Checked out the develop branch
    3) Cloned parser into fuel/packages and retained the name 'parser'
    4) Copied dir 'Smarty-3.0.8' to fuel/app/vendor and renamed to 'Smarty'
    5) Added 'parser' to fuel/app/config/config.php as an always loaded package. i.e. ...'always_load' => array('packages' => array('parser')...
    6) Created directories fuel/app/tmp/Smarty/templates_c (made writable) and fuel/app/tmp/Smarty/configs
    7) Created directory fuel/app/cache/Smarty and made it writable
    Note: Steps 6 & 7 were not in the documentation and may not be necessary.
    8) Changed Controller_Welcome::action_index() to:
    public function action_index()
    {
        $data['foo'] = 'Success';
        $this->response->body = View::factory('welcome/index.tpl', $data);
    }
    
    9) Created file fuel/app/views/welcome/index.tpl with the following contents
    <!DOCTYPE html>
    <html>
    <head>
     <title>Smarty Test</title>
    </head>
    <body>
    The value of $foo is [{$foo}]
    </body>
    </html>
    
    10) Deleted file fuel/app/views/welcome/index.php When I request welcome/index I get the following:
    yppBU.jpg What am I missing?
  • That doesn't work, because .tpl is currently configured for the Dwoo engine. In the config file, no specific extension has been defined for Smarty, so Smarty templates need a .smarty extension. If you want to use .tpl, define it in the config, and remove it from the Dwoo definition (you can't map an extension to multiple engines).
  • Thanks for your reply, WanWizard.
    I renamed the view to index.smarty and changed it accordingly in Controller_Welcome::action_index() as per your instructions.
    i.e.
    public function action_index()
    {
            $data['foo'] = 'Success';
            $this->response->body = View::factory('welcome/index.smarty', $data);
    }
    

    But, I am getting the same error as in my last post. Any help would be appreciated.
  • I may have found the problem.
    I'm wondering if line 38 in fuel/packages/parser/classes/view/smarty.php should be:
    public $extension = 'smarty';
    
    rather than:
    public $extension = 'tpl';
    
    I notice that the assignments in the other files in fuel/packages/parser/classes/view/ correspond with the keys in fuel/packages/parser/config/parser.php. When I do change it to $extension = 'smarty', everything works fine.
    Just a guess.

    EDIT
    The problem was me again. I used the following the the parser config and everything works great.
    'tpl' => array('class' => 'View_Smarty', 'extension' => '.tpl')
    
  • You are right, the default extension should be 'smarty'. I'll see if I can correct that today. edit: done that.
  • Harro Verton wrote on Monday 4th of July 2011:
    You are right, the default extension should be 'smarty'. I'll see if I can correct that today. edit: done that.

    It was my fault, thanks for the correction. But eg netbeans 7.0 theres a smarty plugin, which works with .tpl extension. I hope i can switch in netbeans/tools the smarty plugin's extension (i didnt try it ago, maybe this week i check it).
  • Problem here is that multiple template engines use the extension .tpl, and you can only assign it to one. Currently, .tpl is assigned to Dwoo, which means you can't use it for Smarty. But nobody's stopping you from copying the parser.php config file to app/config, and assign .tpl to Smarty, like @PaulboCo described.
  • Does it work with Inheritance? I got error when tried to use it:
    Fatal error: Method Parser\View_Smarty::__toString() must not throw an exception in /opt/lampp/htdocs/index.php on line 49 49th line in index.php is:
    $response->body(str_replace(array('{exec_time}', '{mem_usage}'), array(round($bm[0], 4), round($bm[1] / pow(1024, 2), 3)), $response->body()));
  • That looks like quite an old version of Fuel. The parser package needs at least RC2, probably RC3.
  • I have RC3.
    When I don't have any variable in template (only some html) this error doesn't appear.
    But then I have another error with Inheritance.
    I'm trying to load dashboard.smarty template which extends template.smarty (both in the same folder):
    Screenshot
  • Maybe your core is, but your index.php definately contains old code. the $response->body() line will overwrite anything your response object has produced. Somehow that line contains a view object (can't see the entire line), which triggers the render method without being properly configured, which in turn leads to Smarty failing to find a view...

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion