IfElse Extension

This extension provides basic functionality for markdown based conditional statements to allow for the rendered content to have conditional requirements. The extension adds three commands: if, elif, and else. These commands must be used in the traditional order, where the first is if, optionally followed by any number of elif commands and optionally a final else command.

A complete list of the available configuration options are provided in Table 1 and the following sections demonstrate the use of the commands.

Table 1: Available configuration options for the ifelse extension.

KeyDefaultDescription
activeTrueToggle for disabling the extension. This only changes the initial active state, use setActive to control at runtime.
modules[]A list of python modules to search for functions; by default the 'ifelse.py' extension is included. All functions called must accept the extension as the first argument.

The "modules" configuration item provides a list of python modules to search when calling conditional functions. The default behavior of the module is equivalent to adding the following the "modules" item.

MooseDocs.extensions.ifelse:
    modules:
        - MooseDocs.extensions.ifelse

As such, any function within this module can be called by the "function" setting within the statements. This setting and the associated function is discussed further in the following section.

Simple if Statement

The "if" command requires that a function to evaluate be supplied using the "function" setting. This function must exist within the modules loaded by the extension using the "modules" configuration item (see Table 1).

This extension was originally created to allow content to depend on the application name, as such a scenario will be used to demonstrate use of the syntax. The most basic use is a single "if" statement. When the supplied function returns True the content is displayed (see Example 1, when False the content is ignored (see Example 2).

Example 1: Single "if" command, when the function returns True the content is displayed.

!if function=hasMooseApp('MooseApp')
The documentation contains 'MooseApp' information.

Example 2: Single "if" command, when the function returns False the content is ignored.

!if function=hasMooseApp('UnknownApp')
The documentation contains 'UnknownApp' information.

It is possible to flip the state of the return value and create an "if not" statement by prefixing the function with an exclamation mark, as shown in Example 3.

Example 3: Single "if not" statement, when the function returns False the content is displayed.

!if function=!hasMooseApp('UnknownApp')
The documentation does not contain 'UnknownApp' information.

The input to the "function" setting is expected to a function that is available within the modules loaded by the extension. The function must accept the IfElseExtension object as the first argument. The arguments supplied in the markdown text are appended.

For example, the hasMooseApp function shown in the above examples is defined as follows in the extension.

def hasMooseApp(ext, app):
    """Module function for searching for the existence of a registered application name."""
    return ext.hasRegistredApp(app)
(contrib/moose/python/MooseDocs/extensions/ifelse.py)

The complete list of settings available for the "if" command are provided in Table 2, the "function" setting is required.

Table 2: Available settings for the "if" command.

KeyDefaultDescription
styleNoneThe style settings that are passed to rendered HTML tag.
classNoneThe class settings to be passed to rendered HTML tag.
idNoneThe class settings to be passed to the rendered tag.
functionNoneThe function---with arguments---to evaluate. This setting is +required+.

Compound if/elif/else Statements

Creating if-elif-else statements is accomplished by using an if command followed by any number of elif commands and then a final (optional) else command. It is important to understand that the implementation actually uses separate commands. As such, each command the if, elif, and/or the else can use the inline or block version of the command definition. The extension will enforce that commands occur in the expected order.

Example 4: A if elif else statement that mixes the command between inline and block syntax.

!if function=hasMooseApp('UnknownApp')
This 'UnknownApp' was found, how did you do that?

!elif! function=hasMooseApp('MooseApp')
You application includes objects registered by MOOSE as follows.

```
registerMooseObject('MooseApp', Diffusion);
```
!elif-end!

!else
Your install is messed up!