Core /

Closures

Encapsulating php code in templates by a closureFactory

Slots are rendered using templates, with a twist. Some Slots require, besides the slotValue, some references like select options (selectables) and grouping (multiples). This means a certain amount of logic and processing to detect the selectedValue or the checkedValue.

Boxary creates a closure to get the html for the specific slot. This is done using the closureMap to avoid multiple creates for the same slot. Not to mention the secure encapsulation of the closure, so no template can access scoped variables.

This is what happens in the closureMap (the Factory):

 $slot = function() {                   
     include 'slotTemplate.php'; 
     return $closure;                   
 };                                             

Whereas the slotTemplate.php looks like:

<?php                                                                                      
 $closure = function($args) {                                                    
     ..... some user code processing $args and deliver $text ...... 
     return $text;                                                                             
 };                                                                                              
 ?> 

Rendering the slot (getting the text to insert into the templet) goes like this:

// pathForInclude = $slotCode . '.php'
$slotCode = 'select.field'; 
$slotName='country';
$closureMap->set($slotName, $slotCode);
 .... // ....
// return the closure, created in closureMap by including a template
 $myClosure = $closureMap->get($slotName);
 $text = $myClosure($args);
 .... feed $text to the template 
 .... be aware of the tVars that the closure contains !!
 .... after all, it IS a template

One might wonder what $args would be. That is closure dependent, off course. For Slots the arguments $slotValue, $selectables, $multiples are used and handed over in a array like $args = array($slotValue, $selectables, $multiples). Well, that is, for the time being.
Some slotTemplates just return text -as is-, other calculate the selectedValue and/or process the slotValue as expression. Anything goes.