dd/evolutioncms-snippets-ddif

此软件包最新版本(2.3.1)没有可用的许可信息。

此代码段比较不同的值,并返回所需的块或字符串。

2.3.1 2023-06-03 13:21 UTC

This package is auto-updated.

Last update: 2024-09-06 07:39:55 UTC


README

此代码段比较不同的值,并返回所需的块或字符串。

要求

安装

使用 (MODX)EvolutionCMS.libraries.ddInstaller

只需在您的资源或 控制台 中运行以下PHP代码

//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'
]);
  • 如果您的网站上不存在 ddIf,则 ddInstaller 将安装它。
  • 如果您的网站上已经存在 ddIf,则 ddInstaller 将检查其版本并在需要时更新它。

手动

1. 元素 → 代码片段:创建一个新代码片段,使用以下数据

  1. 代码片段名称:ddIf
  2. 描述:<b>2.3.1</b> 此代码片段比较不同的值并返回所需的块或字符串。
  3. 类别:核心
  4. 解析 DocBlock:no
  5. 代码片段代码(PHP):插入存档中 ddIf_snippet.php 文件的内容。

2. 元素 → 管理文件

  1. 创建一个新文件夹 assets/snippets/ddIf/
  2. 将存档解压到该文件夹(除 ddIf_snippet.php 外)。

参数描述

  • operand1

    • 描述:比较的第一个操作数。
      一个空的未解析占位符(如 '[+somePlaceholder+]')将被解释为空字符串('')。
    • 有效值:字符串
    • 必需
  • operand2

    • 描述:比较的第二个操作数。
    • 有效值:字符串
    • 默认值:''
  • operator

    • 描述:比较运算符。
      值不区分大小写(以下名称相等:'isNumeric''isnumeric''ISNUMERIC' 等)。
    • 有效值
      • '=='
      • '!='
      • '>'
      • '<'
      • '<='
      • '>='
      • 'bool'
      • 'inArray'
      • 'isNumeric'
      • 'isWhitespace' — 检查 operand1 是否仅包含空白(空字符串也被视为空白)
      • 'isIncludes' — 区分大小写的检查 operand1 是否包含 operand2
    • 默认值:'=='
  • trueChunk

    • 描述:如果结果为真,则返回此值。可用的占位符
      • [+ddIfParams.operand1+] — 包含 operand1 的值。
      • [+ddIfParams.operand2+] — 包含 operand2 的值。
      • [+ddIfParams.operator+] — 包含 operator 的值。
      • [+来自 placeholders 参数的任何占位符+]
    • 有效值
      • stringChunkName
      • string — 使用以 @CODE: 开始的行内模板
    • 默认值:''
  • falseChunk

    • 描述:如果结果为假,则返回此值。可用的占位符
      • [+ddIfParams.operand1+] — 包含 operand1 的值。
      • [+ddIfParams.operand2+] — 包含 operand2 的值。
      • [+ddIfParams.operator+] — 包含 operator 的值。
      • [+来自 placeholders 参数的任何占位符+]
    • 有效值
      • stringChunkName
      • string — 使用以 @CODE: 开始的行内模板
    • 默认值:''
  • placeholders

    • 描述:必须将附加数据传递给 trueChunkfalseChunk
      也支持嵌套对象和数组
      • {"someOne": "1", "someTwo": "test" } => [+someOne+], [+someTwo+].
      • {"some": {"a": "one", "b": "two"} } => [+some.a+], [+some.b+].
      • {"some": ["one", "two"] } => [+some.0+], [+some.1+].
    • 有效值
      • stringJsonObject — 作为 JSON
      • stringHjsonObject — 作为 HJSON
      • stringQueryFormatted — 作为 查询字符串
      • 它也可以设置为原生PHP对象或数组(例如,通过 $modx->runSnippet 调用)
        • arrayAssociative
        • object
    • 默认值:—
  • debugTitle

    • 描述:需要调试时系统事件日志的标题。
      只需设置即可观察系统事件日志。
    • 有效值:字符串
    • 默认值:—

示例

字符串比较

[[ddIf?
	&operand1=`Test string 1`
	&operator=`==`
	&operand2=`Test string 2`
	&trueChunk=`@CODE:The strings are equal.`
	&falseChunk=`@CODE:The strings are not equal.`
]]

返回值

The strings are not equal.

检查值是否存在于数组中

[[ddIf?
	&operand1=`Apple`
	&operator=`inArray`
	&operand2=`Pear,Banana,Apple,Orange`
	&trueChunk=`@CODE:Exists.`
	&falseChunk=`@CODE:Not exists.`
]]

返回值

Exists.

检查 operand1 是否包含 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`
]]

返回值

“fox” is found

检查 operand1 的值是否为数字

[[ddIf?
	&operand1=`123`
	&operator=`isNumeric`
	&trueChunk=`@CODE:Number.`
	&falseChunk=`@CODE:Not number.`
]]

返回值

Number.

检查 operand1 的值是否仅为空白字符

任何数量的空格、制表符、换行符等都被视为空白字符。空字符串也被视为空白字符。

[[ddIf?
	&operand1=`
		 
	   
	    
	`
	&operator=`isWhitespace`
	&trueChunk=`@CODE:The string contains only some whitespace characters.`
	&falseChunk=`@CODE:[+ddIfParams.operand1+]`
]]

返回值

The string contains only some whitespace characters.

如果 operand1 包含任何非空白字符,则返回 falseString

[[ddIf?
	&operand1=`All you need is love.`
	&operator=`isWhitespace`
	&trueChunk=`@CODE:The string contains only some whitespace characters.`
	&falseChunk=`@CODE:[+ddIfParams.operand1+]`
]]

返回值

All you need is love.

数字比较并将附加数据传递到块中

[[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!"
	}`
]]

general_good 块的代码

<div>
	<h2>[+title+], $[+ddIfParams.operand1+]</h2>
	<img src="[+image+]" alt="[+title+]" />
</div>

general_goodInexpensive 块的代码

<div class="inexpensive">
	<h2>[+title+], <strong>$[+ddIfParams.operand1+]</strong></h2>
	<img src="[+image+]" alt="[+title+]" />
	<p>[+somethingText+]</p>
</div>

[*general_price*] 等于 120,然后片段返回

<div class="inexpensive">
	<h2>Some inexpensive good, <strong>$120</strong></h2>
	<img src="assets/images/goods/some1.jpg" alt="Some inexpensive good" />
</div>

通过 \DDTools\Snippet::runSnippet 运行片段,不使用数据库和评估

//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!'
	]
]);

链接