function.module.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
13 
14 /**
15  * Render a content module.
16  *
17  * Content modules must implement the 'wcmf\lib\presentation\ContentModule' interface
18  * and are configured in the 'ContentModule' configuration section that maps the
19  * content module name to the implementing class, e.g.:
20  *
21  * [ContentModule]
22  * next_events = \app\src\lib\content\NextEvents
23  * form = \app\src\lib\content\Form
24  *
25  * Module output is cached by default. To prevent it from being cached it must be
26  * wrapped within a {nocache} tag.
27  *
28  * Usage example:
29  * @code
30  * {module name='next_events' ...} {* cache module output *}
31  * {nocache}{module name='form' ...}{/nocache} {* don't cache module output *}
32  * @endcode
33  *
34  * @param $params Array with keys:
35  * - name: The name of the module that must exist as a key in the configuration section named 'ContentModule'
36  * + additional module specific parameters
37  * NOTE: All variables from the including template are passed to the module template.
38  * @param $template Smarty_Internal_Template
39  * @return String
40  */
41 function smarty_function_module($params, Smarty_Internal_Template $template) {
42  $requiredInterface = 'wcmf\lib\presentation\ContentModule';
43  $config = ObjectFactory::getInstance('configuration');
44  $modules = $config->getSection('ContentModule');
45 
46  // search content module
47  $name = $params['name'];
48  $moduleClass = isset($modules[$name]) ? $modules[$name] : null;
49  if ($moduleClass && class_exists($moduleClass) && in_array($requiredInterface, class_implements($moduleClass))) {
50  $contentModule = new $moduleClass();
51  $contentModule->initialize($template, $params);
52  }
53  else {
54  LogManager::getLogger(__FILE__)->error('Content class \''.$moduleClass.'\' for content module \''.$name.'\' does not exist or does not implement interface \''.$requiredInterface.'\'');
55  }
56 
57  // return content
58  return $contentModule ? $contentModule->render() : '';
59 }
60 ?>
LogManager is used to retrieve Logger instances.
Definition: LogManager.php:20
ObjectFactory implements the service locator pattern by wrapping a Factory instance and providing sta...