stillat/statamic-template-resolver
Composer 安装命令:
composer require stillat/statamic-template-resolver
包简介
README 文档
README
Template Resolver for Statamic is a simple utility addon, intended to be used by other addons. It provides a simple utility for selecting and rendering a template based on an entry's blueprint and collection, with support for a fallback default template.
Example use-cases:
- Selecting templates dynamically to generate social media images from HTML,
- Generating HTML documents on-the-fly, without requiring network requests to the site,
- Anything where you need to support customizable templates based on an entry's blueprint/collection details
How to Install
Run the following command from your project root:
composer require stillat/statamic-template-resolver
How to Use
You need to create an instance of StringTemplateManager, and supply the directory to search for templates in.
<?php use Stillat\StatamicTemplateResolver\StringTemplateManager; $manager = new StringTemplateManager( resource_path('views/social_media_images') );
Once you have a StringTemplateManager instance, you can check if a template exists for a given collection/blueprint combination:
<?php // ... if ($manager->hasTemplate($collection, $blueprint)) { // The template exists. }
The hasTemplate method will return true if a specific template or the default template exists. To create a default template, create a file named _default.antlers.html or _default.blade.php at the root of the template folder.
In our example, the default template would need to be placed here:
views/social_media_images/_default.antlers.html
Specific collection/blueprint templates are stored within a nested directory structure using the following format:
<template_directory><collection_handle>/<blueprint_handle>.<extension>
For example, if we had a blog collection, with a post blueprint, we could create a specific template at the following location:
views/social_media_images/blog/post.antlers.html
This library supports the following extensions:
.antlers.html: Renders the template using Statamic's Antlers templating engine.blade.php: Renders the template using Laravel's Blade templating engine
To render a template with data, we may use the render method:
<?php // ... $results = $manager->render( 'colllection_handle', 'blueprint_handle', $data );
The render method will return null if a template could not be found; $data is provided as an array, and is required.
We may also optionally modify the template before rendering it by supplying an optional callable as the fourth argument:
<?php // Modify the template before its rendered. $results = $manager->render( 'collection_handle', 'blueprint_handle', $data, function ($template, $data) { return mb_strtoupper($template); } );
The modifying callable will receive the unmodified template contents as its first argument, and the original data array as the second.
License
Statamic Template Resolver is free software, released under the MIT license.
stillat/statamic-template-resolver 适用场景与选型建议
stillat/statamic-template-resolver 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.87k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 11 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 stillat/statamic-template-resolver 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 stillat/statamic-template-resolver 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5.87k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-11-04