Tags:
tag this topic
create new tag
view all tags
---+ IF Statements The =%<nop>IF%= construct gives TWiki the power to include content in topics based on the value of simple expressions. =%<nop>IF{"CONDITION" then="THEN" else="ELSE"}%= In the example above, if CONDITION evaluates to TRUE, then THEN will be shown in the topic; otherwise ELSE will be shown. Note that because of the way TWiki evaluates, then whatever is in the THEN and ELSE parameters will already have been expanded by the time the condition is actually evaluated. The standard FormatTokens such as =$percnt= for =%= can be used in the THEN and ELSE parameters when you need to *delay evaluation* of (for example) a TWiki variable. The basic syntax of a condition is the same as [[QuerySearch][the syntax used for queries]], with *operators* <code>=</code>, <code>!=</code>, <code>~</code>, <code><</code>, <code>></code>, <code><=</code>, <code>>=</code>, <code>NOT</code>, <code>AND</code>, <code>OR</code>, <code>()</code>, and *functions* <code>lc()</code>, <code>uc()</code>, <code>d2n()</code>. In addition, the following *special operators* are supported: | =context= | True if the current context is set (see below) | | =allows= | ='X' allows 'Y'= is true if web/topic 'X' exists and allows access mode 'Y' for the current user. Web access rights are only checked if there is no topic called 'X'. | | =istopic= | =istopic 'X'= is true if topic 'X' exists | | =isweb= | =isweb 'X'= is true if web 'X' exists | | =ingroup= | ='X' ingroup 'Y'= is true if user 'X' is in group 'Y'. 'X' can be a login name or a wikiname. | | =defined= | True if a preference variable or url parameter of this name is defined. | | =isempty= | True if a preference variable, url parameter or session variable of this name has an empty value. It is equivalent to the expression =(!defined(x) || $x='')= | | =$= | expands a URL parameter or [[TWikiVariables][TWikiVariable]] name. Plugin handlers *are not called*. Built-in variables and user-defined preferences are supported. You can pass a limited subset of parameters to TWiki variables by enclosing the variable name in single quotes; for example, =$ 'VARIABLE{value}'=. The ='VARIABLE{value}'= string may *not* contain quotes (' or "). | | ={X}= | expands to the value of the configuration variable {X} - for example, ={ScriptUrlPath}= | You can test if a *configuration item* is set or not, using conditions such as ="{AntiSpam}{HideUserDetails}"=. It is not possible see the value of a configuration. Configuration items are defined in [[%SCRIPTURLPATH{"configure"}%][configure]]. You can test if a *context identifier* is set or not, using conditions such as ="context authenticated"=. Context identifier are used in TWiki to label various stages of the rendering process. They are especially useful for [[TWikiSkins][skin]] authors to find out where they are in the rendering process. The following context identifiers are available: | *id* | *context* | | =absolute_urls= | Set if absolute URLs are required | | =attach= | in attach script (see TWikiScripts) | | =authenticated= | a user is authenticated | | =body_text= | when the body text is being processed in a view (useful in plugin handlers) | | =can_login= | current environment supports login | | =changes= | in changes script (see TWikiScripts) | | =command_line= | the running script was run from the command line, and not from CGI | | =content_master= | with the current web, this site is the master of the federated sites (see ReadOnlyAndMirrorWebs) | | =content_slave= | with the current web, this site is a slave and another site in the federation is the master (see ReadOnlyAndMirrorWebs) | | =diff= | in rdiff script (see TWikiScripts) | | =edit= | in edit script (see TWikiScripts) | | =footer_text= | when the footer text is being processed in a view (useful in plugin handlers) | | =header_text= | when the header text is being processed in a view (useful in plugin handlers) | | =i18n_enabled= | when user interface <nop>I18N support is enabled (i.e., user can choose the language for UI) | | =inactive= | if active links such as 'edit' and 'attach' should be disabled; a read-only web puts all topics in this context (see ReadOnlyAndMirrorWebs) | | =login= & =logon= | in login / logon script (see TWikiScripts) | | =manage= | in manage script (see TWikiScripts) | | =new_topic= | if the topic doesn't already exist | | =oops= | in oops script (see TWikiScripts) | | =preview= | in preview script (see TWikiScripts) | | =register= | in register script (see TWikiScripts) | | =rename= | in rename script (see TWikiScripts) | | =resetpasswd= | in resetpasswd script (see TWikiScripts) | | =rss= | if this is an RSS skin rendering | | =save= | in save script (see TWikiScripts) | | =search= | in search script (see TWikiScripts) | | =statistics= | in statistics script (see TWikiScripts) | | =textareas_hijacked= | provided for use by editors that highjack textareas, and want to signal this fact. This is used by skins, for example, so they can suppress extra controls when textareas have been hijacked. | | =upload= | in upload script (see TWikiScripts) | | =view= | in view script (see TWikiScripts) | | =viewfile= | in viewfile script (see TWikiScripts) | | =rest= | in rest script (see TWikiScripts) | | =registration_supported= | registration is supported by the current <nop>UserMapper | | =registration_enabled= | set if ={Register}{EnableNewUserRegistration}= is on, and registrationis supported | | =passwords_modifyable= | set if the password manager support changing the password / email | In addition there is a context identifier for each enabled plugin; for example, if =Gallous<nop>TwistyPlugin= is installed __and enabled__, then the context ID =Gallous<nop>TwistyPluginEnabled= will be set. Other extensions may set additional context identifiers. The =%<nop>IF{}%= statement is deliberately kept simple. In particular, note that there is no way to conditionally execute a Set statement. If you need more sophisticated control over formatting, then consider using the SpreadSheetPlugin. Note also that while the [[QuerySearch][query syntax]] can be used to access form fields, there are some contexts in which an IF statement may be used where there is no topic context, or the topic context is not what you expected. ---++ Examples __1. TWiki variable defined or not__ <verbatim> %IF{"defined 'WIKINAME'" then="WIKINAME is defined" else="WIKINAME is not defined"}% </verbatim> __2. Compare TWiki variable__ <verbatim> You are %IF{ "$ WIKINAME='TWikiGuest' and not defined 'OPEN_DAY'" then="not" }% allowed to %IF{ "context view" then="view" else="edit"}% this TWiki today. </verbatim> __3. URL parameter__ <verbatim> %IF{ "defined 'search'" then="Search: $percntURLPARAM{search}$percnt" else="No search passed in"}% </verbatim> __4. Range test on URL parameter__ <verbatim> url param t is %IF{ "0 < $ t and $ t < 1000" then="in" else="out of"}% range. </verbatim> __5. Text comparison of URL parameter__ <verbatim> %IF{ "$'URLPARAM{scope}'='text'" then="Plain text search" }% </verbatim> __6. Configuration item set or not__ <verbatim> %IF{ "{AntiSpam}{HideUserDetails}" then="User details are hidden" }% </verbatim> __7. Plugin enabled test__ <verbatim> TablePlugin is %IF{ "context TablePluginEnabled" then="enabled" else="disabled" }%. </verbatim> expands to: %BR% TablePlugin is %IF{ "context TablePluginEnabled" then="enabled" else="disabled" }%. __8. Check access permissions__ <verbatim> You %IF{"'%TOPIC%' allows 'change'" then="can" else="cannot"}% change this topic. You %IF{"'Sandbox.TestTopic' allows 'change'" then="can" else="cannot"}% change Sandbox.TestTopic. You %IF{"'Sandbox' allows 'change'" then="can" else="cannot"}% change Sandbox web </verbatim> expands to: %BR% You %IF{"'%TOPIC%' allows 'change'" then="can" else="cannot"}% change this topic. %BR% You %IF{"'Sandbox.TestTopic' allows 'change'" then="can" else="cannot"}% change Sandbox.TestTopic. %BR% You %IF{"'Sandbox' allows 'change'" then="can" else="cannot"}% change Sandbox web __9. Check topic existence__ <verbatim> Topic Sandbox.TestTopic %IF{"istopic 'Sandbox.TestTopic'" then="exists" else="does not exist"}% Web Sandbox.WebHome %IF{"isweb 'Sandbox'" then="exists" else="does not exist"}% </verbatim> expands to: %BR% Topic Sandbox.TestTopic %IF{"istopic 'Sandbox.TestTopic'" then="exists" else="does not exist"}% %BR% Web Sandbox.WebHome %IF{"isweb 'Sandbox'" then="exists" else="does not exist"}% __10. Group membership__ <verbatim> You %IF{"'%USERNAME%' ingroup 'TWikiAdminGroup'" then="are an admin" else="are a normal user"}% </verbatim> expands to: %BR% You %IF{"'%USERNAME%' ingroup 'TWikiAdminGroup'" then="are an admin" else="are a normal user"}% __11. Conditional include__ <verbatim> %IF{"'%USERNAME%' ingroup 'ExecutiveGroup'" then="$percntINCLUDE{ExecutiveReport}$percnt"}% </verbatim> The !ExecutiveReport topic is only included if the user is in the !ExecutiveGroup. __12. Hide section of text conditionally using CSS display__ <verbatim> <div style="display: %IF{"'%USERNAME%' ingroup 'TWikiAdminGroup'" then="block" else="none"}%"> * Conditional text enclosed in div tags here... * ...can be as long as needed </div> </verbatim> Above text is only shown to users who are in the !TWikiAdminGroup. __Related Topics:__ QuerySearch, VarIF, VarGET, VarSET, VarSEARCH, FormattedSearch, FormatTokens, SpreadSheetPlugin, TWikiScripts -- __Contributors:__ TWiki:Main.ArthurClemens, TWiki:Main.CrawfordCurrie, TWiki:Main.PeterThoeny, TWiki:Main.SopanShewale, TWiki:Main.SvenDowideit, TWiki:Main.WillNorris - 2012-08-07
E
dit
|
A
ttach
|
Watch
|
P
rint version
|
H
istory
: r12
<
r11
<
r10
<
r9
<
r8
|
B
acklinks
|
V
iew topic
|
Ra
w
edit
|
M
ore topic actions
Topic revision: r12 - 2012-10-03
-
TWikiContributor
Log In
or
Register
TWiki Web
Users
Groups
Index
Search
Changes
Notifications
RSS Feed
Statistics
Preferences
User Reference
ATasteOfTWiki
TextFormattingRules
TWikiVariables
FormattedSearch
QuerySearch
TWikiDocGraphics
TWikiSkinBrowser
InstalledPlugins
Admin Maintenance
Reference Manual
AdminToolsCategory
InterWikis
ManagingWebs
TWikiSiteTools
TWikiPreferences
WebPreferences
Categories
Admin Documentation
Admin Tools
Developer Doc
User Documentation
User Tools
Prenotazioni esami
Laurea Triennale ...
Laurea Triennale
Algebra
Algoritmi
Introduzione agli algoritmi
Algoritmi 1
Algoritmi 2
Algoritmi per la
visualizzazione
Architetture
Prog. sist. digitali
Architetture 2
Basi di Dati
Basi di Dati 1 Inf.
Basi di Dati 1 T.I.
Basi di Dati (I modulo, A-L)
Basi di Dati (I modulo, M-Z)
Basi di Dati 2
Calcolo
Calcolo differenziale
Calcolo integrale
Calcolo delle Probabilitą
Metodi mat. per l'inf. (ex. Logica)
canale AD
canale PZ
Programmazione
Fond. di Programmazione
Metodologie di Programmazione
Prog. di sistemi multicore
Programmazione 2
AD
EO
PZ
Esercitazioni Prog. 2
Lab. Prog. AD
Lab. Prog. EO
Lab. Prog. 2
Prog. a Oggetti
Reti
Arch. di internet
Lab. di prog. di rete
Programmazione Web
Reti di elaboratori
Sistemi operativi
Sistemi Operativi (12 CFU)
Anni precedenti
Sistemi operativi 1
Sistemi operativi 2
Lab. SO 1
Lab. SO 2
Altri corsi
Automi, Calcolabilitą
e Complessitą
Apprendimento Automatico
Economia Aziendale
Elaborazione Immagini
Fisica 2
Grafica 3D
Informatica Giuridica
Laboratorio di Sistemi Interattivi
Linguaggi di Programmazione 3° anno Matematica
Linguaggi e Compilatori
Sistemi Informativi
Tecniche di Sicurezza dei Sistemi
ACSAI ...
ACSAI
Computer Architectures 1
Programming
Laurea Magistrale ...
Laurea Magistrale
Percorsi di studio
Corsi
Algoritmi Avanzati
Algoritmica
Algoritmi e Strutture Dati
Algoritmi per le reti
Architetture degli elaboratori 3
Architetture avanzate e parallele
Autonomous Networking
Big Data Computing
Business Intelligence
Calcolo Intensivo
Complessitą
Computer Systems and Programming
Concurrent Systems
Crittografia
Elaborazione del Linguaggio Naturale
Estrazione inf. dal web
Fisica 3
Gamification Lab
Information Systems
Ingegneria degli Algoritmi
Interazione Multi Modale
Metodi Formali per il Software
Methods in Computer Science Education: Analysis
Methods in Computer Science Education: Design
Prestazioni dei Sistemi di Rete
Prog. avanzata
Internet of Things
Sistemi Centrali
Reti Wireless
Sistemi Biometrici
Sistemi Distribuiti
Sistemi Informativi Geografici
Sistemi operativi 3
Tecniche di Sicurezza basate sui Linguaggi
Teoria della
Dimostrazione
Verifica del software
Visione artificiale
Attivitą complementari
Biologia Computazionale
Design and development of embedded systems for the Internet of Things
Lego Lab
Logic Programming
Pietre miliari della scienza
Prog. di processori multicore
Sistemi per l'interazione locale e remota
Laboratorio di Cyber-Security
Verifica e Validazione di Software Embedded
Altri Webs ...
Altri Webs
Dottorandi
Commissioni
Comm. Didattica
Comm. Didattica_r
Comm. Dottorato
Comm. Erasmus
Comm. Finanziamenti
Comm. Scientifica
Comm Scientifica_r
Corsi esterni
Sistemi Operativi (Matematica)
Perl e Bioperl
ECDL
Fondamenti 1
(NETTUNO)
Tecniche della Programmazione 1° modulo
(NETTUNO)
Seminars in Artificial Intelligence and Robotics: Natural Language Processing
Informatica generale
Primo canale
Secondo canale
II canale A.A. 10-11
Informatica
Informatica per Statistica
Laboratorio di Strumentazione Elettronica e Informatica
Progetti
Nemo
Quis
Remus
TWiki ...
TWiki
Tutto su TWiki
Users
Main
Sandbox
Home
Site map
AA web
AAP web
ACSAI web
AA2021 web
Programming web
AA2021 web
AN web
ASD web
Algebra web
AL web
AA1112 web
AA1213 web
AA1920 web
AA2021 web
MZ web
AA1112 web
AA1213 web
AA1112 web
AA1314 web
AA1415 web
AA1516 web
AA1617 web
AA1819 web
Old web
Algo_par_dis web
Algoreti web
More...
TWiki Web
User registration
Users
Groups
Index
Search
Changes
Notifications
RSS Feed
Statistics
Preferences
View
Raw View
Print version
Find backlinks
History
More topic actions
Edit
Raw edit
Attach file or image
Edit topic preference settings
Set new parent
More topic actions
User Reference
ATasteOfTWiki
TextFormattingRules
TWikiVariables
FormattedSearch
QuerySearch
TWikiDocGraphics
TWikiSkinBrowser
InstalledPlugins
Admin Maintenance
Reference Manual
InterWikis
ManagingUsers
ManagingWebs
TWikiSiteTools
TWikiPreferences
WebPreferences
Categories
Admin Documentation
Admin Tools
Developer Doc
User Documentation
User Tools
Account
Log In
Register User
Questo sito usa cookies, usandolo ne accettate la presenza. (
CookiePolicy
)
Torna al
Dipartimento di Informatica
E
dit
A
ttach
Copyright © 1999-2025 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback
Note:
Please contribute updates to this topic on TWiki.org at
TWiki:TWiki.IfStatements
.