Conditional Blocks

By tsangktsangk

Description

This snippets allows "conditional blocks" to be added to wiki pages. It checks for the two variables provided and if they are matching (or not matching), the content will be shown. Nesting within modules only work in advanced mode (see below). Thanks to James Kanjo for his awesome SUO snippet where I stole some of the front-end and back-end include coding.

Syntax

Basic

attribute required values default use
type yes equal or notequal none, required sets the type condition (testing for equal values or non-equal values
unique no any string singlemode for multiple condition blocks to be included into a single wiki page
var1 no any string or Wikidot variable 1 one of the variables or values to be tested in the block
var2 no any string or Wikidot variable 1 one of the variables or values to be tested in the block

Use in a data form template:

[[include :snippets:if START |unique=1|type=equal|var1=%%form_data{agree}%%|var2=iagree]]
You have agreed to the Terms and Conditions of the site.
[[include :snippets:if END]]

Advanced

Two separate components must be used with this. One outside a module, the other may be nested within a module.

:snippets:if-adv (PREPARE)

attribute required values default use
unique no any string singlemode a string to match the separate components
var1 no any string or Wikidot variable 1 the string that will be tested against in the nested component (var2

:snippets:if-adv-nest (START and END)

attribute required values default use
type yes equal or notequal none, required sets the type condition (testing for equal values or non-equal values
unique no any string singlemode this must match the unique id you used for the "prepare" block
var2 no any string or Wikidot variable 1 the variable which will be tested against the string defined as var1 in PREPARE.

Similar to James' SUO - but only for one person

[[include :snippets:if-adv PREPARE |var1=tsangk]]

[[module ListUsers users="."]]
[[include :snippets:if-adv-nest START|type=equal|var2=%%name%%]]
[/code:conditional-blocks/edit/true Edit this snippet!]
[[include :snippets:if-adv-nest END]]
[[/module]]

[[include :snippets:if-adv PREPARE |unique=1|var1=tsangk]]

[[module ListUsers users="."]]
[[include :snippets:if-adv-nest START|unique=1|type=notequal|var2=%%name%%]]
Hi %%name%%, you're not tsangk!
[[include :snippets:if-adv-nest END]]
[[/module]]

In action

Advanced

Similar to James' SUO - but only for one person

[[include :snippets:if-adv PREPARE |unique=advanced-eg|var1=tsangk]]

[[module ListUsers users="."]]
[[include :snippets:if-adv-nest START|type=equal|unique=advanced-eg|var2=%%name%%]]
[/code:conditional-blocks/edit/true Edit this snippet!]
[[include :snippets:if-adv-nest END]]
[[/module]]

[[module ListUsers users="."]]
[[include :snippets:if-adv-nest START|type=notequal|unique=advanced-eg|var2=%%name%%]]
Hi %%name%%, you're not tsangk!
[[include :snippets:if-adv-nest END]]
[[/module]]

would render:

Notes

  • For Conditional Blocks to work inside modules, use the advanced version.
  • Unfortunately, variables with spaces will not work in this snippet :(

in Action-live

on the code:_template this is inserted:


Thanks to tsangk for this great snippet: conditional-blocks


text above inserted with:

[[include :snippets:if START |unique=1|type=equal|var1=%%name%%|var2=conditional-blocks]]
**##red|Thanks to tsangk for this great snippet:##** [[[code:conditional-blocks]]]
[[include :snippets:if END]]



Other snippets posted by tsangk

URL Title with Slash Redirect - 04 Jan 2018 11:32
404 (Page does not exist) Redirect - 20 Nov 2012 11:40
Custom HTML Blocks - 25 Oct 2012 11:30
Syntax Highlighter - 16 Feb 2012 01:45


Rate this solution

If you think this solution is useful — rate it up!

rating: +10+x
Add a New Comment
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License