ifEval
Snippet for comparison operations on TV, placeholders and other values.
Use the correct PHP code for input parameter, as the expression in the ’IF’ function in PHP.
IMPORTANT: This snippet uses PHP EVAL function. It should be used with careful security precautions.
@ author Djordje Dimitrijev (dj13 on Modx forum)
@ version 0.9 beta
CURRENT VERSION NOTES:
Added possibility for unlimited number of snippet parameters.
New way for naming parameters.
Added support for placholders.
Added processElementTags suggested by Bert (bertoost on Modx forum).
And much more
VERSION 0.1 pl NOTES:
First initial version.
IMPORTANT:
Support for default naming parameters from ifEval v0.1 (input, contentFalse, contentTrue)
will be removed in next final version.
USAGE:
First select a name as a base for the parameter names. Say, let it be MyVar
All possible parameters with MyVar are (7):
ccMyVar - Statement expresion (require).
rrMyVarTrue - (Opt) Will be returned if the statement is correct.
rrMyVarFalse - (Opt) Will be returned if the statement is not correct.
evalMyVarTrue - (Opt) Evaluate and return if the statement is correct.
evalMyVarFalse - (Opt) Evaluate and return if the statement is not correct.
chunkMyVarTrue (Opt) and
chunkMyVarFalse (Opt) must be in JSON format in the following form:
{"chunk_name" : {"placeholder_name": "content_for_placeholder_name" ,
"other_placholder": "content_for_other_placholder"},
{"other_chunk_name" : {"placeholder_123": "content_for_placeholder_123"}}
Special: evalute content for placholder. Put the ($evalReturnPrefix default "eval") on the placholder:
{"third_chunk_name" : {"evalThisPlaceholder": "[[mySnippet]] * 1.18"}
$jsonQuote - Because of restrictions JSON format, and needs for quotation marks, you can use $jsonQuote default "%%"
{"fourth_chunk_name" : {"evalPlaceholder123": "[[+price]] + %%[[delivery]]%%"}
Output from the snippet will be in the order of parameters in the snippet call.
EXAMPLES:
In template with contentTrue and contentFalse parameter:
[[!ifEval? &ccName123=`stripos("[[*pagetitle]]", "a")===0` &rrName123True=`<h3>Article with first letter A</h3>` &rrName123False=`<h3>Article with first letter different from A</h3>`]]
In template with with several different parameters and evaluate return:
[[!ifEval?
&ccABC=`[[*id]]==5 && "[[*group]]"=="phone"`
&rrABCTrue=`<img src="phone_icon.jpg" />`
&evalABCTrue=`"<h4>Phone price: ".([[*price]]+[[!delivery? &product=`phone`]])."</h4>"`
&ccQWE=`[[*id]]==5 && "[[*group]]"=="notebook"`
&rrQWETrue =`<img src="notebook_icon.jpg" />`
&evalQWETrue=`"<h4>Notebook price: ".([[*price]]+[[!delivery? &product=`notebook`]])."</h4>"`
&ccXYZ=`[[*id]]!=5 and [[*id]]!=23`
&rrXYZTrue=`[[$comment]]`
&rrXYZFalse=`[[$links]]`
]]
With chunk parameter in JSON format:
[[!ifEval?
&ccCondition=`"[[+user]]"=="registered"`
&chunkConditionTrue=
`{
"myChunk" :
{
"name": "[[+name]]" ,
"phone": "[[+phone]]",
"email": "[[+email]]"
}
}`
&chunkConditionFalse=
`{
"myChunk2" :
{
"name": "[[+name]]",
"evaldate": "date(%%d-m-y%%,time())"
}
}`
&rrConditionTrue=`<a href="login.php">Login</a>`
]]
Sorry for my english.