sfSmartyPlugin - 0.2.2

symfony smarty template engine plugin

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


« Back to the Plugins Home

Signin


Forgot your password?
Create an account

Tools

Stats

advanced search
Information Readme Releases Changelog Contribute
Show source | Show as Markdown

sfSmarty plugin

Overview

This plugin gives you the ability to use the Smarty templating engine for your view templates. http://www.smarty.net

Installation

To install sfSmartyPlugin:

Checkout the plugin from the SVN repository http://svn.symfony-project.com/plugins/sfSmartyPlugin/trunk

Its currently the best working copy.

You must also install Smarty (http://www.smarty.net)

The latest version of Smarty is 2.6.22 and is considered the best version to use. Its currently undergoing an upgrade to version 3.0 which is Alpha right now and has not been tested with the sfSmartyPlugin.

Suggest making it an external in the project root/lib/vendor directory.

It can be checked out from the Smarty SVN repository here, http://smarty-php.googlecode.com/svn/tags/Smarty_2_6_22/

Once you have installed smarty, you must edit the app.yml in each of your applications that you intend to use the smarty engine.

# default values
all:
  sfSmarty:
    class_path: lib/vendor/smarty/libs
    template_extension: .tpl
    template_security: false

The Smarty.class.php file is located under your smarty/libs folder. Thus the /libs on the end of the class_path.

Clear the cache to enable the autoloading to find the new classes:

Versions

This plugin is intended for Symfony 1.1 or 1.2 Only.

Usage

Smarty is considered one of the best templating engines for PHP.
The official Smarty documentation is located at http://www.smarty.net/docs.php

If you intend to use Smarty as the default templating engine, edit your project root/config/module.yml file. (if it doesnt exist, create one)

default:
  view_class: sfSmarty
  partial_view_class: sfSmarty

From then on all template files ending with .tpl (or whatever extension you set during installation) will be processed with sfSmarty.

If you only intend to use it with specific modules modify your module/config/module.yml with details as above.

You can still utilize .php templates as the plugin will now check the extention of the template and use the appropriate renderer.

sfSmartyPlugin also contains a settings.yml in its config folder. Suggest leaving this alone.

Example Usage

sfSmarty will automatically attach sf_data, sf_context, sf_request, etc.. to the smarty vars.

These can be used as such:

    {$sf_data->get('arg')}

Any variable in an action that is assigned to $this is also available, for example in the action:

    $this->var = 'foobar';

Can be used in the template as:

    {$var}

This is typical Smarty. All values are currently assigned (and thus copied), this will be updated as soon as possible to assign_by_ref (assigned by reference).

The smarty vars (sf_data, sf_context, ..) are currently assigned by references.

Smarty internal functions for looping and conditions work as is and should be used.

    {foreach from=$arr item='item'}
        My item is {$item}
    {/foreach}

Refer to the Smarty documentation.

The Javascript helper tag is also available and must be used as such:

    {javascript}
        function someFunc() {ldelim}
            do_sum_func();
        {rdelim}
    {/javascript}

Because the braces have a specific use in Smarty. It is suggested (of course) that the bulk of your javascript should be in javascript files which are attached when needed. Keep template javascript to a minimum.

To attach helpers:

    {use helper='Javascript'}

The {use} tag does not accept an array of values currently.

Smarty can also be utilized within actions via static calls. In an action:

    $smarty = sfSmarty::getInstance()->getSmarty();
    $smarty->register_object('foobar',$myobj);

$foobar will then be available as an object reference in your template.

Final Notes

I have tried to optimize certain methods and have cleaned up some from the last 0.12 release.

Helpers that are used are cached and will also be loaded from the cache after that. Smarty also compiles the templates and once compiled are always loaded until modified.

It may even be possible to store the references to these compiled templates to have the sfPHPView load them directly which reduce template rendering time a little further.

Symfony view caching can be used together with sfSmarty but it is not suggested to combine Symfony view cacheing with Smarty view caching as it is just extra overhead. Both cached files would be essentially the same.