承接 tigr/twig-preprocessor 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

tigr/twig-preprocessor

Composer 安装命令:

composer require tigr/twig-preprocessor

包简介

This very simple loader adds a way to pre parse twig files before loading them into twig parser

README 文档

README

Build Status For Twig version Minimum PHP Version Packagist GitHub tag (latest SemVer) GitHub

This Twig Preprocessor loader allows you to do custom manipulations with twig templates before passing them to twig parser. This allows you, for instance, to do some substitutions, or format templates to make them look better.

This branch (master) contains code for Twig 2.x.

Installation

Installation via composer (version for twig 2):

composer require tigr/twig-preprocessor

If you want to use it with twig 1, use version 1:

composer require tigr/twig-preprocessor "~1.0"

Usage

In general usage, you pass real template loader to Twig Preprocessor Loader and also a callback that would be called to mingle with twig template code. So, general usage is this:

$realLoader = Twig_Loader_Filesystem('/path/to/templates');
$loader = Twig_Loader_Preprocessor(
    $realLoader, 
    function($twigSource) {
        // do something with $twigSource
        
        return $twigSource;
    }
);

$twig = new Twig_Environment($loader);

The main reason why this code was written was to make Twig output a bit more pretty-formatted code. You can read more about this in this Twig issue.

In short, the idea is to remove all spaces/tabs before any (well, most) twig control structures. Here is how you can achieve this:

$loader = new Twig_Loader_Preprocessor(
    $realLoader,
    function ($template) {
        static $regExp;
        // the RE isn't perfect, it won't match structures having curly braces within, 
        // but it's okay for me.
        if (!isset($regExp)) {
            $regExp = str_replace(
                ['_',      '{',  '}'],
                ['[\t ]*', '\{', '\}'],
                '/^_({([#%])[^}]*[^-](?2)}|{%_block_\w*_%}{%_endblock_%})$/m'
            );
        }
        
        return preg_replace($regExp, '$1', $template);
    }
);

History

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-01-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固