Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: dd1c71df52
Fetching contributors…

Cannot retrieve contributors at this time

executable file 173 lines (159 sloc) 4.901 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
<?php
/**
* Breeze Framework - Smarty Plugin
*
* This file contains the Smarty plugin for the Breeze Framework. For more
* information on Smarty, visit {@link http://www.smarty.net/}.
*
* LICENSE
*
* This file is part of the Breeze Framework package and is subject to the new
* BSD license. For full copyright and license information, please see the
* LICENSE file that is distributed with this package.
*
* @package Breeze
* @subpackage View
* @author Jeff Welch <whatthejeff@gmail.com>
* @copyright 2010-2011 Jeff Welch <whatthejeff@gmail.com>
* @license https://github.com/whatthejeff/breeze/blob/master/LICENSE New BSD License
* @link http://breezephp.com/
*/

namespace Breeze\View\Driver;

/**
* @see Breeze\Application
*/
use Breeze\Application;

/**
* @see Breeze\Application
*/
require_once 'Breeze/Application.php';
/**
* @see Smarty
*/
@include_once 'Smarty.class.php';

/**
* The Smarty-based template engine for the Breeze Framework.
*
* @package Breeze
* @subpackage View
* @author Jeff Welch <whatthejeff@gmail.com>
* @copyright 2010-2011 Jeff Welch <whatthejeff@gmail.com>
* @license https://github.com/whatthejeff/breeze/blob/master/LICENSE New BSD License
* @link http://breezephp.com/
*/
class Smarty extends Driver
{
    /**
* An instance of Smarty
*
* @var Smarty
* @see http://www.smarty.net/
*/
    protected $_smarty;

    /**
* The default directory for compiled templates.
*/
    const DEFAULT_COMPILE_DIR = 'compiled';
    /**
* The default directory for cached templates.
*/
    const DEFAULT_CACHE_DIR = 'cache';
    /**
* The default directory for configurations.
*/
    const DEFAULT_CONFIG_DIR = 'config';

    /**
* Sets up the templates directory path and the extra options for a
* database engine. The extra options are to be defined by the
* specific engines.
*
* @param Breeze\Application $application A Breeze application
* @param string $path The path to the templates
* directory
* @param array $options Extra options for custom engines
*
* @return void
*/
    public function __construct(Application $application, $path = null,
        array $options = array())
    {
        $this->_smarty = new \Smarty();
        parent::__construct($application, $path, $options);

        $this->_smarty->registerPlugin(
            'function', 'partial', array($this, 'partial')
        );
    }

    /**
* Sets up the internal template engine structures. This is intended
* to be where engine specific options are set up.
*
* @return void
*/
    protected function _config()
    {
        $path = $this->getPath();

        $this->_smarty->template_dir = $path;
        $this->_smarty->compile_dir = $path . '/' . $this->getOption(
            'compile_dir', self::DEFAULT_COMPILE_DIR
        );
        $this->_smarty->cache_dir = $path . '/' . $this->getOption(
            'cache_dir', self::DEFAULT_CACHE_DIR
        );
        $this->_smarty->config_dir = $path . '/' . $this->getOption(
            'config_dir', self::DEFAULT_CONFIG_DIR
        );
    }

    /**
* Renders a template using the $variables parameter and returns
* the contents.
*
* @param string $template The path to the template, excluding the base
* templates directory.
* @param array $variables An associative array of variables to use in the
* template.
*
* @return string
*/
    protected function _fetchTemplate($template, array $variables = array())
    {
        $this->_smarty->assign($variables);
        return $this->_smarty->fetch($template);
    }

    /**
* Smarty wrapper for the {@link Breeze\View\View::partial()} function.
*
* @param array $params An associative array of parameters for the smarty
* function.
*
* @return string
*/
    public function partial($params)
    {
        if (!array_key_exists('file', $params)) {
            trigger_error(
                "Smarty error: [partial] missing parameter 'file'",
                E_USER_ERROR
            );
        }

        $file = $params['file'];
        unset($params['file']);

        return $this->_application->partial($file, $params);
    }
}

Application::register(
    'Smarty',
    function($app)
    {
        // Sets up some default Smarty configurations
        $app->config(
            array(
                'template_engine' => 'Smarty',
                'template_extension' => '.tpl',
                'template_options' => array(
                    'compile_dir' => Smarty::DEFAULT_COMPILE_DIR,
                    'cache_dir' => Smarty::DEFAULT_CACHE_DIR,
                    'config_dir' => Smarty::DEFAULT_CONFIG_DIR)
            )
        );
    }
);
Something went wrong with that request. Please try again.