PHP Classes

File: SmartyPlugin.php

Recommend this page to a friend!
  Classes of Scott Christensen   Phramework   SmartyPlugin.php   Download  
File: SmartyPlugin.php
Role: Auxiliary script
Content type: text/plain
Description: Reusable view plugin class for Smarty templates in Phramework
Class: Phramework
MVC framework for building web applications
Author: By
Last change: Changed description
Date: 19 years ago
Size: 3,229 bytes
 

Contents

Class file image Download
<?php
/*
 * Phramework Copyright (C) 2005 GodsArmor.com
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option) any
 * later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, write to theFree Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import('Smarty');

/**
 * This class requires the following parameters
 * <ul>
 * <li>templateDir: The directory where Smarty templates are located.</li>
 * <li>compileDir: The directory where Smarty templates are compiled.</li>
 * <li>configDir: The directory where configuration files are located.</li>
 * <li>cacheDir: The directory where the cache should be located.</li>
 * </ul>
 * <p>Optionally you can pass in these parameters as well:
 * <ul>
 * <li>caching: Should Smarty cache templates. Defaults to false.</li>
 * </ul>
 * <p>You can also pass in anything else to this class and the plugin will
 * do an initial assign for the key to the value. So you could pass in
 * appName=MyApp and this plugin would assign the appName key to MyApp
 */
class SmartyPlugin extends ViewPlugin {
    var
$smarty;
   
    function
SmartyPlugin() {
       
$this->smarty = new Smarty();
    }
   
    function
init($params) {
       
$this->smarty->template_dir = $params['template_dir'];
       
$this->smarty->compile_dir = $params['compile_dir'];
       
$this->smarty->config_dir = $params['config_dir'];
       
$this->smarty->cache_dir = $params['cache_dir'];
       
        if (
array_key_exists('plugins_dir', $params)) {
            if (
strlen(trim($params['plugins_dir'])) > 0) {
               
// append this list of plugins directories to the current one
                // placing the ones in the configuration file first
               
$arrPluginsDir = explode('|', $params['plugins_dir']);
               
$arrPluginsDir[] = $this->smarty->plugins_dir[0];
               
$this->smarty->plugins_dir = $arrPluginsDir;
            }
        }
       
        if (
array_key_exists('caching', $params)) {
           
$this->smarty->caching = $params['caching'];
        } else {
           
$this->smarty->caching = 0;
        }
       
       
$this->setAssigns($params);
    }

    function
destroy() {
        unset(
$this->smarty);
    }

    function
processView($viewValue, $data) {
       
$this->setAssigns($data);
       
$this->smarty->display($viewValue);
    }
   
    function
setAssigns($params) {
        if (
is_array($params)) {
           
$arrKeys = array_keys($params);
            foreach(
$arrKeys as $key) {
               
$this->smarty->assign($key, $params[$key]);
            }
        }
    }
}
?>