How to use the Plugin Otherwise in IBM WCM 8 and 9

challenge_accepted

The challenge

Imagine you want to run a kind of conditional if with a fallback (else) for example: you would like to have some custom HTML markup when there is no description set on a piece of content. Let’s see following pseudocode:

IF is_There_A_Description(current_Content)
    DISPLAY description
ELSE
    DISPLAY a default text

So, how could we emulate this behaviour on IBM WCM? Simple…

The solution

Let’s use a server-side solution using the NotEquals plugin, which is really easy to apply, let’s see an example:

[Plugin:NotEquals text1="" text2="[Property context='current' type='content' field='description']"]
	<div class="b-content__description">
               [Property context='current' type='content' field='description']
	</div>
[/Plugin:NotEquals]
[Plugin:Otherwise]<!-- This is our "else" -->
<!-- No description was set, do some custom logic here -->
[/Plugin:Otherwise]

 Explanation

The plugin NotEquals requires two mandatory parameters: text1 and text2 (both strings). In this example, we have text1=”” compared with text2=”[Property context=”current” type=”content” field=”description”].

When WCM renders this HTML with the [Property] meta-tag, it will compare the text2 with text1 and if text2 is not empty, the HTML inside the NotEquals plugin will be rendered, otherwise (else) no HTML will be generated. Simple and elegant.

However… be careful when combining / nesting conditionals:

[Plugin:NotEquals text1="" text2="[Property context='current' type='content' field='description']"]
    [Plugin:NotEquals text1="generic-description" text2="[Property context='current' type='content' field='description']"]
	<div class="b-content__description">
               [Property context='current' type='content' field='description']
	</div>
    [/Plugin:NotEquals]
[/Plugin:NotEquals]
[Plugin:Otherwise]<!-- This is our "else" -->
<!-- No description was set, do some custom logic here -->
[/Plugin:Otherwise]
Previous conditional will not work, since the Otherwise plugin is not going to know under which context should act.

IBM provides a way to solve this using what is is called scope. Let’s change a bit our code to set this scope parameter in BOTH NotEquals and Otherwise plugins.

[Plugin:NotEquals text1="" text2="[Property context='current' type='content' field='description']" scope="1"]
    [Plugin:NotEquals text1="generic-description" text2="[Property context='current' type='content' field='description']" scope="2"]
	<div class="b-content__description">
               [Property context='current' type='content' field='description']
	</div>
    [/Plugin:NotEquals]
[/Plugin:NotEquals]
[Plugin:Otherwise scope="1"]<!-- This is our "else" with scope="1" which means, it will act with the first NotEquals plugin -->
<!-- No description was set, do some custom logic here -->
[/Plugin:Otherwise]

Otherwise plugin restrictions

The Otherwise plugin ONLY works with:

  • Equals plug-in
  • NotEquals plug-in
  • Matches plug-in

I hope you find this information useful, and remember, be happy with your code!

Leave a Reply

Your email address will not be published. Required fields are marked *