Jump to content

Smarty Templating for ProcessWire


niutech
 Share

Recommended Posts

Hi folks!

First of all, I would like to thank @Ryan for such a flexible and nifty CMS as ProcessWire! However, I am used to Smarty template engine, which PW is lacking, so I have made a module to enable Smarty 3 templating, as well as I have converted the default PHP template to Smarty template.

The source code is on GitHub: Smarty Templating for ProcessWire

This is the initial version, so please send me your feedback. For syntax info, go to the documentation. Enjoy!

  • Like 8
Link to comment
Share on other sites

Thanks for sharing.

"Just copy the SmartyTemplating folder to your /wire/modules/" ...

That's plain wrong. Never use /wire for modules and such. It's the core and if you provide 3rdparty modules they are always in /site/modules.

  • Like 3
Link to comment
Share on other sites

Cool, thanks for making this!

I know smarty from my past, I liked the "template inheriting" stuff.

One question: As you can't use php tags in templates know, what do you do if you need php logic? Where do you handle this?

Cheers

Link to comment
Share on other sites

Greetings niutech,

Thanks for putting this together.

There has been much debate here regarding template engines. Personally, I like getting as close as possible to "pure" PHP. No matter which side of the template engine side you're on, being able to use Smarty (for example) will help make ProcessWire more appealing for people who are accustomed to those systems.

I suppose the goal could be like the attitude at Symfony, where Twig works naturally but developers can also just use PHP as needed.

Thanks,

Matthew

  • Like 1
Link to comment
Share on other sites

Great work niutech! Thanks for putting this together. This seems like a really nice implementation you've put together here that plugs in effortlessly. In addition to being a great solution for those that want to use Smarty, it's also a great example of how one can implement a template engine with ProcessWire. Excellent readme too. 

One suggestion, related to what Soma said. Though this is minor. Your repo has a /wire/modules/ directory, and you might want to move /modules/ to your /site-default/ directory. Ultimately you can run a module from /wire/modules/ but it'll get wiped out every time you upgrade. So it's just a lot safer to keep them in /site/modules/, which gets retained through upgrades. Since people often skim through a readme, they might use your /wire/ dir as a guide rather than the readme. While less important, it might also be better to use /site/ rather than /site-default/, since /site-default/ is just a dir that would be present on an uninstalled copy of PW. 

Please add this to the modules directory at your convenience: http://modules.processwire.com/add/

  • Like 2
Link to comment
Share on other sites

@Ryan, Thanks for your tip, I have refactored the repo accordingly.

@MatthewSchenker, As to evaluating PHP in Smarty templates, it is highly discouraged by Smarty itself, but it's still technically possible. Just replace: Smarty.class.php to: SmartyBC.class.php and: new Smarty(); to: new SmartyBC(); on lines 32-33 and insert a new line 43: $this->smarty->php_handling = Smarty::PHP_ALLOW; in SmartyTemplating.module file.

@Wanze: I don't think it's necessary, since Smarty does it automatically: If $compile_check is enabled (default), every template file and config file that is involved with the cache file is checked for modification. If any of the files have been modified since the cache was generated, the cache is immediately regenerated.

Link to comment
Share on other sites

@Wanze: I don't think it's necessary, since Smarty does it automatically: If $compile_check is enabled (default), every template file and config file that is involved with the cache file is checked for modification. If any of the files have been modified since the cache was generated, the cache is immediately regenerated.

Yep that belongs to the template file itself.

But when you change for example fields like title, body, images etc in ProcessWire, Smarty doesn't know.

You won't see the changes on your website until the cache time is up.

  • Like 1
Link to comment
Share on other sites

niutech, if you want to clear the cache after save like Wanze mentioned, you could do it like this:

public function init() {
  $this->pages->addHookAfter('save', $this, 'clearCache'); 
  $this->pages->addHookAfter('saveField', $this, 'clearCache'); // optional
}

public function clearCache(HookEvent $e) {
  $page = $e->arguments[0]; // if you need it
  // clear cache
}
Link to comment
Share on other sites

@Wanze, I thought by saving page you meant saving the template page, not the page contents. But now I have just added hooks to clear the cache on page & field save, using Ryan's snippet.

Please give it a try and tell me if it works for you.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...