定制 liip/frctl-twig 二次开发

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

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

liip/frctl-twig

Composer 安装命令:

composer require liip/frctl-twig

包简介

Alias-Loader for handling fractal @handle's in twig and callable handler for the accompanied node module

README 文档

README

frctl-twig is an adapter consisting of an NPM and a Composer package. It integrates the Twig PHP template engine into fractal.

Installation

Inside your fractal project add a composer package by adding a composer.json:

{
  "name": "my/fractal-project",
  "type": "project",
  "require-dev": {
    "liip/frctl-twig": "dev-master"
  },
}

Run composer install.

Add a devDependencies to the fractal twig adapter into your package.json:

"frctl-twig": "git+https://github.com/liip/frctl-twig.git#master"

Run npm install.

Adding Twig Extensions

Add any relevant composer packages to your composer.json.

For exampe run composer require twig/twig-extensions.

Then add a file php-twig/TwigExtensions.php to your fractal project with the following content:

<?php

namespace Frctl;

class TwigExtensions
{
    static public function getExtensions()
    {
        return [
            # Add your extensions here, for example the twig-extension text extension
            # new \Twig_Extensions_Extension_Text(),
        ];
    }
}

Then add the following section to your fractal project composer.json:

  "autoload": {
    "psr-4": {
      "Frctl\\": "php-twig/"
    }
  }

How to use Twig templates in another project

Add the composer package pointing to your fractal project into the composer project of this other project.

Adjust the file loader to be able to find the twig templates in the fractal project:

class TwigFilesystemLoader extends BaseTwigFilesystemLoader
{
    /**
     * Should probably be set via a setter from configuration
     *
     * @var string
     */
    private $fractalPath = '/path/to/fractal/twig/templates';

    /**
     * @param string $name
     *
     * @return string
     */
    protected function findTemplate($name)
    {
        $fractalPath = $this->getFractalPath();
        if ($fractalPath && preg_match('/^@fractal-(.*)$/', $name, $templatePath)) {
            $fullFilePath = $fractalPath . '/' . $templatePath[1];

            return $fullFilePath;
        }

        ...
    }
}

Load all the extensions into your Twig_Environment instance:

    if (class_exists('Frctl\TwigExtensions')) {
        $extensions = \Frctl\TwigExtensions::getExtensions();
        foreach ($extensions as $extension) {
            $twig->addExtension($extension);
        }
    }

strict_variables and debug config flags

By default, the strict_variables flag of twig is set to false and the debug flag is set to true. To change these variables, pass them in an optional config object with their desired values while configuring fractal:

    const frctlTwig = require("frctl-twig");
    
    fractal.components.engine(frctlTwig({
        strict_variables: true, // Or false
        debug: false // Or true
    }));
    
    // Further setup...

Credits

The code is based on the work by Benjamin Milde:

liip/frctl-twig 适用场景与选型建议

liip/frctl-twig 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.02k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2018 年 03 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 liip/frctl-twig 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 8
  • Watchers: 4
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-05