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

Custom Template Resource that doesn't update

 
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 -> General
View previous topic :: View next topic  
Author Message
LG
Smarty n00b


Joined: 25 Aug 2015
Posts: 2

PostPosted: Tue Aug 25, 2015 8:15 am    Post subject: Custom Template Resource that doesn't update Reply with quote

Hello everyone.
I'm using some kind of module system and each got it's templates in it's own directory.
Templates are called from one to another and it quickly became complicated to call a template (they are all files).

I've made a custom template that goes like this :
Code:
class Smarty_Resource_Mymodules extends Smarty_Resource_Custom {
   protected $smarty;
   public function __construct($smarty) {
      $this->smarty = $smarty;
   }
   private function getTpl($name) {
      list($module, $file) = explode(':', $name, 2);
      $this->smarty->setModule($module); // <- this gives smarty specifics for the module
      return __DIR__."/../../$module/tpl/$file";
   }
   protected function fetch($name, &$source, &$mtime){
      $name = $this->getTpl($name);
      if (file_exists($name)) {
         $source = file_get_contents($name);
         $mtime = filemtime($name);
      } else {
         $source = null;
         $mtime = null;
      }
   }
   protected function fetchTimestamp($name) {
      $name = $this->getTpl($name);
      return (int) @filemtime($name);
   }
}

Call in smarty:  {include file="mymodules:Member:register.tpl"}

My main problem: once the template cache was written once, it's never even re-written.
I modify "register.tpl", but no change in the resulting html.

I added debug code and the filemtime() do work and are called.
I tried | and - as module separator instead, all worked the same, so it's not because of the :
I have no clue why, I tried many tests and searched online a lot Sad
I have $smarty->caching = false;

So...
As it's almost the same as loading a file template, could I use a more direct way like extending some kind of Smarty_Resource_File?

If not, do you have any idea why the compiled template isn't updated?

Thanks a lot for your help
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Aug 25, 2015 8:50 am    Post subject: Reply with quote

I have a feeling that you need to rethink your templating model. Too many cross-references spelling doom to your application maintainability.

To your immenent issue: You MUST remove template cache when you modify a template.
There's no excuse to not do it.
Back to top
View user's profile Send private message
LG
Smarty n00b


Joined: 25 Aug 2015
Posts: 2

PostPosted: Tue Aug 25, 2015 9:20 am    Post subject: Reply with quote

I still can change my template model and will be happy to do so if there is a better way.
So that it's the only way I could find.
Before that, I was writing the complete path to each file, which was a little bothering.
like that » {include tpl="$pathmodules/Member/tpl/register.tpl"}
(and this didn't handle the smarty personal conf I needed, for example for module-specifics translations)

> You MUST remove template cache when you modify a template.

You mean manually? In normal file template system, any change of the tpl file automatically make the cache file be re-written. I though functions like fetchTimestamp() were for that purpose: to compare the source update time with the cached file time.

Should I add in my code something to explicitly remove the cached template file? I think that smarty already does that (well, obviously, it doesn't in my case).
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Aug 25, 2015 4:13 pm    Post subject: Reply with quote

I mean, it is configurable, but on production systems, template cache is never checked for performance reasons.

Frankly, you should go back to Smarty documentation and re-read Part III. Smarty For Programmers. It has answers to all your questions and more.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Tue Aug 25, 2015 10:57 pm    Post subject: Reply with quote

The simplest solution is to addTemplatesDir() to be an array with named index (index is your module names).

Code:
{include file="['Member']register.tpl"}


But this could be not the best solution if you have a large number of modules.

Here a special resource (not tested)

Code:
class Smarty_Resource_Mymodules extends Smarty_Internal_Resource_File {
    public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
    {
        list($module, $file) = explode(':', $source->name, 2);
        $s = Smarty_Template_Source::load(null, $source->smarty, 'file:' . __DIR__."/../../$module/tpl/$file");
        $source->filepath = $s->filepath;
        $source->uid = $s->uid;
        $source->exists = $s->exists;
        $source->timestamp= $s->timestamp;
     }
}
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 -> 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