Skip to content

Basic concepts # Templates

hertsch edited this page Sep 14, 2013 · 4 revisions

The kitFramework is depending on a Model View Controller (MVC) pattern and uses the Twig template engine for the Views.

Assume your extension is named MyExtension, so the templates are expected in the path:

/kit2/extension/thirdparty/thirdParty/MyExtension/Template/default

Twig will access the templates with the Namespace, in this case:

@thirdParty/MyExtension

The subdirectory /default is also the name of the template = default.

The default template must exists. You (or the users of your extension) can add additional templates (directories), for example with another design or another functionality.

Using different templates

You can determine the template(s) in the configuration file framework.json:

"FRAMEWORK_TEMPLATES": [
    "red",
    "propangas24",
    "mytemplate",
    "default"
]

tells the kitFramework to search for the specified template first in the directory

/kit2/extension/thirdparty/thirdParty/MyExtension/Template/red

if the template does not exists, the kitFramework will look into

/kit2/extension/thirdparty/thirdParty/MyExtension/Template/propangas24

if the template does not exists, the kitFramework will step into

/kit2/extension/thirdparty/thirdParty/MyExtension/Template/mytemplate

and at least fall back into the /default directory.

If your extension support kitCommands the user can select a template with a parameter. Assume your kitCommand is named myextension then

~~ myextension template[mytemplate] ~~

will try to load the template mytemplate, otherwise the kitCommand will try to load the template red.

Parsing a template

In your controller use the kitFramework Utilities function templateFile():

$template = $this->app['utils']->templateFile('@thirdParty/MyExtension/Template', 'template.twig');

to determine which template should be used for parsing the desired file. This function is expecting two parameters:

  • '@thirdParty/MyExtension/Template' - the Namespace for the Templates of your extension
  • 'template.twig' - the file name of the template. You can also specify a subdirectory.

Now let Twig render the template:

return $this->app['twig']->render($template, $variables);

$variables is an associative array containing all variables given from the controller to the template engine.

Render a template

There are no restrictions in the usage of the template engine.

Please read the Twig Documentation to learn how to handle templates.

Clone this wiki locally