Conditional Blocks

By tsangk tsangk

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
the wikidot code

Hi Tsangk…

Imagen I want to use this on only one page, and also to better understand what you're doing… Would it be possible to give the code without the includes on one page…


A - S I M P L E - P L A N by ARTiZEN a startingpoint for simple wikidot solutions.

by Steven Heynderickx Steven Heynderickx , 18 Apr 2012 13:09
Re: the wikidot code
tsangk tsangk 18 Apr 2012 13:46

This is basically how it works:

[[module CSS]]
.if-equal-default{ display: none; }
.if-equal-VAR1{ display: block !important; }
[[/module]]
[[div class="if-equal-default if-equal-VAR2"]]
Hide content unless VAR1 = VAR2
[[/div]]

By default, the DIV block is hidden by the first statement .if-equal-default{ display: none; }. However, in the case when VAR1 = VAR2, the second line in the CSS module is matched, so the previous statement is overrided. The !important is actually redundant there, as CSS is processed line-by-line.

Let's say VAR1 was apple and VAR2 was banana, the code would render:

[[module CSS]]
.if-equal-default{ display: none; }
.if-equal-apple{ display: block !important; }
[[/module]]
[[div class="if-equal-default if-equal-banana"]]
APPLES ONLY!
[[/div]]


Therefore, the div won't be shown as by default (set by if-equal-default class), the div is not displayed.

Kenneth Tsang (@jxeeno)

by tsangk tsangk , 18 Apr 2012 13:46
Ok

Yes that is what I assumed it would be like… but what I am trying to understand is how did you get the var1 (apple) into the module listpages… that's what I'm after.

Have a look at http://feedback.wikidot.com/wish:780 … because you seemed to manage this
Have a look at http://community.wikidot.com/forum/t-462190/include-with-variable-acting-strange#post-1419497 because you seemed to manage this


A - S I M P L E - P L A N by ARTiZEN a startingpoint for simple wikidot solutions.

Last edited on 18 Apr 2012 14:42 by Steven Heynderickx
by Steven Heynderickx Steven Heynderickx , 18 Apr 2012 14:36
RobElliott RobElliott 09 Apr 2011 07:26

"Advanced….One inside outside a module, the other may be nested with a module."

Sorry, the English here doesn't make sense. Must it be inside or must it be outside? I assume it's outside.


Rob Elliott - Strathpeffer, Scotland - Wikidot first line support & community admin team.

Last edited on 09 Apr 2011 07:51 by RobElliott
by RobElliott RobElliott , 09 Apr 2011 07:26
tsangk tsangk 09 Apr 2011 07:54

Fixed. Is it clearer now?


Kenneth Tsang (@jxeeno)

by tsangk tsangk , 09 Apr 2011 07:54
RobElliott RobElliott 09 Apr 2011 08:14

Yes thanks. Now off to try it :)


Rob Elliott - Strathpeffer, Scotland - Wikidot first line support & community admin team.

by RobElliott RobElliott , 09 Apr 2011 08:14
It is time to write a new blog entry on the community!
Helmut_pdorf Helmut_pdorf 25 Mar 2011 06:36

Hi Kenneth,

Thanks again for the idea to use James Idea for such conditional comparisons!

I think it is time to write a new "blog" entry on the community-news about this snippet!

It works ..

Regards
Helmut


Service is my success. My webtips:www.blender.org (Open source), Wikidot-Handbook.

Sie können fragen und mitwirken in der deutschsprachigen » User-Gemeinschaft für WikidotNutzer oder
im deutschen » Wikidot Handbuch ?

by Helmut_pdorf Helmut_pdorf , 25 Mar 2011 06:36
Re: It is time to write a new blog entry on the community!
tsangk tsangk 25 Mar 2011 07:32

I would - but don't have the time. I have a few assignments I have to complete - and they are very time consuming :S


Kenneth Tsang (@jxeeno)

by tsangk tsangk , 25 Mar 2011 07:32
tsangk tsangk 24 Mar 2011 11:37

Thanks James :D


Kenneth Tsang (@jxeeno)

by tsangk tsangk , 24 Mar 2011 11:37
GoVegan GoVegan 24 Mar 2011 11:51

You credit me too much!

I'm glad my SUO helped you build this framework :)

The clever thing about SUO was that you use the same include for the opening and closing code blocks via neglecting attributes in the closing block. Whilst the backend coding is compleximicated to manufacture, it makes coding for the end-user easier.

What a great snippet tsangk ! It's going to be part of fantastic applications, never before possible!

by GoVegan GoVegan , 24 Mar 2011 11:51
RobElliott RobElliott 24 Mar 2011 12:32

I echo James' comments: great work which I look forward to implementing.


Rob Elliott - Strathpeffer, Scotland - Wikidot first line support & community admin team.

by RobElliott RobElliott , 24 Mar 2011 12:32
(account deleted) 24 Mar 2011 19:00

I'll add another echo to the chamber. This looks really cool and I'm also looking forward to giving it a spin.

Thanks Kenneth!

by (account deleted), 24 Mar 2011 19:00
ok - this works!
Helmut_pdorf Helmut_pdorf 24 Mar 2011 09:33

Sorry, but the template text is now ncouded in all your snippets….!


Service is my success. My webtips:www.blender.org (Open source), Wikidot-Handbook.

Sie können fragen und mitwirken in der deutschsprachigen » User-Gemeinschaft für WikidotNutzer oder
im deutschen » Wikidot Handbuch ?

by Helmut_pdorf Helmut_pdorf , 24 Mar 2011 09:33
Re: ok - this works!
tsangk tsangk 24 Mar 2011 09:36

Thanks Helmuti ~!


Kenneth Tsang (@jxeeno)

by tsangk tsangk , 24 Mar 2011 09:36
Re: ok - this works!
Helmut_pdorf Helmut_pdorf 24 Mar 2011 09:38

You can always change to the %%fullname%% for action-live to show the inlcude only here…

ok - i changed it to the one selected page only :
[[include :snippets:if START |unique=1|type=equal|var1=%%name%%|var2=conditional-blocks]]
@@


Service is my success. My webtips:www.blender.org (Open source), Wikidot-Handbook.

Sie können fragen und mitwirken in der deutschsprachigen » User-Gemeinschaft für WikidotNutzer oder
im deutschen » Wikidot Handbuch ?

Last edited on 24 Mar 2011 09:44 by Helmut_pdorf
by Helmut_pdorf Helmut_pdorf , 24 Mar 2011 09:38
Re: ok - this works!
tsangk tsangk 24 Mar 2011 09:40

It's fine. I don't mind it :D


Kenneth Tsang (@jxeeno)

by tsangk tsangk , 24 Mar 2011 09:40
Re: ok - this works!
tsangk tsangk 24 Mar 2011 09:49

You might also want to make bind the code in "code blocks":

text above inserted with:

[[div class="code"]]
@@[[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]]@@
[[/div]]

Kenneth Tsang (@jxeeno)

Last edited on 24 Mar 2011 09:51 by tsangk
by tsangk tsangk , 24 Mar 2011 09:49
tsangk tsangk 24 Mar 2011 09:30

Hi Shane and Helmuti! Thanks for checking my new snippet out!

I just updated the documentation with a nestable version. It's experimental and may be unstable - but check it out!

@Helmuti: would it be better to include the horizontal rule in within the conditional block:

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

EDIT: Oh.. you fixed it… nevermind :D

Kenneth Tsang (@jxeeno)

Last edited on 24 Mar 2011 09:31 by tsangk
by tsangk tsangk , 24 Mar 2011 09:30
I made a test on the code:_template
Helmut_pdorf Helmut_pdorf 24 Mar 2011 09:14

I inserted.

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

This results only in a source coding in the rendered output:

<div class="if-equal-1-formdata-hide if-equal-1-tsangk">

<p><strong><span style="color: red;">Thanks to tsangk for this great snippet:</span></strong> <a href="/code:conditional-blocks">conditional-blocks</a></p>
</div>

The css module inserted fpor Author: [[*user tsangk]]{ !!


Service is my success. My webtips:www.blender.org (Open source), Wikidot-Handbook.

Sie können fragen und mitwirken in der deutschsprachigen » User-Gemeinschaft für WikidotNutzer oder
im deutschen » Wikidot Handbuch ?

Last edited on 24 Mar 2011 09:24 by Helmut_pdorf
by Helmut_pdorf Helmut_pdorf , 24 Mar 2011 09:14
leiger leiger 24 Mar 2011 07:58

I've achieved this result before with CSS (with some help from Bryce, possibly… I forget) — but this is simpler to put into a page. Nice work!


~ Leiger - Wikidot Community Admin - Volunteer
Wikidot: Official Documentation | Wikidot Discord server

by leiger leiger , 24 Mar 2011 07:58
page revision: 16, last edited: 15 Feb 2012 00:36
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License
Click here to edit contents of this page.
Click here to toggle editing of individual sections of the page (if possible). Watch headings for an "edit" link when available.
Append content without editing the whole page source.
Check out how this page has evolved in the past.
If you want to discuss contents of this page - this is the easiest way to do it.
View and manage file attachments for this page.
A few useful tools to manage this Site.
Change the name (also URL address, possibly the category) of the page.
View wiki source for this page without editing.
View/set parent page (used for creating breadcrumbs and structured layout).

AltStyle によって変換されたページ (->オリジナル) /