Using variables as the conditions for (in)visible content

Posted by GoVegan on 07 Dec 2014 02:57, last edited by GoVegan on 07 Dec 2014 07:18

Tags:

rating: +3+x

Variables are powerful tools used in Wikidot's Live Templates, modules and includes.

%%tags%% is an example of a variable. If a page has no tags, then using this variable will render nothing. If a page is tagged, then those tags will be displayed.

If you want a certain category to display a list of tags at the top of each page, then you may use the following code:

This page has the following tags: %%tags%%

If the page has tags, the following could be rendered:

This page has the following tags: diet fitness health

However, if the page has no tags, the following will be rendered:

This page has the following tags:

As you can see, this does not appear professional.

Continue reading this "How to …" if you want to:

  • Show content if a variable is blank or has a null value
    OR Hide content only if a variable has a value
  • Show content only if a variable has a value
    OR Hide content if a variable is blank or has a null value

For the purpose of demonstrating working examples, this document will use %%tags%% as the conditional variable. Of course, %%tags%% could be replaced with any other existing variable, and the code will still function as expected.

How to render if variable is blank

If you want to display content if a variable is blank, use the following syntax:

[!----%%VARIABLE%%]CONDITIONAL CONTENT[!----]

A practical use for this code could be to inform your users that "This page has not been tagged."

[!----%%tags%%]This page has not been tagged.[!----]

How to render if variable has a value

Hiding content when a variable is blank is a little more complicated to do, however it is possible by extending on the code from the previous section.

If you want to display content if a variable has a value, and is not blank, use the following syntax:

[!----%%VARIABLE%%][%%VARIABLE%%!--]CONDITIONAL CONTENT[!----]

A practical use for this code is the same example used in the introduction of this document:

[!----%%tags%%][%%tags%%!--]This page has the following tags: %%tags%%[!----]

You may note from the above example, that you can use the conditional variable within the content you wish to display.

Author

James KanjoJames Kanjo. Please visit his/her userPage.


Related articles

Comments

Add a New Comment
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.