定制 jascha030/wp-sequoia 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

jascha030/wp-sequoia

Composer 安装命令:

composer require jascha030/wp-sequoia

包简介

Static HTML Component library using OOP Php in conjunction with the Twig templating language

README 文档

README

A Cheeky Component library for PHP, using the Twig templating language library and an OOP approach. Built with Wordpress in mind but not limited to Wordpress.

Prerequisites

  • Php ^7.4 || ^8.0
  • Composer ^2.0

Installation

Require as dependency by either; adding it directly to your composer.json, or running:

composer require jascha030/wp-sequoia

Simple as that…

Usage

It’s all based around two interfaces:

TwigTemplaterInterface and the TwigComponentInterface and it’s main implementation TwigComponentAbstract.

Templater

Create a Templater which requires a Twig\Environment instance. (Preferably using a FilesystemLoader).

Example

<?php

use Jascha030\Sequoia\Templater\TwigTemplater;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

// Replace with the path to your templates folder.
$loader      = new FilesystemLoader('/Twig/Template/Folder');
$environment = new Environment($loader);
$templater = new TwigTemplater($environment);

Let’s say you have a template in your templates folder called list-template.twig

<ul>
    {% for item in items %}
        <li>{{ item.text }}</li>     
    {% endfor %}
</ul>

Now you can extend the TwigComponentAbstract.

<?php 

namespace Example\Component;

use Jascha030\Sequoia\Component\TwigComponentAbstract;

final class ListComponent extends TwigComponentAbstract 
{
    /**
     * @inheritDoc
     */
    public function getTemplate(): string
    {
        return 'list-template.twig';
    }
}

Which can then be rendered like:

<?php

use Example\Component\ListComponent;
use Jascha030\Sequoia\Templater\TwigTemplater;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

// Replace with the path to your templates folder.
$loader      = new FilesystemLoader('/Twig/Template/Folder');
$environment = new Environment($loader);
$templater = new TwigTemplater($environment);

ListComponent::render(
    $templater, 
    [
        'items' => [
            ['text' => 'List item 1'],
            ['text' => 'List item 2']
        ]
    ]
);

Using default values

You can also provide, defaults for a component using the HasDefaultContextTrait like:

final class ListComponent extends TwigComponentAbstract 
{
    use HasDefaultContextTrait;
    
    /**
     * @inheritDoc
     */
    public function getTemplate(): string
    {
        return 'list-template.twig';
    }
    
    /**
     * @inheritDoc
     */
    final public function getDefaults(): array
    {
        return [
            'items' => [
                ['text' => 'List item 1'],
                ['text' => 'List item 2']
            ]
        ];
    }
}

With Wordpress

When available the getContext method used in rendering, will have an automatically generated filter, to mutate the defaults following the pattern:

twig_template_context_{$this->getTemplateSlug()}

Where $this->getTemplateSlug() will by default return the template name defined in the getTemplate() method, minus the .twig appendix, in the above example this would be: twig_template_context_list-template, which could then be used in the following context (in Wordpress):

function edit_list_items(array $context): array
{
    $context['items'][] = ['text' => 'List item 3'];
    
    return $context;
}

\add_filter('twig_template_context_list-template', 'edit_list_items');

Now you would hypothetically have added a third list item and the output of the ListComponent::render() method would be:

<ul>
    <li>List item 1</li>
    <li>List item 2</li>
    <li>List item 3</li>
</ul>

Running UnitTests

To check wether your implementation is allright or if there is something wrong with the library while extending it. you can use:

composer run phpunit

Make sure to install with dev dependencies by using composer install instead of composer install —no-dev.

More comming soon.

Inspiration

This is the start of a library based around the frustration with Wordpress, their messy template system, their messy user base, and the sadly, lately rarely updated timber/timber library and it’s overkill set of features.

It’s pretty specific and built mainly for myself and to make Front-end features a bit more fun to develop.

But maybe someone will ever find some use in this library as I hope to extend it and add loads of features, until it will reach a point of being just as overkill as timber.

That being said, timber/timber is a great library so support it if you use it, and even though Wordpress’ messy and old-timey nature, It’s a great tool for those who are learning or don’t aspire to be developers to build their website, which is obviously, why it has seen the great success, that it has.

And for those who are aspiring to be developers, beware of the fact that the coding styles of Wordpress are a great starting point, they are not how we develop code in the rest of the world, and based around old, long forgotten standards.

Shout out to those few internet heroes, who are finding and teaching how to do Wordpress development, in a more modern fashion.

If you are one who aspires to be one or is just plain bored by having to work with Wordpress, I suggest googling people like:

  • Carl Alexander
  • Josh Pollock
  • The people from Roots (from bedrock, sage)

jascha030/wp-sequoia 适用场景与选型建议

jascha030/wp-sequoia 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 06 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 jascha030/wp-sequoia 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-06-25