De:TSrefConditions
From TYPO3Wiki
Contents |
Conditions
TYPOScript bietet die Möglichkeit auch Fallunterscheidungen innerhalb von TYPOScript vorzunehmen.
Die Informationen zu Conditions sind derzeit noch ein wenig zerstreut:
http://typo3.org/documentation/document-library/references/doc_core_tsref/current/view/4/1/
http://typo3.org/documentation/document-library/core-documentation/doc_core_ts/current/view/1/2/
Ein klassischer Fall für Conditions ist die Fallunterscheidung bei mehrsprachigen Webseiten: http://typo3.org/documentation/document-library/core-documentation/doc_l10nguide/current/view/1/2/
Hier steht z.B. der Hinweis, dass Conditions nicht innerhalb von geschleiften Klammern verwendet werden dürfen: http://typo3.org/documentation/document-library/core-documentation/doc_core_tstemplates/current/view/2/4/
Grundsätzliches zu Conditions
Mit Conditions werden können Bedingungen überprüft werden und bestimmte TYPOScript Konfigurationen an diese Bedingung genknüpft werden.
#Beispiel page.40 = HTML [browser = opera] page.40 { value = <h2>Sie surfen mit Opera</h2> } [else] page.40 { value = <h2>Sie surfen <b>nicht</b> mit Opera</h2> value.case = upper } [global]
Eine Bedingung wird mit Eckigen Klammern begonnen und mit einem [global] oder einem [end] geschlossen. Über die Bedingung [else] kann in dem Fall, dass die Prüfung nicht erfolgreich war ein anderer TYPOScript Code gesetzt werden.
Auf alle Werte wird die Funktion trim() angewendet, die die Leerzeichen am Anfang und am Ende eines Strings entfernt.
Es besteht die Möglichkeit mehrere Bedinungen über die Operatoren && (und) sowie || oder zu Verknüpfen. Der &&-Operator hat gegenüber dem ||-Operator Vorrang. D.h. es werden zuerst die Und-Verknüften Bedingungen ausgewertet und danach die Oder-Bedingungen. Alternativ kann anstelle von && auch "AND" und anstelle von || auch "OR" verwendet werde. Wenn kein Operator angegeben wird, wird eine Oder-Bedingung angenommen.
Innerhalb der Bedingung dürfen geschweifte Klammern verwendet werden.
Dagegen werden Bedingungen innerhalb von geschweiften Klammen nicht ausgewertet!
Beispiele: [browser = msie] && [system = win] page.40 = HTML page.40.value = Nur Internet Explorer unter Windows, nicht unter Mac! [global] [browser = opera] || [browser = netscape] page.40 = HTML page.40.value = Entweder Opera oder Netscape [global] [browser = msie] || [browser = netscape] && [version > 4] page.40 = HTML page.40.value = Entweder Internet Explorer oder Netscape mit einer Versionsnummer größer als 4 [global] # bei der Abfrage von Globalenvariablen kann # auch direkt eine Liste angegeben werden [globalVar = TSFE:id=12, TSFE:id=18, TSFE:id=24]
Browser
Syntax: [browser = browser1,browser2,...]
| Bezeichung: | Interer Bezeichner für den Vergleich: |
| Microsoft Internet Explorer | msie |
| Netscape Communicator | netscape |
| Lynx | lynx |
| Opera | opera |
| PHP fopen | php |
| AvantGo (www.avantgo.com) | avantgo |
| Adobe Acrobat WebCapture | acrobat |
| IBrowse (amiga-browser) | ibrowse |
| Teleport Pro | teleport |
| unbekannt, wenn "mozilla" nicht in useragent vorkommt | unknown |
Jeder Wert wird mit $browsername.$browserversion z.B. "netscape4.72" via strstr() verglichen.
[browser = scape] # Diese Bedingung trifft zu, wenn im Useragent der String "scape" vorkommt. # Alle Netscape-Browser werden von dieser Bedinung erfasst.
Es ist auch möglich, eine Liste mit Werten anzugegben, wobei es dann ausreicht, wenn ein einziger Wert zutrifft.
Beispiel: [browser = netscape, opera] page.40 = HTML page.40.value = Opera oder Netscape! [global]
version
Syntax: [version = value1, >value2, =value3, <value4, ...]
Comparison:
values are floating-point numbers with "." as the decimal separator.
The values may be preceeded by three operators:
| Operator: | Function: |
| [nothing] | The value must be part of the beginning of the version as a string. This means that if the version is "4.72" and the value is "4" or "4.7" it matches. But "4.73" does not match.
Example from syntax: "value1" |
| = | The value must match exactly. Version "4.72" matches only with a value of "4.72" |
| > | The version must be greather than the value |
| < | The version must be less than the value |
Examples: [version= =4.03] #This matches with exactly "4.03" browsers [global]
[version= >4][browser= netscape3] This matches with all 4+ browsers and netscape 3 browsers [global]
system
Syntax: [system= system1,system2]
Values and comparison:
| System: | Identification: |
| Linux | linux |
| SGI / IRIX | unix_sgi |
| SunOS | unix_sun |
| HP-UX | unix_hp |
| Macintosh | mac |
| Windows 3.11 | win311 |
| Windows NT | winNT |
| Windows 95 | win95 |
| Windows 98 | win98 |
| Amiga | amiga |
Values are strings an a match happens if one of these strings is the first part of the system-identification.
Fx. if the value is "win9" this will match with "win95" and "win98" systems.
Examples: This will match with windows and mac -systems only [system= win,mac]
device
Syntax: [device= device1, device2]
Values and comparison:
| Device: | Identification: |
| HandHeld | pda |
| WAP phones | wap |
| Grabbers: | grabber |
| Indexing robots: | robot |
Values are strings an a match happens if one of these strings equals the type of device
Examples: This will match WAP-phones and PDA's [device= wap, pda]
useragent
Syntax: [useragent= agent]
Values and comparison:
This is a direct match on the useragent string from getenv(“HTTP_USER_AGENT”)
You have the options of putting a "*" at the beginning and/or end of the value agent thereby matching with this wildcard!
Examples: If the HTTP_USER_AGENT is "Mozilla/4.0 (compatible; Lotus-Notes/5.0; Windows-NT)" this will match with it:
[useragent = Mozilla/4.0 (compatible; Lotus-Notes/5.0; Windows-NT)]
This will also match with it:
[useragent = *Lotus-Notes*]
... but this will also match with a useragent like this: "Lotus-Notes/4.5 ( Windows-NT )"
A short list of user-agent strings and a proper match:
| HTTP_USER_AGENT: | Agent description: | Matching condition: |
| Nokia7110/1.0+(04.77) | Nokia 7110 WAP phone | [useragent= Nokia7110*] |
| Lotus-Notes/4.5 ( Windows-NT ) | Lotus-Notes browser | [useragent= Lotus-Notes*] |
| Mozilla/3.0 (compatible; AvantGo 3.2) | AvantGo browser | [useragent= *AvantGo*] |
| Mozilla/3.0 (compatible; WebCapture 1.0; Auto; Windows) | Adobe Acrobat 4.0 | [useragent= *WebCapture*] |
WAP-agents:
This is some of the known WAP agents:
- ALAV UP/4.0.7
- Alcatel-BE3/1.0 UP/4.0.6c
- AUR PALM WAPPER
- Device V1.12
- EricssonR320/R1A
- fetchpage.cgi/0.53
- Java1.1.8
- Java1.2.2
- m-crawler/1.0 WAP
- Materna-WAPPreview/1.1.3
- MC218 2.0 WAP1.1
- Mitsu/1.1.A
- MOT-CB/0.0.19 UP/4.0.5j
- MOT-CB/0.0.21 UP/4.0.5m
- Nokia-WAP-Toolkit/1.2
- Nokia-WAP-Toolkit/1.3beta
- Nokia7110/1.0 ()
- Nokia7110/1.0 (04.67)
- Nokia7110/1.0 (04.67)
- Nokia7110/1.0 (04.69)
- Nokia7110/1.0 (04.70)
- Nokia7110/1.0 (04.71)
- Nokia7110/1.0 (04.73)
- Nokia7110/1.0 (04.74)
- Nokia7110/1.0 (04.76)
- Nokia7110/1.0 (04.77)
- Nokia7110/1.0 (04.80)
- Nokia7110/1.0 (30.05)
- Nokia7110/1.0
- PLM's WapBrowser
- QWAPPER/1.0
- R380 2.0 WAP1.1
- SIE-IC35/1.0
- SIE-P35/1.0 UP/4.1.2a
- SIE-P35/1.0 UP/4.1.2a
- UP.Browser/3.01-IG01
- UP.Browser/3.01-QC31
- UP.Browser/3.02-MC01
- UP.Browser/3.02-SY01
- UP.Browser/3.1-UPG1
- UP.Browser/4.1.2a-XXXX
- UPG1 UP/4.0.7
- Wapalizer/1.0
- Wapalizer/1.1
- WapIDE-SDK/2.0; (R320s (Arial))
- WAPJAG Virtual WAP
- WAPman Version 1.1 beta:Build W2000020401
- WAPman Version 1.1
- Waptor 1.0
- WapView 0.00
- WapView 0.20371
- WapView 0.28
- WapView 0.37
- WapView 0.46
- WapView 0.47
- WinWAP 2.2 WML 1.1
- wmlb
- YourWap/0.91
- YourWap/1.16
- Zetor
language
Syntax:
[language = lang1, lang2, ...]
Comparison:
The values must be a straight match with the value of getenv(“HTTP_ACCEPT_LANGUAGE”) from PHP. Alternatively, if the value is wrapped in “*” (eg. “*en-us*”) then it will split all languages found in the HTTP_ACCEPT_LANGUAGE string and try to match the value with any of those parts of the string. Such a string normally looks like “de,en-us;q=0.7,en;q=0.3” and “*en-us*” would match with this string.
IP
Syntax: [IP = ipaddress1, ipaddress2, ...]
Comparison:
The values are compared with the getenv(“REMOTE_ADDR”) from PHP.
You may include "*" instead of one of the parts in values. You may also list the first one, two or three parts and only they will be tested. Examples:
These examples will match any IP-address starting with "123":
[IP = 123.*.*.*] or [IP = 123] This examples will match any IP-address ending with "123" or being "192.168.1.34": [IP = *.*.*.123][IP = 192.168.1.34]
hostname
Syntax: [hostname = hostname1, hostname2, ...]
Comparison:
The values are compared with the fully qualiteid hostname of getenv(“REMOTE_ADDR”) retrieved by PHP.
Value is comma-list of domain names to match with. *-wildcard allowed but cannot be part of a string, so it must match the full host name (eg. myhost.*.com => correct, myhost.*domain.com => wrong)
hour
Syntax: [hour = hour1, >hour2, <hour3, ...]
Comparison:
The values in floating point are compared with the current hour (24-hours-format) of the server.
| Operator: | Function: |
| [nothing] | Requires exact match |
| > | The hour must be greather than the value |
| < | The hour must be less than the value |
minute
See "Hour" above. Same syntax!
Syntax: [minute = ...]
Comparison:
Minute of hour, 0-59
dayofweek
See "Hour" above. Same syntax!
Syntax: [dayofweek = ...]
Comparison:
Day of week, starting with sunday being 0 and saturday being 6
dayofmonth
See "Hour" above. Same syntax!
Syntax: [dayofmonth = ...]
Comparison:
Day of month, 1-31
month
See "Hour" above. Same syntax!
Syntax: [month = ...]
Comparison:
Month, january being 1 and december being 12
usergroup
Syntax: [usergroup = group1-uid, group2-uid, ...]
Comparison:
The comparison can only return true if the grouplist is not empty (global var "gr_list").
The values must either exists in the grouplist OR the value must be a "*". Example:
#This matches all logins [usergroup = *]
#This matches logins from users members of groups with uid's 1 and/or 2: [usergroup = 1,2]
loginUser
Syntax: [loginUser = fe_users-uid, fe_users-uid, ...]
Comparison:
Matches on the uid of a logged in fe_user. Works like 'usergroup' above including the * wildcard to select ANY user.
#Example: This matches any login (use this instead of “[usergroup = *]” to match when a user is logged in!): [loginUser = *]
treeLevel
Syntax: [treeLevel = levelnumber, levelnumber, ...]
Comparison:
This checks if the last element of the rootLine is at a level corresponding to one of the figures in "treeLevel". Level = 0 is the "root" of a website. Level=1 is the first menuen
Example: This changes something with the template, if the page viewed is on level either level 0 (basic) or on level 2 [treeLevel = 0,2]
PIDinRootline
Syntax: [PIDinRootline = pages-uid, pages-uid, ...]
Die aktuelle Seite ist eine der angegebenen Seiten oder davon eine Unterseite
#Beispiel [PIDinRootline = 12,13] page.40 = HTML page.40.value = <h1>Hallo Welt</h1> [global]
Comparison:
This checks if one of the figures in "treeLevel" is a PID (pages-uid) in the rootline
Example: This changes something with the template, if the page viewed is or is a subpage to page 34 or page 36 [PIDinRootline = 34,36]
PIDupinRootline
Die aktuelle Seite ist eine Unterseite der angegebenen Seiten.
Syntax: [PIDupinRootline = pages-uid, pages-uid, ...]
Comparison:
Do the same as PIDinRootline, except the current page-uid is excluded from check.
compatVersion
Syntax: [compatVersion = x.y.z]
Comparison:
Require a minimum compatibility version. This version is not necessary equal with the TYPO3 version, it is a configurable value that can be changed in the Upgrade Wizard of the Install Tool.
“compatVersion” is especially useful if you want to provide new default settings but keep the backwards compatibility for old versions of TYPO3.
globalVar
Syntax: [globalVar= var1=value, var2<value2, var3>value3, ...]
Comparison:
The values in floating point are compared with the global var "var1" from above.
| Operator: | Function: |
| [nothing] | Requires exact match |
| > | The var must be greather than the value |
| < | The var must be less than the value |
# Beispiel: für ein Plugin mit der id 37 die Plugin-Konfiguration ändern
[globalVar = TSFE:tt_content|uid = 37]
plugin.tt_news._LOCAL_LANG.de.preAuthor = Autor:
[global]
beUserLogin
# Wenn eine Person im Backend eingeloggt ist, # werden die Backendbenutzer-Seiten angezeigt, da # excludeDoktypes default-Mäßig auf 5,6 steht und damit # auch die Backendbenutzerseiten ausblendet. # Damit normale Benutzer die Backendbenutzerseiten nicht sehen können # wird die Bedingung verwendet. lib.menu = HMENU # Default Wert! lib.menu.excludeDoktypes = 5,6 [globalVar = TSFE : beUserLogin > 0] lib.menu.excludeDoktypes = 5 [global]
globalString
Syntax: [globalString = var1=value, var2= *value2, var3= *value3*, ...]
Comparison:
This is a direct match on global strings.
You have the options of putting a "*" as a wildcard or using a PCRE style regular expression (must be wrapped in "/") to the value.
Examples: If the HTTP_HOST is "www.typo3.com" this will match with: [globalString = HTTP_HOST=www.typo3.com] This will also match with it: [globalString = HTTP_HOST= *typo3.com] ... but this will also match with a HTTP_HOST like this: "demo.typo3.com"
IMPORTANT NOTE ON globalVar and globalString:
You can use values from global arrays and objects by deviding the var-name with a "|" (vertical line).
Examples: The global var $HTTP_POST_VARS["key"]["levels"] would be retrieved by "HTTP_POST_VARS|key|levels"
Also note that it's recommended to program your scripts in compliance with the php.ini-optimized settings. Please see that file (from your distribution) for details.
Caring about this means that you would get values like HTTP_HOST by getenv(), you would retrieve GET/POST values with t3lib_div::GPvar(). Finally a lot of values from the TSFE object are useful. In order to get those values for comparison with “globalVar” and “globalString” conditions, you prefix that varname with either "IENV"/“ENV:” , “GP:”, “TSFE:” or “LIT:” respectively. Still the “|” divider may be used to separate keys in arrays and/or objects. “LIT” means “literal” and the string after “:” is trimmed and returned as the value (without being divided by “|” or anything)
Notice: Using the "IENV:" prefix is highly recommended to get server/environment variables which are system independant. Basically this will call and return the value from t3lib_div::getIndpEnv() function. With "ENV:" you get the raw output from getenv() which is NOT always the same on all systems!
Examples: This will match with a url like “...&print=1” [globalVar = GP:print > 0] This will match with a remote-addr begining with “192.168.” [globalString = IENV:REMOTE_ADDR = 192.168.*] This will match with the page-id being higher than 10: [globalVar = TSFE:id > 10]
This will match with the pages having the layout field set to “Layout 1”: [globalVar = TSFE:page|layout = 1] This will match with the user whose username is “test”: [globalString = TSFE:fe_user|user|username = test]
If the constant {$constant_to_turnSomethingOn} is “1” then this matches:
[globalVar = LIT:1 = {$constant_to_turnSomethingOn}]
userFunc
Syntax: [userFunc = user_match(checkLocalIP)]
Comparison:
This call the function “user_match” with the first parameter “checkLocalIP”. You write that function. You decide what it checks. Function result is evaluated as true/false.
Example: Put this function in your localconf.php file:
function user_match($cmd){
switch($cmd){
case "checkLocalIP":
if (strstr(getenv("REMOTE_ADDR"),"192.168")){
return true;
}
break;
case "checkSomethingElse":
// ....
break;
}
}
This condition will return true if the remote address contains “192.168” - which is what your function finds out.
[userFunc = user_match(checkLocalIP)]
