About Template Engine Smarty

This module adds the Smarty template engine to the TemplateEngineFactory

Category 1Development Tools
Modules that have more to do with assisting development of a site rather than managing its content.
Category 2Markup Generation
Markup modules that are called upon to generate or parse markup (like HTML). Markup modules are most often used on the front-end of a site (rather than admin).
Category 3Other Modules
Modules that have a unique purpose not covered by existing categories.
Release StateStable
Should be safe for use in production environments. *
Authorwanze
Module Version2.0.0
Class NameTemplateEngineSmarty
Compatibility2.3, 2.4, 2.5, 2.6, 2.7, 3.0
Date AddedJuly 3, 2014
Last UpdatedFebruary 28, 2019
Recommended ByNew recommendations may take up to 1 day to appear.

Instructions

This module's files should be placed in /site/modules/TemplateEngineSmarty/
How to install or uninstall modules

README

TemplateEngineSmarty

License: MIT ProcessWire 3

A ProcessWire module adding Smarty to the TemplateEngineFactory.

Requirements

  • ProcessWire 3.0 or newer
  • TemplateEngineFactory 2.0 or newer
  • PHP 7.0 or newer
  • Composer

The 1.x version of this module is available on the 1.x branch.Use this version if you still use TemplateEngineFactory 1.x.

Installation

Execute the following command in the root directory of your ProcessWire installation:

composer require blue-tomato/template-engine-smarty:^2.0

This will install the TemplateEngineSmarty and TemplateEngineFactory modules in one step. Afterwards, don't forgetto enable Smarty as engine in the TemplateEngineFactory module's configuration.

ℹ️ This module includes test dependencies. If you are installing on production with composer install, make sure topass the --no-dev flag to omit autoloading any unnecessary test dependencies!.

Configuration

The module offers the following configuration:

  • Template files suffix The suffix of the Smarty template files, defaults to tpl.
  • Provide ProcessWire API variables in Smarty templates API variables ($pages, $input, $config...)are accessible in Smarty, e.g. {{ config }} for the config API variable.
  • Debug If enabled, Smarty outputs debug information.
  • Compile Check If enabled, templates are recompiled whenever the source code changes.
  • Error Reporting If set to false, Smarty will silently ignore invalid variables (variables andor attributes/methods that do not exist) and replace them with a null value. When set to true,Smarty throws an exception instead
  • Escape HTML If enabled, templates will auto-escape variables. If you are using ProcessWiretextformatters to escape field values, do not enable this feature.

Extending Smarty

It is possible to extend Smarty after it has been initialized by the module. Hook the method TemplateEngineSmarty::initSmarty to register custom functions, extensions, global variables, filters etc.

Here is an example how you can use the provided hook to attach a custom function.

function foo_function($params, $smarty) {
  return 'bar';
};
wire()->addHookAfter('TemplateEngineSmarty::initSmarty', function (HookEvent $event) {
    /** @var \Smarty $smarty */
    $smarty = $event->arguments('smarty');
    $smarty->registerPlugin("function", "foo", "foo_function");
});
// ... and then use it anywhere in a Smarty template:
{foo}

The above hook can be put in your site/init.php file. If you prefer to use modules, put it into the module's init() method and make sure that the module is auto loaded.

Comments

No comments yet. Be the first to post!

Post a Comment

Your e-mail is kept confidential and not included with your comment. Website is optional.