The controller

The controller

What are controller files?

The controller file controls a specific part or layout.

Controller files (control.php) contain all the commands needed to connect the model with the view. The PHP classes that mainly being used in these controller files are the H4 Temp and H4 Get classes.

Besides the part and layout a controller script can be used in the root path of the theme. However this is not very common. If used in the root then it is the very first script that is being included even before the layout and part. This concludes the systematical layout given in the topic about themes.

Standard variables

Several standard variables can be used in the controller file.
Depending on the theme level you can use some standard variables. These variables are named $aTheme, $aLayout and $aPart. Like the variable names suggest they contain the specific values that are assigned to the level you are in. So for example in the layout you can use $aTheme and $aLayout, when editing a part you can use all three ($aTheme, $aLayout and $aPart).

The array of a variable contains the data structure that was given in the model file. So when in the model file the variable $aModel['fields']['name'] is used then this variable is called in the part $aPart['name'].

View control

For controlling the output the class H4 Temp is used. This is the same class that is used for outputting the view.

For setting the variables in the view the class H4 Temp is used. This class is called in every level of the layout (so not in the theme level). In the layout level the object name is $oLayout and in the part level $oPart. Following up is a list of common commands used.

setVar($sKey,$sValue)

Assigns a variable with the value of $sValue to the template variable $sKey. Important is that the variable $sKey exists in the template. Example of the setVar function in the $oPart :

$oPart->setVar('sText', $aPart['text']);

setCss($sValue,[$sType])

Ads extra CSS styling to the template. $sValue can contain a link to a CSS file or the actual CSS styling script. Optional the type of the script can be added in $sType. Normally this function is only used in combination with the layout. This because the layout usually contains the header of the HTML. For example the style print or mobile. Example of the setCss function :

$oLayout->setCss('css/screen.css');

setJs($sValue,[$aCommands])

This function can be used in three ways. The first option is to assign a JavaScript file (important is that the file ends with the extension .js). This is done by passing the location of the JavaScript in $sValue. Example :

$oLayout->setJs(LIBSURL.'js/jquery.js');

The second option is to assign just some raw JavaScript in the $sValue variable. For example :

$oLayout->setJs('alert('Eureka!');');

The last option is to use the JavaScript plug-ins of the H4 Temp class. These plug-ins are located in the libs/h4plugins/js/ path. The creation of these plug-ins we explained later on. Important to know for now is that the assigning of the different variables in a plug-in works with arrays. So for example to use the simplevalidate plug-in use the following script :

$oLayout->setJs('simplevalidate', array('id'=>'#contactform'));

Special settings

There are several special controller file settings that effect the rendering,

There are several special controller file settings that effect the rendering, Following up is a list of some special settings that can be used in a controller file.

oneCss($bValue)

When true creates a set of CSS files that contains all the seperate CSS files and coding ordered by media. For example one CSS file for the screen, one for mobile and one for print. Default is false. The setting should only being set in the $oLayout object. Example:

$oLayout->oneCss(true);

oneJs($bValue)

When true creates a single JavaScript file that contains all the seperate JavaScript files and coding. Default is true. The setting should only being set in the $oLayout object. Example:

$oLayout->oneJs(true);

setJsBody($bValue)

When true places the JavaScript in the end of the HTML body. The reason for placing the JavaScript at the end of the body is for faster loading. Default is true. The setting should only being set in the $oLayout object. Example:

$oLayout->setJsBody(true);

setConstant($sName,$sValue)

Creates a constant variable that can be used in the template, CSS and JavaScript. In order to use within a CSS and JavaScript file oneJs and oneCss should be set true. The constant name will always being capitalized. The constant should only being set in the $oLayout object. For using the constant please check the view section. Example:

$oLayout->setConstant('language','English');