Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

A simple compile method

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
sviver
Smarty Rookie


Joined: 14 Jun 2006
Posts: 6

PostPosted: Mon Jun 19, 2006 7:38 pm    Post subject: A simple compile method Reply with quote

If you want to compile templates without displaying them (for example compile all .tpl files, and set templates_c readonly):

1) In Smarty.class.php add a new parameter to the fetch() method:
Code:
 function fetch($resource_name, $cache_id = null, $compile_id = null, $display = false, $compileonly=false)


2) Modify the fetch method not to include the compiled template, if this parameter is true:
Code:
        // if we just need to display the results, don't perform output
        // buffering - for speed
        $_cache_including = $this->_cache_including;
        $this->_cache_including = false;
        if ($display && !$this->caching && count($this->_plugins['outputfilter']) == 0) {
            if ($this->_is_compiled($resource_name, $_smarty_compile_path)
                    || $this->_compile_resource($resource_name, $_smarty_compile_path))
            {
                if (!$compileonly) {
                         include($_smarty_compile_path);
                }
            }
        } else {


3) Create a new compile method:
Code:
     /**
     * compiles the template
     *
     * @param string $resource_name
     * @param string $cache_id
     * @param string $compile_id
     */
    function display($resource_name, $cache_id = null, $compile_id = null)
    {
        $this->fetch($resource_name, $cache_id, $compile_id, true, true);
    }
Back to top
View user's profile Send private message
gektor
Smarty n00b


Joined: 25 Jun 2006
Posts: 1

PostPosted: Mon Jun 26, 2006 4:33 am    Post subject: source Reply with quote

Great idea!
_________________
Cool Chemical Portal
Genuine Chemical Blogs
Back to top
View user's profile Send private message Visit poster's website
sorin
Smarty n00b


Joined: 03 Sep 2006
Posts: 4

PostPosted: Sun Sep 03, 2006 4:18 pm    Post subject: Reply with quote

Sounds good, but what about just checking to see if a template is valid and displaying any errors found?

Something like checktemplate() that would either return true or an error message?
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun Sep 03, 2006 6:53 pm    Post subject: Reply with quote

I've posted these before, but am too lazy to search...add these methods to your extended Smarty class -- no modifications to the Smarty code required.

[php:1:71eb4e788c] /**
* fetch's a template without saving to disk
*/
public function fetch_eval($template_source='')
{
if( !empty( $template_source ) ) {
$this->_compile_source( 'evaluated template', $template_source, $_var_compiled );
ob_start();
$this->_eval( '?>' . $_var_compiled );
$_contents = ob_get_contents();
ob_end_clean();

return( $_contents );
}
}

/**
* fetch's a template's compiled source without saving to disk
*/
public function fetch_compiled($template_source='')
{
if( !empty( $template_source ) ) {
$this->_compile_source( 'evaluated template', $template_source, $_var_compiled );

return( $_var_compiled );
}
}
[/php:1:71eb4e788c]
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP