|
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 |
androidworkz Smarty Rookie
Joined: 08 Aug 2011 Posts: 15
|
Posted: Sun Aug 14, 2011 4:23 pm Post subject: Another odd issue with 3.1 DEV |
|
|
I have written my own MVC framework and integrated Smarty as the template engine. I have a page controller and a post controller which pulls the content from the database based on the page slug:
For example:
First Page URL: http://www.mysite.com/page/contact-us/
Second Page URL: http://www.mysite.com/page/about-us/
I am using a single template (page.tpl) to serve the content. The problem is that if you visit "About Us" and then visit "Contact Us", the content from the "About Us" page will be shown. I have even tried placing {nocache}{/nocache} around the content section and I still get the same results.
I have dumped the content in the controller... so I have verified that it is pulling the correct content... smarty just isn't showing the right content. |
|
Back to top |
|
androidworkz Smarty Rookie
Joined: 08 Aug 2011 Posts: 15
|
Posted: Sun Aug 14, 2011 4:31 pm Post subject: |
|
|
Ok... just for clarification, I removed 3.1 and replaced it with 3.0.8 and am seeing the same problem.
This is a deal breaker if I can't address it. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sun Aug 14, 2011 5:28 pm Post subject: |
|
|
Can you post the section of your code where the data get assigned and display() or fetch() is called. |
|
Back to top |
|
androidworkz Smarty Rookie
Joined: 08 Aug 2011 Posts: 15
|
Posted: Sun Aug 14, 2011 9:33 pm Post subject: |
|
|
How I integrated Smarty is that I extended the class with my "View" class.
Code: |
class View extends Smarty {
public $section = 'index';
public $sections = array();
public $registry;
public $context;
public $directory;
public function __construct() {
global $smarty_config; // smarty_config is an array from my config file which is included in the bootstrap
parent::__construct();
$this->registry = new RegistryInterface();
$this->context = $this->registry->org('context');
parent::setTemplateDir($smarty_config['template_dir'].$this->context.DIRECTORY_SEPARATOR);
parent::setCompileDir($smarty_config['compile_dir']);
parent::setConfigDir($smarty_config['config_dir']);
parent::setCacheDir($smarty_config['cache_dir']);
$this->directory = $smarty_config['template_dir'].$this->context.DIRECTORY_SEPARATOR;
/* Smarty Security */
parent::enableSecurity('Policy'); // Policy is an object I created as described in the Smarty documentation
/* End Smarty Security */
}
public function load($template) {
echo parent::fetch($this->getTemplate($template));
}
public function autoload($template) {
$this->setSections(array('header.tpl', $template, 'footer.tpl'));
$this->renderSections();
}
public function setSections($sections = array()) {
$this->sections = array();
foreach ($sections as $section) {
$this->sections[] = $this->getTemplate($section);
}
}
public function setSection($section) {
$this->section = $section;
}
public function renderSections() {
$section_html = '';
foreach ($this->sections as $section) {
$section_html .= parent::fetch($section);
}
echo $section_html;
}
public function getTemplate($template) {
return $this->directory . $this->section . DIRECTORY_SEPARATOR . $template;
}
public function set($key, $value, $nocache = false) {
parent::assign($key, $value, $nocache);
}
public function setCaching($cache = true) {
parent::setCaching($cache); //I added this setter function to Smarty
}
public function setContext($context) {
$this->context = $context;
}
}
|
So this is the great majority my View class.
In between my Controller class and the View class is an object bootstrap which dynamically instantiates objects as they are needed (my own "autoloading" mechanism that uses Reflection) and is an interface for my object registry... so if the object has already been instantiated it will reuse it.
In the constructor of my Controller class I am setting caching (I didn't want caching enabled in my admin section, for example). All of my Controller classes extend a base Controller class that extends my Registry interface class... so I can instantiate every object that I need by call $this->object()->method()
Code: |
$this->view()->setCaching(false);
|
Then the Controller class calls the action method that is appropriate. $args is an array of key/value pairs of the url that have been sanitized in my Router class.
Code: |
private function pagesAction($args) {
$pages = $this->pages()->getPages($args);
if ($pages) {
$this->view()->set('pages', $page);
$this->view()->load('pages.tpl');
}
else {
header('location: ' . $this->application()->url(). $this->cfg()->relative . 'error/notfound/');
exit;
}
}
|
|
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Aug 15, 2011 12:39 pm Post subject: |
|
|
If you have caching enabled and you use same template for different content you must use a cache_id to get the rendered content seperated.
Code: | public function load($template, $cache_id = null) {
echo parent::fetch($this->getTemplate($template), $cache_id);
}
|
and
Code: | private function pagesAction($args) {
$pages = $this->pages()->getPages($args);
if ($pages) {
$this->view()->set('pages', $page);
$this->view()->load('pages.tpl',md5($page));
}
else {
header('location: ' . $this->application()->url(). $this->cfg()->relative . 'error/notfound/');
exit;
}
} |
The cache_id must be something which is unique to identify the content. In my example above I use md5($page) but it could be something else.
I have no idea why it did not work with the {nocache} tags. are you sure that the template got compiloed after that modification? |
|
Back to top |
|
androidworkz Smarty Rookie
Joined: 08 Aug 2011 Posts: 15
|
Posted: Mon Aug 15, 2011 12:50 pm Post subject: |
|
|
Thanks for the prompt reply.
Two things were apparently awry for me. First, as you mentioned, I needed to review the cache control section of the documentation better.
Secondly, it appears that my server admin made some changes to my server and added suexec which played hell with permissions. I was able to address this by having the script create the folders... I had to create an automatic setup routine to accommodate this issue.
So... while I was quick to think that Smarty was playing a role, I was mistaken and I apologize for taking your time with it. |
|
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
|