guimauve/craft-twig-view-composers 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

guimauve/craft-twig-view-composers

Composer 安装命令:

composer require guimauve/craft-twig-view-composers

包简介

Allow composable PHP classes to be called before page view rendering, hence allowing organizing code à-la Laravel View Composers.

README 文档

README

Allow composable PHP classes to be called before page view rendering, hence allowing organizing code à-la Laravel View Composers.

Configuration

Install the plugin

composer require guimauve/craft-twig-view-composer
php craft plugin/install _twig-view-composers

Then, add the following path to your composer autoloader in composer.json. You can name the second part, which is the directory that psr-4 is going to look into, as you wish.

"autoload": {
"psr-4": {
  "guimauve\\composers\\": "composers/"
}
},

In that folder, you can now create a simple PHP class, named according to your twig template, with a "compose" method and you will receive the TemplateEvent as single argument. You can now add anything you want to the template data and thus isolate functionality, such as database calls, outside of the templates.

<?php
/**
 * Basic view composer for the templates/index.twig file.
 * To create a view composer for a subfolder, simply replicate the structure here.
 * Ex.: templates/blog/_entry.twig -> composers/blog/Entry.php
 *
 * Element queries are made with PHP to
 * avoid using their twig counterparts
 * @see https://craftcms.com/docs/4.x/element-queries.html
 */

namespace guimauve\composers;

use craft\events\TemplateEvent;
use craft\elements\Entry;
use Craft;

class Index
{

    /**
     * @param      TemplateEvent $event Object passed by View::EVENT_BEFORE_RENDER_TEMPLATE
     * @see        https://github.com/craftcms/cms/blob/a1b232ea1888f131bb7626a5bdaff0f5fa2f4469/src/web/View.php#L1891
     * @see        https://github.com/craftcms/cms/blob/2eac9249964ccc553bf841c79b9ee44d58f16b61/src/events/TemplateEvent.php
     *
     * @return     void
     */
    public static function compose(TemplateEvent $event)
    {
         $locations = Entry::find()->section('shop')->relatedTo($selectedCity)->with([
            [
                'featuredImage', ['withTransforms' => ['x870']]
            ]
        ])->collect();
        $event->variables['locations'] = $locations;
    }
}

If you use a block based system and want to call the same code on multiple template or pages, there's sadly no way to bind one view composer to multiple templates (yet), but you can use a PHP Trait to make most of your code re-usable.

namespace guimauve\composers\traits;

use Craft;
use craft\helpers\App;

trait blockableTrait {
    public static function getBlocksContent() {
        /**
         * Do your thing
         */
    }
}

And in your view composer

namespace guimauve\composers;

use craft\events\TemplateEvent;
use craft\elements\Entry;
use Craft;

class Index
{
    use \guimauve\composers\traits\blockableTrait;

    /**
     * @param      TemplateEvent $event Object passed by View::EVENT_BEFORE_RENDER_TEMPLATE
     * @see        https://github.com/craftcms/cms/blob/a1b232ea1888f131bb7626a5bdaff0f5fa2f4469/src/web/View.php#L1891
     * @see        https://github.com/craftcms/cms/blob/2eac9249964ccc553bf841c79b9ee44d58f16b61/src/events/TemplateEvent.php
     *
     * @return     void
     */
    public static function compose(TemplateEvent $event)
    {
        $event->variables['blocks'] = self::getBlocksContent();
    }
}

Requirements

This plugin requires Craft CMS 4.4.0 or later, and PHP 8.0.2 or later.

guimauve/craft-twig-view-composers 适用场景与选型建议

guimauve/craft-twig-view-composers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 06 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 guimauve/craft-twig-view-composers 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 guimauve/craft-twig-view-composers 我们能提供哪些服务?
定制开发 / 二次开发

基于 guimauve/craft-twig-view-composers 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 26
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2023-06-01