Reputation: 21
I am not quite sure about this, but reading from the smarty instructions http://www.smarty.net/docs/en/installing.smarty.basic.tpl , I would have to set $template_dir, $compile_dir, $config_dir, and $cache_dir every time I have a new PHP script. In other words, I would have to add the following lines of code for each PHP script:
$smarty->setTemplateDir('/.../templates/');
$smarty->setCompileDir('/...templates_c/');
$smarty->setConfigDir('/.../configs/');
$smarty->setCacheDir('/.../cache/');
Is that correct? Did you guys do any "shortcuts" to avoid this?
Upvotes: 1
Views: 5317
Reputation: 3487
Better solution in my opinion, is to extend the Smarty class.
<?php
require_once SMARTY_DIR . 'Smarty.class.php';
class Application extends Smarty {
public function __construct() {
parent::__construct();
$this
->addTemplateDir (TPL_DIR)
->setCompileDir (COMPILE_DIR)
->setConfigDir (CONFIG_DIR)
->setCacheDir (CACHE_DIR)
->setPluginsDir ( array ( SMARTY_DIR.'plugins', PRESENTATION_DIR.'smarty_plugins' ) );
$this->caching = false; // set Smarty caching off by default
$this->muteExpectedErrors(); // can't remember what this does exactly, but it tunes down the sensitivity of errors
$this->debugging = SMARTY_DEBUG; // setting controlled in config. file
}
}
?>
Then simply initiate the new "Application" class.
Upvotes: 0
Reputation: 8301
You should set all of these things in a common config file, then include it when you need it.
include( 'path/to/common_config.php' );
Then, in your common_config.php, you can just do something like this:
//set up Smarty
require_once( dirname( __FILE__ ) . '/smarty/Smarty.class.php' );
$smarty = new Smarty;
$smarty->error_reporting = E_ALL & ~E_NOTICE;
$smarty->setTemplateDir( dirname( __FILE__ ) . '/../templates' );
$smarty->setCompileDir( dirname( __FILE__ ) . '/../smarty/templates_c' );
The use of "dirname( FILE )" will ensure that the path is always relative to the common config file.
Now all you need to do is use the display method with the name of the template file:
$smarty->display( 'index.tpl' );
Upvotes: 2