(file) Return to TemplateEngine.class.php CVS log (file) (dir) Up to [Development] / old-advokit-installer

File: [Development] / old-advokit-installer / TemplateEngine.class.php (download) / (as text)
Revision: 1.1.1.1 (vendor branch), Sat Jun 5 04:22:18 2004 UTC (6 years, 3 months ago) by travislow
Branch: advokit-installer, MAIN
CVS Tags: start, HEAD
Changes since 1.1: +0 -0 lines
Initial import of AdvoKit into voter2voter cvs ... I hope you committed your changes. :-) 

<?
#======================================================================
# Wrapper for template engine.
#
# (c) 2003, Dawnstar - http://dawnstar.com - info@dawnstar.com
#======================================================================

# This is included because the functions within must be
# globally accessible.
require "$t_root".DIRSEP."tabindex.inc.php";
require "$t_root".DIRSEP."stripspaces.inc.php";

class TemplateEngine
{
    var $name;
    var $logger;
    var $te;
    var $auto_html_escape;
    var $te_dir;
    var $login_no_cookies;
    var $login_cookie_name;
    #.................................................. TemplateEngine
    #
    # Create an instance of the template engine.
    #
    function TemplateEngine( &$config, &$logger )
    {
        $this->logger =& $logger;
        $this->name = $config->get( "te_name" );
        $this->auto_html_escape = $config->get( "te_auto_html_escape" );
        $this->login_no_cookies = $config->get( "login_no_cookies" );
        $this->login_cookie_name = $config->get( "login_cookie_name" );
        $this->te_dir = $config->get( 'te_dir' );
        if( $this->te_dir )
        {
            $this->te_dir .= DIRSEP;
        }
        if( "smarty" == $this->name )
        {
            if( $this->te_dir )
            {
                define( 'SMARTY_DIR', $this->te_dir );
            }
            require $this->te_dir.'Smarty.class.php';
            $this->te = new Smarty();
            $this->te->use_sub_dirs = FALSE;
            $te_root = $config->get( "te_root" );
        
            $this->te->template_dir = $te_root.DIRSEP.$config->get( "te_tpls_raw" );
            $this->te->compile_dir = $te_root.DIRSEP.$config->get( "te_tpls_compiled" );
            $this->te->config_dir = $te_root.DIRSEP.$config->get( "te_tpl_configs" );
            $this->te->debugging = $config->get( "te_debugging_on" );
            $this->te->debug_tpl = $config->get( "te_debugging_tpl" );
            $this->te->compile_check = $config->get( "te_compile_if_new" );
            $this->te->force_compile = $config->get( "te_compile_always" );
            $this->te->caching = $config->get( "te_caching_on" );
            $this->te->cache_dir = $config->get( "te_tpls_cached" );
            $this->te->use_sub_dirs = $config->get( "te_use_sub_dirs" );
            $this->te->cache_lifetime = $config->get( "te_cache_timeout" );
            $this->te->left_delimiter = $config->get( "te_left_delimiter" );
            $this->te->right_delimiter = $config->get( "te_right_delimiter" );
            if( $config->get( "te_security_level" ) == "high" )
            {
                $this->te->security = true;
                $this->te->secure_dir = 
                    array( $te_root.DIRSEP.$config->get( "te_tpls_raw" ),
                           $te_root.DIRSEP.$config->get( "te_tpls_compiled" ),
                           $te_root.DIRSEP.$config->get( "te_tpl_configs" ),
                           $te_root.DIRSEP.$config->get( "te_tpls_cached" ) );
            }
            # The following statements may use the files
            # included (required) at the top of this file.
            if(  $config->get( "strip_leading_spaces" ) )
            {
                $this->registerPrefilter( "stripLeadingSpaces" );
            }            
            if(  $config->get( "strip_tab_indexes" )
              || $config->get( "add_tab_indexes" )
              )
            {
                # You have to register them in this order, since 
                # you want to strip before adding.
                if( $config->get( "strip_tab_indexes" ) )
                {
                    $this->registerPostfilter( "stripTabIndexes" );
                }
                if( $config->get( "add_tab_indexes" ) )
                {
                    $this->registerPostfilter( "addTabIndexes" );
                }
            }

            $this->logger->debug( __FILE__, "Created instance of '".$this->name."' template engine." );
        }
        else
        {
            $this->logger->error( __FILE__, "Specified template engine '$this->name' not found.  Can't create instance!" );
        }
    }

    #.................................................. render
    #
    # Render the specified template.  The $info parameter
    # is only used if the template engine needs additional
    # information in order to render the specified template.
    #
    function render( $template, $info=NULL )
    {
        if( "smarty" == $this->name )
        {
            $this->te->display( "file:$template" );
        }
        else
        {
            $this->logger->error( __FILE__, "Error trying to render '$template'." );
        }
    }

    #.................................................. getstring
    #
    # Render the specified template as a string.
    #
    function getstring( $template, $info=NULL )
    {
        if( "smarty" == $this->name )
        {
            return $this->te->fetch( "file:$template" );
        }
        else
        {
            $this->logger->error( __FILE__, "Error trying to render '$template' as a string." );
        }
        return NULL;
    }

    #.................................................. registerPostfilter
    #
    # Register a function to filter templates after they
    # have been processed.
    #
    function registerPostfilter( $filtername )
    {
        if( "smarty" == $this->name )
        {
            $this->te->register_postfilter( $filtername );
        }
        else
        {
            $this->logger->error( __FILE__, "Error trying to register postfilter '$filtername'.  No template engine named '".$this->name."'" );
        }
    }

    #.................................................. registerPrefilter
    #
    # Register a function to filter templates before they
    # have been processed.
    #
    function registerPrefilter( $filtername )
    {
        if( "smarty" == $this->name )
        {
            $this->te->register_prefilter( $filtername );
        }
        else
        {
            $this->logger->error( __FILE__, "Error trying to register prefilter '$filtername'.  No template engine named '".$this->name."'" );
        }
    }

    #.................................................. set
    #
    # Set the value of the specified variable
    #
    function set( $key, $value )
    {
        if( "smarty" == $this->name )
        {
            if( $this->auto_html_escape )
            {
                $this->te->assign( $key, $this->htmlEscape( $value ) );
            }
            else
            {
                $this->te->assign( $key, $value );
            }
        }
        else
        {
            $this->logger->error( __FILE__, "Error trying to set value in template." );
        }
    }

    #.................................................. setWithDefault
    #
    # Set the value of the specified variable.  If $value
    # is empty, use $default instead.
    #
    function setWithDefault( $key, $value, $default )
    {
        if( "smarty" == $this->name )
        {
            if( isset( $value ) && $value != "" )
            {
                if( $this->auto_html_escape )
                {
                    $this->te->assign( $key, $this->htmlEscape( $value ) );
                }
                else
                {
                    $this->te->assign( $key, $value );
                }
            }
            else
            {
                if( $this->auto_html_escape )
                {
                    $this->te->assign( $key, $this->htmlEscape( $default ) );
                }
                else
                {
                    $this->te->assign( $key, $default );
                }
            }
        }
        else
        {
            $this->logger->error( __FILE__, "Error trying to render '$template' as a string." );
        }
    }

    #.................................................. getAutoHtmlEscape
    #
    # Return the value of $this->auto_html_escape
    #
    function getAutoHtmlEscape()
    {
        return $this->auto_html_escape;
    }

    #.................................................. setAutoHtmlEscape
    #
    # Set the value of $this->auto_html_escape
    #
    function setAutoHtmlEscape( $value )
    {
        $this->auto_html_escape = $value;
    }

    #.................................................. htmlEscape
    #
    # Convert characters such as "<" to their corresponding
    # HTML escape sequences.
    #
    function htmlEscape( &$item )
    {
        if( is_array( $item ) )
        {
            foreach( $item as $key => $value )
            {
                $item[$key] = $this->htmlEscape( $value );
            }
            return $item;
        }
        else
        {
            return htmlspecialchars( $item );
        }
    }
} 
?>

cvsadmin@voter2voter.org
CVS Snapshots (updated daily)