How to use Smarty with Cake (SmartyView)

This article is also available in the following languages:
By tclineks
The SmartyView package sits here:
http://cakeforge.org/snippet/detail.php?type=snippet&id=6 But without some significant baking-knowledge, new users may not know how to incorporte SmartyView.
Here's how.

Steps

1. Download a version of SmartyView from here: http://cakeforge.org/snippet/detail.php?type=snippet&id=6
  1. Save SmartyView as /(app)/views/smarty.php

2. Download Smarty

http://smarty.php.net/download.php Extract tarball so Smarty.class.php sits at /vendor/smarty/Smarty.class.php

3. Create "smarty/compile" and "smarty/cache" within your tmp dir

(/(app)/tmp/ by default) and make them writable by the apache user

4. Include in your controller

(in app_controller.php to do it app-wide) var $view = 'Smarty'; ܡםjwmnWjr٥)܉jrzw(uOO[[OH܎ \[\ɛI\^J X\R[ X\R]\ܚ\ N [ O to use Smarty templates and helpers.
You now have SmartyView installed!

Notes:

  1. /views/smarty_templates is parsed for smarty plugins : http://smarty.incutio.com/?page=SmartyPlugins
  2. SmartyView will fall back to .thtml templates for views.
  3. [li]By default Smarty views must reside in 'smarty' subdirectories -- see addendum for information regarding removing this requirement.

Examples:

Without Smarty Helpers all helpers are available through: {$helper->method()} ex:
{$html->input('User/username')} Smarty doesn't allow for 'array(' in the templates so to use calls requiring them you have to use a plugin to assign arrays as well (such as http://cakeforge.org/snippet/detail.php?type=snippet&id=13 or http://smarty.incutio.com/?page=AdvancedAssignPlugin).
This ends up looking like this (using array_assoc from cakeforge):
{assign_assoc var='ArrayName' value='size=>40,class=>classname'}
<p>Title: {$html->input('Post/title', $ArrayName)}</p>

Alternatively, you can use the Smarty Helpers http://bakery.cakephp.org/articles/view/138. The Smarty Helpers are Smarty wrappers for existing helpers so you can call helper methods 'natively' in smarty. Note that many helper methods have not been wrapped yet - the current code serves as an example.

With Smarty Helpers all helpers are available in native Smarty style:     {html func=css path="cake.generic"}
    {javascript func=link url="lib/prototype"}
    {html func=url url="/admin/logout"}
    {html func=input fieldName=User/username class="test" size=30 }
For html func=input above, fieldName is required while all other parameters are passed as $htmlAttributes.


Enjoy!

Addendum:

note:

This line within the constructor ( __construct ) :

$this->subDir = 'smarty'.DS; requires view templates to be within a subdirectory (i.e. /app/views/posts/smarty/index.tpl)

Commenting this line removes this requirement.

This will not cause problems unless you are using another inherited view class that uses '.tpl' as its extension.

Comments

  • Posted 05/03/11 05:18:03 PM
    To speed up Smarty development, you need to enable the Smarty Debug Console. Here are steps on how to achieve that:

    http://blog.anas-mughal.com/?p=28
  • Posted 02/09/11 05:00:34 PM
    I tried both plugins, even validated them both, but there's problem with them - no actual value is being assigned. What could be wrong?
    • Posted 02/09/11 05:02:29 PM
      [quote] I tried both plugins, even validated them both, but there's problem with them - no actual value is being assigned. What could be wrong?
      [end quote]
      I meant smarty plugins assign_assoc and assign_adv - they fail to assign values.
  • Posted 10/17/10 05:39:40 PM
    i need more explain,i cannt read step 4
    and if there anther link explain how 2 use smarty with cakePHP plz send it
  • Posted 09/13/10 07:22:31 AM
    I know that this is a old tutorial, BTW, it's a good article, but, how did you resolve the issue on cakephp debugging? I tried using View::element('sql_dump'), but smarty tells me that View isnt a unknown tag.

Comments are closed for articles over a year old