|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
yus2aces Smarty n00b
Joined: 25 Jan 2013 Posts: 4
|
Posted: Fri Jan 25, 2013 2:46 pm Post subject: Can't find template/compile/config dir |
|
|
The code below i got from a book which shows this error "can't find index.tpl".
INDEX.PHP
Code: | <?php
//Load Smarty library and config files
require_once 'include/app_top.php';
//Load Smarty template file;
$page = new Smarty();
//Display the page
$page->display('index.tpl');
?> |
CONFIG.PHP
Code: | <?php
//SITE_ROOT contains the full path to the hatshop folder
define('SITE_ROOT', dirname(dirname(_FILE_)));
//Application directories
define('PRESENTATION_DIR', SITE_ROOT . '/presentation');
define('BUSINESS_DIR', SITE_ROOT . '/business');
//Settings needed to configure the Smarty template engine
define('SMARTY_DIR', SITE_ROOT . '/libs/smarty/');
define('TEMPLATE_DIR', PRESENTATION_DIR . '/templates');
define('COMPILE_DIR', PRESENTATION_DIR . '/templates_c');
define('CONFIG_DIR', SITE_ROOT . '/include/configs');
?> |
PAGE.PHP
Code: | <?php
//Reference Smarty library
require_once SMARTY_DIR . 'Smarty.class.php';
//Class that extends Smarty, used to process and display Smarty files
class Page extends Smarty
{
//Class constructor
public function _construct()
{
//Call Smarty's constructor
parent::_construct();
//Change the default template directories
$this->setTemplateDir(TEMPLATE_DIR);
$this->setCompileDir(COMPILE_DIR);
$this->setConfigDir(CONFIG_DIR);
}
}
?> |
APP_TOP.PHP
Code: | <?php
//include utility files
require_once 'include/config.php';
//Load the page template
require_once PRESENTATION_DIR . '/page.php';
?> |
INDEX.TPL
Code: | {* smarty *}
{config_load file="site.conf"}
<!DOCTYPE html>
<html>
<head>
<title>{#site_title#}</title>
<link href="hatshop.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div>
<div class="left_box">
Place list of departments here
</div>
<div id="content">
Place contents here
</div>
</div>
</body>
</html> |
Could anybody shows me where the source of error?
Thank you in advance. |
|
Back to top |
|
yus2aces Smarty n00b
Joined: 25 Jan 2013 Posts: 4
|
Posted: Fri Jan 25, 2013 2:57 pm Post subject: testInstall() result |
|
|
BTW this is the testInstall() result.
Code: | Smarty Installation test...
Testing template directory...
FAILED: .\templates\ does not exist.
Testing compile directory...
FAILED: .\templates_c\ does not exist.
Testing plugins directory...
E:\hatshop\libs\smarty\plugins is OK.
Testing cache directory...
FAILED: .\cache\ does not exist.
Testing configs directory...
FAILED: .\configs\ does not exist.
Testing sysplugin files...
... OK
Testing plugin files...
... OK
Tests complete. |
|
|
Back to top |
|
yus2aces Smarty n00b
Joined: 25 Jan 2013 Posts: 4
|
Posted: Fri Jan 25, 2013 3:40 pm Post subject: Working for absolute path |
|
|
I tried to write an absolute path to the template dir, and it worked. The page load successfully.
Code: | <?php
require_once('e:/hatshop/libs/smarty/Smarty.class.php');
$smarty = new Smarty();
$smarty->setTemplateDir('e:/hatshop/presentation/templates');
$smarty->setCompileDir('e:/hatshop/presentation/templates_c');
//$smarty->setCacheDir('/web/www.example.com/smarty/cache');
$smarty->setConfigDir('e:/hatshop/include/configs');
//$smarty->testInstall();
$smarty->display('index.tpl');
?> |
Could someone shows me where i've done wrong? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Jan 25, 2013 5:47 pm Post subject: |
|
|
If you want the defaults to work, be sure your cwd is the directory where the templates dir resides, and "." is in your php include_path. That said, it is better to supply full filepaths anyways (less error prone, more efficient.) |
|
Back to top |
|
yus2aces Smarty n00b
Joined: 25 Jan 2013 Posts: 4
|
Posted: Sun Jan 27, 2013 7:32 am Post subject: Solved |
|
|
Thanks mohrt for your guidance.
I've skipped page.php and change index.php a bit. So the index.php will contain this code :
Code: | <?php
//Load Smarty library and config files
require_once 'include/app_top.php';
//Load Smarty template file;
$page = new Smarty();
$page->setTemplateDir(TEMPLATE_DIR);
$page->setCompileDir(COMPILE_DIR);
$page->setConfigDir(CONFIG_DIR);
//Display the page
$page->display('index.tpl');
?> |
And it worked.
But still confused why, page.php is not working. Hoping someone could tell me why, for my learning purpose. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sun Jan 27, 2013 9:59 am Post subject: |
|
|
In page.php you did extend the Smarty class. If you include it in index.php you must instance the Page class instead of Smarty.
in index.php
Code: | $page = new Page();
|
|
|
Back to top |
|
|
|
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
|