jalendport/craft-preparse
Composer 安装命令:
composer require jalendport/craft-preparse
包简介
A fieldtype that parses Twig when an element is saved and saves the result as plain text.
README 文档
README
A fieldtype that parses Twig when an element is saved and saves the result as plain text.
Requirements
This plugin requires Craft CMS 5.0.0 or later and PHP 8.2.0 or later.
Installation
To install the plugin, follow these instructions.
-
Open your terminal and go to your Craft project:
cd /path/to/project -
Then tell Composer to load the plugin:
composer require jalendport/craft-preparse -
In the Control Panel, go to Settings → Plugins and click the “Install” button for Preparse Field.
Usage
When creating a new Preparse field, you add the Twig that you want run to the field's settings. When an element with that Preparse field is saved, the code will be parsed and the resulting value saved as plain text.
It's worth noting that the Preparse field is only updated when the element the field is on is saved. If you grab data from a related element (like in the category title example below), and then update the related element, the preparsed value will not automatically be updated.
In the Twig, the element that the Preparse field is added to is available as a variable named element. It's best to use this variable (as opposed to something like entry or asset) because it's possible you add the same Preparse field to multiple element types. This also means that when a Preparse field is added to a Matrix, SuperTable, or Neo block, that block will be what is available as element, so if you want to access the element that the Matrix/SuperTable/Neo field belongs to, you will want to use element.owner.
Examples
If you have a category field on your element named relatedCategory, you can save the category title to the Preparse field by adding the following Twig to the field settings:
{{ element.relatedCategory.one().title ?? '' }}
This is useful for saving preparsed values to a field for use with sorting, searching, or similar things.
You can also do more advanced stuff, for instance performance optimizing. Let's say you have three different asset fields that may or may not be populated. Having to check these in the template may require a bunch of queries since you can't check if a field has a relation in Craft without actually querying for it. You could do something like this to get the id of the asset to use:
{% if element.smallListImage | length %}
{{ element.smallListImage.one().id }}
{% elseif element.largeListImage | length %}
{{ element.largeListImage.one().id }}
{% elseif element.mainImage | length %}
{{ element.mainImage.one().id }}
{% endif %}
You'd probably want to wrap that in {% apply spaceless %} ... {% endapply %} to make it more useful...
Or you could just use it to do some bulk work when saving, like pre-generating a bunch of image transforms with Imager X:
{% if element.mainImage | length %}
{% set transformedImages = craft.imager.transformImage(element.mainImage.one(), [
{ width: 1000 },
{ width: 900 },
{ width: 800 },
{ width: 700 },
{ width: 600 },
{ width: 500 },
{ width: 400 },
{ width: 300 },
{ width: 200 },
{ width: 100 }
]) %}
{% endif %}
Preparse also has access to your site's template root, so you can even include local templates if you want to do more advanced stuff and/or want to keep your field's Twig in version control:
{% include '_partials/customPreparseFieldStuff' %}
Make sure that you always write solid Twig, taking into account that fields may not be populated yet. If an error occurs in your Twig, the element will not be saved. Code defensively!
Price, License, and Support
The plugin is released under the MIT license, meaning you can do whatever you want with it as long as you don't blame us. It's free, which means there is absolutely no support included, but you might get it anyway. Just post an issue here on GitHub if you have one, and we'll see what we can do. :)
Changelog
See the changelog file.
jalendport/craft-preparse 适用场景与选型建议
jalendport/craft-preparse 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.63k 次下载、GitHub Stars 达 108, 最近一次更新时间为 2024 年 07 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「twig」 「cms」 「fieldtype」 「Craft」 「craftcms」 「craft-plugin」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jalendport/craft-preparse 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jalendport/craft-preparse 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jalendport/craft-preparse 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
A Twig extension to insert css as inline styles with a tag
One of its kind; unlike anything else
Caching and compression for Twig assets (JavaScript and CSS).
GraphQL authentication for your headless Craft CMS applications.
统计信息
- 总下载量: 7.63k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 108
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-12