fenom-template / fenom

Template Engine for PHP. Maintainers wanted!
Other
446 stars 108 forks source link

How to Get the Cached Path from File Name? #231

Closed taufik-nurrohman closed 8 years ago

taufik-nurrohman commented 8 years ago

I want to create plugin with this Template Engine but I don’t know how to get the cached file path of the template. I want to use it to replace the original file that should be included so that the orginal template flle can be replaced by Fenom template. Something like this:

$fenom_path = $fenom->getTheCachedFilePathBasedOnFileName('article.tpl'); // how to get the cached file path based on file name?
Filter::add('theme_path', function($path) use($fenom_path) {
    return $fenom_path; // so that I can include it here
});

For:

include Filter::apply('theme_path', $path);

Thanks.

maxisoft-git commented 8 years ago

I apologize for my very bad English, but I can not understand why replace a reference to the template file in the compiled template file. You can see a more detailed example of where it is running as?

taufik-nurrohman commented 8 years ago

Because $foo->display() will behave like echo $str and $foo->fetch() is basically the same as $str.

The original theme file using a plain PHP syntax, if I can get the cached path, then I can create an optional CMS feature where user can use plain PHP or use template engine for their themes without affecting the CMS core.

Yes it is possible to replace the generated markup from the output buffer but it is the same as overwriting previous included + rendered file, so using template engine will only increase the workload twice.

I may need to consider to add new hooks on my CMS to handle this.

taufik-nurrohman commented 8 years ago

Case

article.php
article.tpl
$o_path = 'path/to/article.php';
$n_path = preg_replace('#\.php$#', '.tpl', $o_path);

if(file_exists($n_path)) {
    include $fenom->doGetCachedPathFromName($n_path, $vars);
} else {
    include $o_path;
}

Something like that.

maxisoft-git commented 8 years ago

I have a different question, and where there will be a compilation of fenom template? to replace php on tpl you must first compile it. It seems to me that can not be done as you have described, and will have to make changes in your cms to provide the correct output templates.

taufik-nurrohman commented 8 years ago

And where there will be a compilation?

It should return the compiled version with .php extension. If the compiled version is still not available, the compilation will occur before the compiled path is returned.

… will have to make changes in your CMS to provide the correct output templates.

I’m now thinking about it.

If that’s not possible, looks like this will only be a request to add a feature that allows us to load a template with include.

Update: BTW how to use this part?

bzick commented 8 years ago

In the next version I will make a function that will return the path to the cache

taufik-nurrohman commented 8 years ago

Thanks.

bzick commented 8 years ago

In 2.11.0

$fenom->getCompileName($template_name);