20
20
*
21
21
* Each content module is supposed to be associated with a template file that defines the output.
22
22
*
23
- * The cache id of the template is calculated from the cacheId of the parent template and the name of the module
24
- * and an optional "cacheId" plugin parameter that is only necessary, if the same module is used several times in
25
- * the same parent template.
23
+ * If output caching is activated, the cache id of the template is calculated from the cacheId of the parent template
24
+ * and the name of the module and an optional "cacheId" plugin parameter that is only necessary, if the same module is used
25
+ * several times in the same parent template.
26
26
*/
27
27
abstract class AbstractContentModule {
28
28
@@ -33,53 +33,55 @@ abstract class AbstractContentModule {
33
33
private $ logger = null ;
34
34
35
35
/**
36
- * Constructor
37
- * @param $name Template filename name without .tpl extension (must exist in templates/modules)
38
- * @param $parentTemplate Template object that includes this content module
39
- * @param $params Associative array of parameters passed to the smarty plugin
36
+ * @see ContentModule::initialize()
40
37
*/
41
- public function __construct ( $ name , \Smarty_Internal_Template $ parentTemplate , array $ params ) {
42
- $ this ->name = $ name ;
38
+ public function initialize ( \Smarty_Internal_Template $ parentTemplate , array $ params ) {
39
+ $ this ->name = $ params [ ' name ' ] ;
43
40
$ this ->tpl = $ this ->getTemplateFile ();
44
41
if (!file_exists ($ this ->tpl )) {
45
42
throw new IllegalArgumentException ('The template file \'' .$ this ->tpl .'\' does not exist. ' );
46
43
}
47
44
$ this ->view = ObjectFactory::getInstance ('view ' );
48
45
$ this ->logger = LogManager::getLogger (get_class ($ this ));
49
46
47
+ $ isCaching = !$ parentTemplate ->cached ->has_nocache_code ;
48
+
50
49
// calculate cache id
51
- $ this ->cacheId = $ parentTemplate ->cache_id .'- ' .$ name .(isset ($ params ['cacheId ' ]) ? $ params ['cacheId ' ] : '' );
50
+ $ this ->cacheId = $ isCaching ? $ parentTemplate ->cache_id .'- ' .$ this -> name .(isset ($ params ['cacheId ' ]) ? $ params ['cacheId ' ] : '' ) : null ;
52
51
53
52
// handle parameters depending on the cache state of the parent template
54
53
$ cache = ObjectFactory::getInstance ('dynamicCache ' );
55
- if ($ parentTemplate ->isCached ()) {
54
+ if ($ isCaching && $ parentTemplate ->isCached ()) {
56
55
// get cached parameters
57
56
$ parentParams = $ cache ->exists ('module-cache ' , $ parentTemplate ->cache_id ) ? $ cache ->get ('module-cache ' , $ parentTemplate ->cache_id )['params ' ] : [];
58
57
$ this ->params = $ cache ->exists ('module-cache ' , $ this ->cacheId ) ? $ cache ->get ('module-cache ' , $ this ->cacheId )['params ' ] : array_merge ($ parentParams , $ params );
59
58
}
60
59
else {
61
- // use fresh parameters
62
- $ this ->params = $ params ;
63
- foreach ($ this ->getRequiredTemplateVars () as $ var ) {
64
- $ this ->params [$ var ] = $ parentTemplate ->getTemplateVars ($ var );
60
+ // use fresh parameters (including parent template variables)
61
+ $ this ->params = array_merge ($ params , $ parentTemplate ->getTemplateVars ());
62
+
63
+ if ($ isCaching ) {
64
+ // store parameters for later use
65
+ $ cache ->put ('module-cache ' , $ this ->cacheId , ['params ' => $ this ->params ]);
66
+ $ cache ->put ('module-cache ' , $ parentTemplate ->cache_id , ['params ' => $ this ->params ]);
65
67
}
66
- // store parameters for later use
67
- $ cache ->put ('module-cache ' , $ this ->cacheId , ['params ' => $ this ->params ]);
68
- $ cache ->put ('module-cache ' , $ parentTemplate ->cache_id , ['params ' => $ this ->params ]);
69
68
}
70
- // check parameters and assign them to the view
69
+
70
+ // check parameters
71
71
foreach ($ this ->getRequiredTemplateVars () as $ var ) {
72
72
if (!isset ($ this ->params [$ var ])) {
73
73
$ this ->logger ->error ('Parameter \'' .$ var .'\' is undefined. ' );
74
74
}
75
- else {
76
- $ this ->view ->setValue ($ var , $ this ->params [$ var ]);
77
- }
75
+ }
76
+
77
+ // assign paramters to the view
78
+ foreach (array_keys ($ this ->params ) as $ var ) {
79
+ $ this ->view ->setValue ($ var , $ this ->params [$ var ]);
78
80
}
79
81
}
80
82
81
83
/**
82
- * Render the module
84
+ * @see ContentModule::render()
83
85
*/
84
86
public function render () {
85
87
if (!$ this ->view ->isCached ($ this ->tpl , $ this ->cacheId )) {
0 commit comments