dd / evolutioncms-snippets-ddif
This snippet compares different values and returns required chunk or string.
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Type:modxevo-snippet
Requires
- php: >=5.6.0
- dd/evolutioncms-libraries-ddtools: >=0.60.0
This package is auto-updated.
Last update: 2024-12-06 08:15:10 UTC
README
This snippet compares different values and returns required chunk or string.
Requires
- PHP >= 5.6
- (MODX)EvolutionCMS >= 1.1
- (MODX)EvolutionCMS.libraries.ddTools >= 0.60
Installation
Using (MODX)EvolutionCMS.libraries.ddInstaller
Just run the following PHP code in your sources or Console:
//Include (MODX)EvolutionCMS.libraries.ddInstaller require_once( $modx->getConfig('base_path') . 'assets/libs/ddInstaller/require.php' ); //Install (MODX)EvolutionCMS.snippets.ddIf \DDInstaller::install([ 'url' => 'https://github.com/DivanDesign/EvolutionCMS.snippets.ddIf', 'type' => 'snippet' ]);
- If
ddIf
is not exist on your site,ddInstaller
will just install it. - If
ddIf
is already exist on your site,ddInstaller
will check it version and update it if needed.
Manually
1. Elements → Snippets: Create a new snippet with the following data
- Snippet name:
ddIf
. - Description:
<b>2.3.1</b> This snippet compares different values and returns required chunk or string.
. - Category:
Core
. - Parse DocBlock:
no
. - Snippet code (php): Insert content of the
ddIf_snippet.php
file from the archive.
2. Elements → Manage Files
- Create a new folder
assets/snippets/ddIf/
. - Extract the archive to the folder (except
ddIf_snippet.php
).
Parameters description
-
operand1
- Desctription: The first operand for comparing.
An empty unparsed placeholder (like'[+somePlaceholder+]'
) will be interpretated as empty string (''
). - Valid values:
string
- Required
- Desctription: The first operand for comparing.
-
operand2
- Desctription: The second operand for comparing.
- Valid values:
string
- Default value:
''
-
operator
- Desctription: Comparing operator.
Values are case insensitive (the following names are equal:'isNumeric'
,'isnumeric'
,'ISNUMERIC'
, etc). - Valid values:
'=='
'!='
'>'
'<'
'<='
'>='
'bool'
'inArray'
'isNumeric'
'isWhitespace'
— checks ifoperand1
is just white space (an empty string is also considered as white space)'isIncludes'
— case-sensitive check ifoperand1
containsoperand2
- Default value:
'=='
- Desctription: Comparing operator.
-
trueChunk
- Desctription: This value is returning if result is true.
Available placeholders:
[+ddIfParams.operand1+]
— containsoperand1
value.[+ddIfParams.operand2+]
— containsoperand2
value.[+ddIfParams.operator+]
— containsoperator
value.[+
any placeholders from theplaceholders
parameter+]
- Valid values:
stringChunkName
string
— use inline templates starting with@CODE:
- Default value:
''
- Desctription: This value is returning if result is true.
Available placeholders:
-
falseChunk
- Desctription: This value is returning if result is false.
Available placeholders:
[+ddIfParams.operand1+]
— containsoperand1
value.[+ddIfParams.operand2+]
— containsoperand2
value.[+ddIfParams.operator+]
— containsoperator
value.[+
any placeholders from theplaceholders
parameter+]
- Valid values:
stringChunkName
string
— use inline templates starting with@CODE:
- Default value:
''
- Desctription: This value is returning if result is false.
Available placeholders:
-
placeholders
- Desctription:
Additional data has to be passed into the
trueChunk
andfalseChunk
.
Nested objects and arrays are supported too:{"someOne": "1", "someTwo": "test" }
=>[+someOne+], [+someTwo+]
.{"some": {"a": "one", "b": "two"} }
=>[+some.a+]
,[+some.b+]
.{"some": ["one", "two"] }
=>[+some.0+]
,[+some.1+]
.
- Valid values:
stringJsonObject
— as JSONstringHjsonObject
— as HJSONstringQueryFormatted
— as Query string- It can also be set as a native PHP object or array (e. g. for calls through
$modx->runSnippet
):arrayAssociative
object
- Default value: —
- Desctription:
Additional data has to be passed into the
-
debugTitle
- Desctription: The title for the System Event log if debugging is needed.
Just set it and watch the System Event log. - Valid values:
string
- Default value: —
- Desctription: The title for the System Event log if debugging is needed.
Examples
String comparison
[[ddIf?
&operand1=`Test string 1`
&operator=`==`
&operand2=`Test string 2`
&trueChunk=`@CODE:The strings are equal.`
&falseChunk=`@CODE:The strings are not equal.`
]]
Returns:
The strings are not equal.
Checks if a value exists in an array
[[ddIf?
&operand1=`Apple`
&operator=`inArray`
&operand2=`Pear,Banana,Apple,Orange`
&trueChunk=`@CODE:Exists.`
&falseChunk=`@CODE:Not exists.`
]]
Returns:
Exists.
Check if operand1
contains operand2
[[ddIf?
&operand1=`The quick brown fox jumps over the lazy dog.`
&operator=`isIncludes`
&operand2=`fox`
&trueChunk=`@CODE:“fox” is found`
&falseChunk=`@CODE:“fox” is not found`
]]
Returns:
“fox” is found
Checks if a operand1
value is number or not
[[ddIf?
&operand1=`123`
&operator=`isNumeric`
&trueChunk=`@CODE:Number.`
&falseChunk=`@CODE:Not number.`
]]
Returns:
Number.
Checks if a operand1
value is just white space or not
Any number of spaces / tabs / new lines / etc are considered as white space. An empty string is also considered as white space.
[[ddIf?
&operand1=`
`
&operator=`isWhitespace`
&trueChunk=`@CODE:The string contains only some whitespace characters.`
&falseChunk=`@CODE:[+ddIfParams.operand1+]`
]]
Returns:
The string contains only some whitespace characters.
If operand1
contains any non-whitespace characters, falseString
will be returned.
[[ddIf?
&operand1=`All you need is love.`
&operator=`isWhitespace`
&trueChunk=`@CODE:The string contains only some whitespace characters.`
&falseChunk=`@CODE:[+ddIfParams.operand1+]`
]]
Returns:
All you need is love.
Number comparison and pass additional data to chunks
[[ddIf?
&operand1=`[*general_price*]`
&operator=`<`
&operand2=`500`
&trueChunk=`general_goodInexpensive`
&falseChunk=`general_good`
&placeholders=`{
"title": "[*pagetitle*]",
"image": "[*general_image*]",
"somethingText": "All we need is love!"
}`
]]
Code of the general_good
chunk:
<div> <h2>[+title+], $[+ddIfParams.operand1+]</h2> <img src="[+image+]" alt="[+title+]" /> </div>
Code of the general_goodInexpensive
chunk:
<div class="inexpensive"> <h2>[+title+], <strong>$[+ddIfParams.operand1+]</strong></h2> <img src="[+image+]" alt="[+title+]" /> <p>[+somethingText+]</p> </div>
Let [*general_price*]
be equal to 120
, then the snippet returns:
<div class="inexpensive"> <h2>Some inexpensive good, <strong>$120</strong></h2> <img src="assets/images/goods/some1.jpg" alt="Some inexpensive good" /> </div>
Run the snippet through \DDTools\Snippet::runSnippet
without DB and eval
//Include (MODX)EvolutionCMS.libraries.ddTools require_once( $modx->getConfig('base_path') . 'assets/libs/ddTools/modx.ddtools.class.php' ); //Run (MODX)EvolutionCMS.snippets.ddIf \DDTools\Snippet::runSnippet([ 'name' => 'ddIf', 'params' => [ 'operand1' => '1', 'operator' => 'bool', 'trueChunk' => '@CODE:It's true!' ] ]);