marionnewlevant / agnostic-fetch
统一语法,用于获取元素,无论它们是否已预加载
2.0.4
2019-02-04 20:20 UTC
Requires
- craftcms/cms: ^3.0.0-RC1
README
Craft 插件,提供统一语法以获取元素,无论它们是否已预加载
安装
要安装 Agnostic Fetch,请按照以下步骤操作
- 下载并解压缩文件,将解压缩文件夹重命名为
agnosticfetch
并将其放置在您的craft/plugins
文件夹中 - -OR- 直接在您的
craft/plugins
文件夹中执行git clone https://github.com/marionnewlevant/craft3-agnositic_fetch.git
,并将其重命名为agnosticfetch
。您可以使用git pull
更新它 - -OR- 通过 Composer 在项目文件夹中使用
composer require marionnewlevant/agnostic-fetch
进行安装 - 在 Craft 控制面板的设置 > 插件下安装插件
- 插件文件夹应命名为
agnosticfetch
,以便 Craft 能够识别。
Agnostic Fetch 在 Craft 3.x 上运行。
Agnostic Fetch 概述
当元素已被预加载时,从 Craft Element Query 获取元素的代码不同。例如,要获取非预加载字段的第一个元素,请使用 someField.one()
,但对于预加载字段,请使用 someField[0]
(这甚至还没有涉及到错误检查)。使用 Agnostic Fetch,您可以使用 someField | one
(或 one(someField)
或 craft.agnosticfetch.one(someField)
)。
Agnostic Fetch 函数可作为 twig 过滤器、twig 函数和 craft 变量使用。
使用 Agnostic Fetch
all
使用以下任何一种
- someField | all
- all(someField)
- craft.agnosticfetch.all(someField)
one
使用以下任何一种
- someField | one
- one(someField)
- craft.agnosticfetch.one(someField)
nth
使用以下任何一种
- someField | nth(index)
- nth(someField, index)
- craft.agnosticfetch.nth(someField, index)
(index 从 0
开始计数)
ids
使用以下任何一种
- someField | ids
- ids(someField)
- craft.agnosticfetch.ids(someField)
count
使用以下任何一种
- someField | count
- count(someField)
- craft.agnosticfetch.count(someField)
isEager
除了获取函数之外,isEager
还会告诉您元素是否已预加载。
使用以下任何一种
- someField | isEager
- isEager(someField)
- craft.agnosticfetch.isEager(someField)
由 Marion Newlevant 提供