endel / slim-lightncandy-view

Mustache / Handlebars template engine for Slim Framework 3.x
MIT License
9 stars 3 forks source link

Caching #9

Open kenesa opened 7 years ago

kenesa commented 7 years ago

Hello @endel,

Question for you. I am looking at adding caching of the compiled templates and was wondering if you would like to have it added to the project. I know that folks at zordius/lightncandy have different views on 'compile-on-demand' but their recommended approuch does not work for all cases.

My idea would be to add it as a plugin to \Slim\Views\Lightncandy and would be used by the fetch() method.

What are your thoughts?

endel commented 7 years ago

Hey @kenesa, that's a really good idea.

It would be nice to have a new option when registering, like:

$app->register(new \Slim\Views\Lightncandy('path/to/templates', [
    'cache' => "path/for/cache"
]));

Is your idea to cache the result of the rendering, or to have a pre-compiled template that is faster to render new data on?

kenesa commented 7 years ago

@endel,

I was only going to cache the compliled templates to help eliminate the effort needed to compile the template. This makes it easy to determine when the cache is stale, basically when the templates modifed timestamp changes.

Caching the results after rendering is better left to an extension in Slim as it can capture the the results of the entire request/results process.

I was planning on making it a simple plugin so that if someone comes up with a different approuch it would be simple to plug it in. It would be more along these lines:

$cache = new \Slim\Views\TemplateCache('path/for/cache');
$view = new \Slim\Views\Lightncandy('path/to/templates');
$view->addCaching($cache);
$app->register($view);

Not quite as eloquent, but most of it would be handled in dependency injection.