Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Dedicated server web hosting provided by Guru-host.eu.
Cashing included tpl in targeted file

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
at1976
Smarty n00b


Joined: 31 Jan 2012
Posts: 1

PostPosted: Tue Jan 31, 2012 3:52 pm    Post subject: Cashing included tpl in targeted file Reply with quote

hi. i am trying to figure out how smarty caching work. i have a target template store.tpl which has included tpl files in it. How can you go about caching included tpl files in targeted template. so far only caching targeted tpl. SO far smarty only caches one tpl file and wont let it load another one.

Below is partial of my code..

Code:

******application.php*********
<?php
// Reference Smarty library
require_once SMARTY_DIR . 'Smarty.class.php';

class Application extends Smarty
{
  // Class constructor
  public function __construct()
  {
    // Call Smarty's constructor
    parent::Smarty();

    // Change the default template directories
    $this->template_dir = TEMPLATE_DIR;
    $this->compile_dir = COMPILE_DIR;
    $this->config_dir = CONFIG_DIR;
    $this->plugins_dir[0] = SMARTY_DIR . 'plugins';
    $this->plugins_dir[1] = PRESENTATION_DIR . 'smarty_plugins';
    $this->cache_dir = CACHE_DIR; 
    $this->caching = 1;
    $this->compile_check = TRUE;
    $expiration_secs = 2 * 60 * 60;
    $this->clear_all_cache($expiration_secs);
  }
}
?>

*******Index.php************
<?php
require_once 'application.php';

// Load Smarty template file
$application = new Application();

// Display the page
  $application->display('store.tpl');
?>



*****store.tpl***********
{load_presentation_object filename="store" assign="obj"}
{include file="header.tpl"}
{include file="body.tpl"}
{include file="another.tpl"}
{$obj->mContentsCell}

******store.php**********
<?php
class Store
{
  //defined some varibles 

  // Class constructor
  public function __construct()
  {
   //some code here
  }

  // Initialize presentation object
  public function init()
  {
   
    // Load department details if visiting a main
    if (isset ($_GET['DepartmentId']))
    {
      $this->mContentsCell = 'department_no_col.tpl';
         
    }
    elseif (another condition)
    {
   $this->mContentsCell = 'product.tpl';
    }
}
?>

******smarty plugin that loads objects from class******
<?php
// Plug-in functions inside plug-in files must be named: smarty_type_name
function smarty_function_load_presentation_object($params, $smarty)
{
  require_once PRESENTATION_DIR . $params['filename'] . '.php';

  $className = str_replace(' ', '',
                           ucfirst(str_replace('_', ' ',
                                             $params['filename'])));
  // Create presentation object
  $obj = new $className();

  if (method_exists($obj, 'init'))
  {
    $obj->init();
  }

  // Assign template variable
  $smarty->assign($params['assign'], $obj);
 
 
}
?>
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4200
Location: Hamburg / Germany

PostPosted: Tue Jan 31, 2012 4:54 pm    Post subject: Reply with quote

If you enable caching and call display('store.tpl') the complete output including the subtemplate is cached.

There is no need to call $this->clear_all_cache($expiration_secs);
to delete outdated cache files.

Just define your life time like

$this->cache_lifetime = 2 * 60 * 60;

and Smarty will refresh the cache automatically if this time has expired.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General 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