6.5 Conditional Expressions

SSI allows for some programmability in its own right through the use of conditional expressions. The structure of this conditional construct is:

<!--#if expr="test_condition" -->
<!--#elif expr="test_condition" -->
<!--#else -->
<!--#endif -->

A test condition can be any sort of logical comparison, either comparing values to one another, or testing the 'truth' of a particular value.

The source of variables used in conditional expressions is distinct from the set of global data used by the Python code executed within a page. Instead, the variables are sourced from the subprocess_env table object contained within the request object. The values of these variables can be set from within a page using the SSI 'set' directive, or by a range of other Apache directives within the Apache configuration files such as BrowserMatchNoCase and SetEnvIf.

To set these variables from within a mod_python handler, the subprocess_env table object would be manipulated directly through the request object.

from mod_python import apache

def fixuphandler(req):
    debug = req.get_config().get('PythonDebug', '0')
    req.subprocess_env['DEBUG'] = debug
    return apache.OK

If being done from within Python code contained within the page itself, the request object would first have to be accessed via the filter object.

<!--#python exec="
debug = filter.req.get_config().get('PythonDebug', '0')
filter.req.subprocess_env['DEBUG'] = debug
" -->
<html>
<body>
<!--#if expr="${DEBUG} != 0" -->
DEBUG ENABLED
<!--#else -->
DEBUG DISABLED
<!--#endif -->
</body>
</html>