Template Execution Context

In essence, configuration templates are PHP files which, when executed, output web server configuration files. The templates are executed in the environment where the specific variables $VAR and $OPT are available.

$VAR is an object containing the data model which should be applied to a template. The variable contains an essential set of parameters defining the content of web server configuration. The detailed structure of the array is presented in the Appendix A: Apache Configuration Variables.

The most important function is IncludeTemplate() which is part of the $VAR array. The function allows including templates one into another, and it is defined as

IncludeTemplate($templateName, $OPT, $metainfo)

where

 

The basic function usage is as follows:

## source: default/server.php
<?php echo $VAR->includeTemplate('server/tomcat.php') ?>
 

A text generated by the included template (server/tomcat.php) will be included in the configuration file.

In cases when the text generated by an included template should depend on the context, say, when iterating over a set of values, it is possible to pass additional parameters to the template.

 ## source: default/server.php
<?php echo $VAR->includeTemplate('service/php.php', array(
    'enabled' => false,
)) ?>

Here, we included the service/php.php template and passed the value 'enabled' => false to it. In the template being included the passed value is available in the variable $OPT:

 
## source: service/php.php
<?php
if ($OPT['enabled']) { // it is required to detect 'enabled'
    echo "php_admin_flag engine on\n";
    if (!array_key_exists('safe_mode', $OPT) || $OPT['safe_mode']) { // optional parameter 'safe_mode'
        echo "php_admin_flag safe_mode on\n";
    } else {
        echo "php_admin_flag safe_mode off\n";
    }
    if(array_key_exists('dir', $OPT) && $OPT['dir']) { // optional parameter 'dir'
        echo "php_admin_value open_basedir {$OPT['dir']}:/tmp\n";
    }
} else {
    echo "php_admin_flag engine off\n";
}
?>

The code in this sample will generate two different blocks of text depending on which value of the 'enabled' parameter is passed.

Note that $VAR, which contains the data model, can be used in templates being included as well. Some values of $VAR are defined using the content of $metainfo. For details on possible $metainfo content and how it affects a template context, refer to Appendix A: Apache Configuration Variables. For example, by defining the subDomainId value in the $metainfo parameter, it is possible to set an exact subdomain model available at $VAR->subDomain in a template being included:

## source: default/domainVhost.php
<?php
//going through all subdomains of current domain
foreach ($VAR->domain->physicalHosting->subdomains as $subdomain) {
    if ($subdomain->ssl) { //if SSL is enabled on a subdomain
        //include configuration for subdomain with enabled SSL
        echo $VAR->includeTemplate('domain/subDomainVirtualHost.php', array(
            'ssl' => true, // passing $OPT['ssl'] = true
        ), array(
            'subDomainId' => $subdomain->id, // define target subdomain for which a configuration file is being built
        ));
    }

    //include configuration for subdomain with disabled ssl
    echo $VAR->includeTemplate('domain/subDomainVirtualHost.php', array(
        'ssl' => false,
    ), array(
        'subDomainId' => $subdomain->id,
    ));
}
?>

 ## source: domain/subDomainVirtualHost.php
 ServerName "<?php echo $VAR->subDomain->asciiName ?>.<?php echo $VAR->domain->asciiName ?>:<?php echo $OPT['ssl'] ? $VAR->server->webserver->httpsPort : $VAR->server->webserver->httpPort ?>"

Please send us your feedback on this help page.