dd/evolutioncms-snippets-ddgetmultiplefield

此包的最新版本(3.10.0)没有提供许可证信息。

用于处理、操作和自定义输出结构化数据(JSON或由分隔符分隔的字符串)的代码片段。

3.10.0 2024-09-05 23:13 UTC

This package is auto-updated.

Last update: 2024-09-05 23:15:06 UTC


README

用于处理、操作和自定义输出结构化数据(JSON或由分隔符分隔的字符串)。由mm_ddMultipleFields小部件值输出的字段通过代码片段变得更加方便。

特性

  • 通过其ID获取所需文档(TV)的字段值。使用inputString_docFieldinputString_docId参数。
  • 按值和行号返回所需值的数量。使用startRowtotalRowsfilter参数。
  • 按列号返回所需值。使用columns参数。
  • 在返回之前按列值排序(包括多级排序)('ASC''DESC''RAND''REVERSE')。使用sortDirsortBy参数。
  • 使用行和列分隔符分隔数据输出。使用rowGluecolGlue参数。
  • 在输出之前删除空值。使用removeEmptyRowsremoveEmptyCols参数。
  • 在输出之前对值进行排版(使用代码片段ddTypograph)。使用typography参数。
  • 结果URL编码。使用urlencode参数。
  • 结果JSON编码。使用outputFormat参数。
  • 通过给定的行和列的模板(块)返回值(也提供了在行和列模板中的[+rowNumber+][+rowNumber.zeroBased+]占位符,其中包含行号)。使用rowTplcolTpl参数。
  • 以块形式返回结果(通过占位符发送附加数据)(使用outerTpl参数)。使用placeholders参数。

要求

安装

使用(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.ddGetMultipleField
\DDInstaller::install([
	'url' => 'https://github.com/DivanDesign/EvolutionCMS.snippets.ddGetMultipleField',
	'type' => 'snippet'
]);
  • 如果您的网站上不存在ddGetMultipleField,则ddInstaller将仅安装它。
  • 如果您的网站上已存在ddGetMultipleField,则ddInstaller将检查其版本并在需要时更新它。

手动

1. 元素 → 代码片段:创建一个新的代码片段,以下为所需数据

  1. 代码片段名称:ddGetMultipleField
  2. 描述:<b>3.10</b> 用于处理、操作和自定义输出结构化数据(JSON或由分隔符分隔的字符串)的代码片段。
  3. 类别:核心
  4. 解析文档块:
  5. 代码片段代码(PHP):插入存档中ddGetMultipleField_snippet.php文件的内容。

2. 元素 → 文件管理

  1. 创建一个新文件夹assets/snippets/ddGetMultipleField/
  2. 将存档提取到文件夹中(除了ddGetMultipleField_snippet.php)。

参数描述

inputString / inputString_docField参数对中需要一个。

  • inputString

    • 描述:包含值的输入字符串。
      • 也支持任何嵌套级别的JSON。
    • 有效值
      • stringJsonArrayJSON数组
      • stringJsonObjectJSON 对象
      • stringHjsonObjectHJSON 对象
      • stringHjsonArrayHJSON 数组
      • stringQueryFormatted查询字符串
      • stringSeparated — 由 inputString_rowDelimiterinputString_colDelimiter 分隔
    • 必需
  • inputString_docField

    • 描述:需要获取的文档字段/电视机的名称。
      • 如果传递了参数,则从字段/电视机获取输入字符串,并将 inputString 忽略。
    • 有效值:string
    • 默认值:—
  • inputString_docId

    • 描述:需要获取字段/电视机值的文档的 ID。
    • 有效值:integer
    • 默认值:$modx->documentIdentifier(当前文档 ID)
  • inputString_rowDelimiter

    • 描述:当 inputString 不是 JSON 时,输入字符串的行分隔符。
    • 有效值
      • string
      • regexp
    • 默认值:'||'
  • inputString_colDelimiter

    • 描述:当 inputString 不是 JSON 时,输入字符串的列分隔符。
    • 有效值
      • string
      • regexp
    • 默认值:'::'
  • startRow

    • 描述:初始行的索引(索引从 0 开始)。
    • 有效值:integer
    • 默认值:0
  • totalRows

    • 描述:要返回的最大行数。
    • 有效值
      • integer
      • 'all' — 将返回所有行
    • 默认值:'all'
  • columns

    • 描述:要返回的列的索引(索引从 0 开始)。
    • 有效值
      • stringCommaSeparated
      • array
      • 'all' — 将返回所有列
    • 默认值:'all'
  • filter

    • 描述:列的过滤子句。
      • 因此,
         0 == 'a' ||
         0 =='b' &&
         1 == 'some' &&
         2 != ''
        
        返回满足以下条件的行
        • 0 列等于 'a'
        • 0 列等于 'b'
        • 1 列等于 some
        • 2 列不等于 ''
      • 引号中的值是可选的,这也是有效的
         0 == a ||
         0 == b &&
         1 == some &&
         2 != 
        
      • 双引号也支持单引号
         0 == "a" ||
         0 == "b" &&
         1 == "some" &&
         2 != ""
        
      • 空格、制表符和换行符是可选的,这也是有效的:0==a||0==b&&1==some&&2!=
    • 有效值:stringSeparated
    • 默认值:—
  • removeEmptyRows

    • 描述:是否需要删除空行?
    • 有效值
      • 0
      • 1
    • 默认值:1
  • removeEmptyCols

    • 描述:是否需要删除空列?
    • 有效值
      • 0
      • 1
    • 默认值:1
  • sortBy

    • 描述:要排序的列的索引(索引从 0 开始)。
      • 该参数也接受逗号分隔的值进行多级排序,例如 '0,1'
    • 有效值
      • stringCommaSeparated
      • array
    • 默认值:'0'
  • sortDir

    • 描述:行排序方向(不区分大小写)。
    • 有效值
      • 'ASC' — 将按升序返回行
      • 'DESC' — 将按降序返回行
      • 'RAND' — 将按随机顺序返回行
      • 'REVERSE' — 将按反转顺序返回行
      • '' — 将按设置的顺序返回行(不排序)
    • 默认值:''
  • typography

    • 描述:需要纠正值的列的逗号分隔索引(索引从 0 开始)。
      • 如果未设置,则不会有纠正。
    • 有效值
      • stringCommaSeparated
      • array
    • 默认值:—
  • outputFormat

    • 描述:结果输出格式(不区分大小写)。
    • 有效值
      • 'html'
      • 'json'
      • 'array'
      • 'htmlarray'
    • 默认值:'html'
  • rowGlue

    • 描述:在渲染时组合行的字符串。
      • 它可以与 rowTpl 一起使用。
    • 有效值:string
    • 默认值:''
  • colGlue

    • 描述:在渲染时组合列的字符串。
      • 它可以与colTplrowTpl一起使用。
    • 有效值:string
    • 默认值:''
  • rowTpl

    • 描述:行渲染的模板(outputFormat必须等于'html')。
      • 可用的占位符
        • [+rowNumber+] — 当前行的索引,从1开始
        • [+rowNumber.zeroBased+] — 当前行的索引,从0开始
        • [+rowKey+] — 当前行的键,在inputString中的对象或关联数组中很有用,对于索引数组,占位符等于[+rowNumber.zeroBased+]
        • [+total+] — 行总数
        • [+resultTotal+] — 返回的行总数
        • [+colcolumnNumber+] (例如[+col0+][+col1+]等) — 列值,当columnNumber是零基列号时
        • [+columnKey+] — 列值,当columnKey是原始列键时(请参见下面的示例)
        • [+columnKey.nestedProperty+][+colcolumnNumber.nestedProperty+] — 嵌套属性的值,如果列值是对象
        • [+allColumnValues+] — 通过colGlue组合的所有列值
        • [+allColumnValuesObjectJson+] — 所有列值作为一个JSON对象,其中键是原始列键,值是值
    • 有效值
      • stringChunkName
      • string — 使用以@CODE:开始的行内模板
    • 默认值:—
  • colTpl

    • 描述:列渲染的模板列表(outputFormat必须等于'html')。
      • 如果模板的数量少于列的数量,则最后一个传递的模板将用于渲染剩余的列。
    • 有效值
      • stringCommaSeparated
      • array
    • 默认值:—
  • colTpl[$i]

    • 描述:列渲染的模板。
      • 可用的占位符
        • [+val+] — 列的值
        • [+columnIndex+] — 列的索引,从0开始
        • [+columnKey+] — 列的键,在inputString中的对象或关联数组中很有用,对于索引数组,占位符等于[+columnIndex+]
        • [+rowNumber+] — 当前行的索引,从1开始
        • [+rowNumber.zeroBased+] — 当前行的索引,从0开始
        • [+rowKey+] — 当前行的键,在inputString中的对象或关联数组中很有用,对于索引数组,占位符等于[+rowNumber.zeroBased+]
        • [+total+] — 行总数
        • [+resultTotal+] — 返回的行总数
    • 有效值
      • stringChunkName
      • string — 使用以@CODE:开始的行内模板
      • 'null' — 指定不使用模板进行渲染
    • 默认值:'null'
  • outerTpl

    • 描述:包装模板(outputFormat必须不等于'array')。
      • 可用的占位符
        • [+result+] — 片段结果
        • [+total+] — 行总数
        • [+resultTotal+] — 返回的行总数
        • [+rowY.colX+] — 值(Y — 行号,X — 列号)
        • [+rowKey.colX+] — 值(Key — 行键,X — 列号)
    • 有效值
      • stringChunkName
      • string — 使用以@CODE:开始的行内模板
    • 默认值:—
  • 占位符

    • 描述:必须将附加数据传递给outerTplrowTplcolTpl模板。
      • 也支持嵌套对象和数组
        • {"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
    • 默认值:—
  • urlencode

    • 描述:是否需要URL编码结果?
      • outputFormat必须不等于'array'
      • URL编码根据RFC 3986使用。
    • 有效值
      • 0
      • 1
    • 默认值:0
  • totalRowsToPlaceholder

    • 描述:包含行总数的全局(MODX)Evolution占位符的名称。
      • 如果totalRowsToPlaceholder为空,则不会设置占位符。
    • 有效值:string
    • 默认值:—
  • resultToPlaceholder

    • 描述:包含片段结果的全球(MODX)Evolution占位符的名称。
      • 如果参数为空,则结果将以常规方式返回。
    • 有效值:string
    • 默认值:—

示例

输出带有描述的images

初始字符串(位于images TV)

assets/images/some_img1.jpg::Image 1||assets/images/some_img2.jpg::Image 2

在文档模板中调用片段

[[ddGetMultipleField?
	&inputString=`[*images*]`
	&rowTpl=`images_item`
]]

images_item片段代码

[+col1+]:
<img src="[+col0+]" alt="[+col1+]" />

返回

Image 1:
<img src="assets/images/some_img1.jpg" alt="Image 1" />
Image 2:
<img src="assets/images/some_img2.jpg" alt="Image 2" />

使用行模板中的原始列键从JSON输出图像

[[ddGetMultipleField?
	&inputString=`[
		{
			"src": "assets/images/some_img1.jpg",
			"alt": "Image 1"
		},
		{
			"src": "assets/images/some_img2.jpg",
			"alt": "Image 2"
		}
	]`
	&rowTpl=`@CODE:<img src="[+src+]" alt="[+alt+]" />`
]]

返回

<img src="assets/images/some_img1.jpg" alt="Image 1" />
<img src="assets/images/some_img2.jpg" alt="Image 2" />

使用[+allColumnValues+]占位符和rowTplcolGlue参数输出具有动态列数的行

让第一行包含2列,第二行3列,第三行1列

[[ddGetMultipleField?
	&inputString=`{
		"First prices": [
			"$100",
			"$120"
		],
		"Second prices": [
			"$300",
			"$320",
			"$350"
		],
		"Third prices": [
			"$50"
		]
	}`
	&outerTpl=`@CODE:<ul>[+result+]</ul>`
	&rowTpl=`@CODE:<li>[+rowKey+]: [+allColumnValues+]</li>`
	&colGlue=`, `
]]

返回

<ul>
	<li>First prices: $100, $120</li>
	<li>Second prices: $300, $320, $350</li>
	<li>Third prices: $50</li>
</ul>

如果数据不为空,则以表格格式获取并输出ID为25的文档的prices TV数据

初始字段值

Tasty apples::100::kg||Usual nails::5 000::centner||Cats::865::pieces

片段调用( wherever)

[[ddGetMultipleField?
	&inputString_docField=`prices`
	&inputString_docId=`25`
	&outerTpl=`prices`
	&rowTpl=`prices_item`
]]

prices_item片段代码

<tr>
	<td>[+rowNumber+]</td>
	<td>[+col0+]</td>
	<td>[+col1+]/[+col2+]</td>
</tr>

prices片段代码

<h1>Price table</h1>
<table>
	[+result+]
</table>

返回

<h1>Price table</h1>
<table>
	<tr>
		<td>1</td>
		<td>Tasty apples</td>
		<td>100/kg</td>
	</tr>
	<tr>
		<td>2</td>
		<td>Usual nails</td>
		<td>5 000/centner</td>
	</tr>
	<tr>
		<td>3</td>
		<td>Cats</td>
		<td>865/pieces</td>
	</tr>
</table>

使用正则表达式在inputString_rowDelimiter中按逗号分隔返回文档标签

(MODX)EvolutionCMS.plugins.ManagerManager.mm_widget_tags应用于存储在tags TV中的标签,用户填写以逗号分隔的标签,而字段可以填充空格或没有空格。

tags TV值

Cats, Dogs,Whales , Bears , Elephants

片段调用

[[ddGetMultipleField?
	&inputString=`[*tags*]`
	&inputString_rowDelimiter=`/\s*,\s*/`
	&rowGlue=`, `
	&rowTpl=`tags_item`
]]

tags_item片段内容

<a href="[~16~]?tags=[+col0+]">[+col0+]</a>

返回

<a href="[~16~]?tags=Cats">Cats</a>,
<a href="[~16~]?tags=Dogs">Dogs</a>,
<a href="[~16~]?tags=Whales">Whales</a>,
<a href="[~16~]?tags=Bears">Bears</a>,
<a href="[~16~]?tags=Elephants">Elephants</a>

通过placeholders将附加数据传递到模板中

[[ddGetMultipleField?
	&inputString=`Grey::8 kg::loves meat||Red::6 kg::vegetarian`
	&outerTpl=`cats`
	&rowTpl=`cats_item`
	&colTpl=`cats_item_color,null,null`
	&placeholders=`{
		"kind": "cats",
		"price": "not for sale",
		"colorTitle": "He has a nice thick coat."
	}`
]]

cats片段代码([+kind+]将被替换为cats

<h1>Our [+resultTotal+] favorite [+kind+].</h1>
<div>
	[+result+]
</div>

cats_item片段代码([+price+]将被替换为not for sale

<p>[+rowNumber+]. [+col0+], [+col1+], [+col2+] — <i>[+price+]</i>.</p>

cats_item_color片段代码([+colorTitle+]将被替换为He has a nice thick coat.

<span title="[+colorTitle+]">[+val+]</span>

返回

<h1>Our 2 favorite cats.</h1>
<div>
	<p>1. <span title="He has a nice thick coat.">Grey</span>, 8 kg, loves meat — <i>not for sale</i>.</p>
	<p>2. <span title="He has a nice thick coat.">Red</span>, 6 kg, vegetarian — <i>not for sale</i>.</p>
</div>

按列值筛选(filter参数)

[[ddGetMultipleField?
	&inputString=`[
		[
			"John Bon Jovi",
			"musician",
			"Bon Jovi"
		],
		[
			"Steve Jobs",
			"businessman",
			"Apple"
		],
		[
			"Roger Waters",
			"musician",
			"Pink Floyd"
		],
		[
			"Robbie Williams",
			"musician",
			""
		],
		[
			"Hugh Laurie",
			"actor",
			""
		]
	]`
	&filter=`
		1 == 'musician' &&
		2 != '' ||
		0 == 'Hugh Laurie'
	`
	&outputFormat=`json`
]]

返回

[
	[
		"John Bon Jovi",
		"musician",
		"Bon Jovi"
	],
	[
		"Roger Waters",
		"musician",
		"Pink Floyd"
	],
	[
		"Hugh Laurie",
		"actor",
		""
	]
]

按多个列排序JSON对象(参数 → sortBysortDir

[[ddGetMultipleField?
	&inputString=`{
		"Albert Einstein": {
			"number": "18",
			"born": "1879.03.14",
			"died": "1955.04.18"
		},
		"Alan Turing": {
			"number": "42",
			"born": "1912.06.23",
			"died": "1954.06.07"
		},
		"Nikola Tesla": {
			"number": "7",
			"born": "1856.07.10",
			"died": "1943.01.07"
		},
		"Marie Curie": {
			"number": "42",
			"born": "1867.11.07",
			"died": "1934.07.04"
		},
		"Dmitri Mendeleev": {
			"number": "7",
			"born": "1834.02.08",
			"died": "1907.02.02"
		}
	}`
	&sortDir=`ASC`
	&sortBy=`number,born`
	&outputFormat=`json`
]]

返回

{
	"Dmitri Mendeleev": {
		"number": "7",
		"born": "1834.02.08",
		"died": "1907.02.02"
	},
	"Nikola Tesla": {
		"number": "7",
		"born": "1856.07.10",
		"died": "1943.01.07"
	},
	"Albert Einstein": {
		"number": "18",
		"born": "1879.03.14",
		"died": "1955.04.18"
	},
	"Marie Curie": {
		"number": "42",
		"born": "1867.11.07",
		"died": "1934.07.04"
	},
	"Alan Turing": {
		"number": "42",
		"born": "1912.06.23",
		"died": "1954.06.07"
	}
}

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

// Include (MODX)EvolutionCMS.libraries.ddTools
require_once(
	$modx->getConfig('base_path') .
	'assets/libs/ddTools/modx.ddtools.class.php'
);

// Run (MODX)EvolutionCMS.snippets.ddGetMultipleField
\DDTools\Snippet::runSnippet([
	'name' => 'ddGetMultipleField',
	'params' => [
		'inputString' => '[
			[
				"assets/images/example1.png",
				"Example image 1"
			],
			[
				"assets/images/example2.png",
				"Example image 2"
			]
		]',
		'rowTpl' => '@CODE:<img src="[+col0+]" alt="[+col1+]" />',
	],
]);

很难在这里列出所有可能的示例,所以如果您在这里不理解某些内容,请向我们提问。

链接