wanze/template-engine-factory 问题修复 & 功能扩展

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

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

wanze/template-engine-factory

Composer 安装命令:

composer require wanze/template-engine-factory

包简介

Provides ProcessWire integration for various template engines such as Twig.

README 文档

README

StyleCI License: MIT ProcessWire 3

A ProcessWire module integrating template engines such as Twig. It allows to render pages or individual templates via template engine and encourages to separate logic from markup by implementing a simple MVC pattern.

  • For a quick introduction, please read the Getting Started section of this readme.
  • More information is available in the official Documentation.

Version 2.x of this module differs from the 1.x version in many ways. Modules providing template engines must be installed with Composer. Please take a look at the update guide, as the new version introduces backwards compatibility breaks. The 1.x version of the module is available on the 1.x branch.

Requirements

  • ProcessWire 3.0 or newer
  • PHP 7.0 or newer
  • Composer

Installation

Execute the following command in the root directory of your ProcessWire installation:

composer require wanze/template-engine-factory:^2.0

This installs the module and its bundled template engine called TemplateEngineProcessWire. This template engine uses ProcessWire's internal TemplateFile class for rendering. Other template engines are added to the factory by installing separate ProcessWire modules.

Installing Twig and other template engines

Each template engine is a separate ProcessWire module. For example, if you want to use Twig, execute the following command:

composer require wanze/template-engine-twig:^2.0

This will install the TemplateEngineTwig module and all Twig dependencies.

ℹ️ This module includes test dependencies. If you are installing it on production with composer install, make sure to pass the --no-dev flag to omit autoloading any unnecessary test dependencies!.

Configuration

The TemplateEngineFactory offers the following configuration options:

  • Template Engine The template engine used to render pages and templates. Any installed engine is listed here.
  • Path to templates Relative path from the site directory where template files are stored. E.g. templates/views/ resolves to /site/templates/views/.
  • Enable automatic page rendering Check to delegate the rendering of pages to the template engine. You may enable or disable this behaviour for specific templates.
  • API variable to interact with the template engine Enter a name for the API variable used to pass data from the ProcessWire template (Controller) to the template engine.
  • Enabled templates Restrict automatic page rendering to the templates selected here.
  • Disabled templates Select templates of pages that should not automatically be rendered via template engine. Do not use in combination with the Enabled templates configuration, either enable or disable templates.

More configuration options might be available in the module providing a template engine, e.g. the module TemplateEngineTwig offers several configuration related to Twig.

Available template engines

Getting Started

This section assumes that Twig is used as active template engine, but the usage is exactly the same for any other chosen template engine.

Using the template engine to render templates

Assume the following Twig template exists in /site/templates/views/foo.html.twig

<h1>{{ title }}</h1>
{% if body %}
    <p>{{ body }}</p>
{% endif %}

The template can be rendered anywhere with the Template Engine Factory module:

$factory = wire('modules')->get('TemplateEngineFactory');

// Render foo.html.twig with some data.
$factory->render('foo', ['title' => 'Foo', 'body' => 'Hello World']);

Automatic Page Rendering

If enabled, this feature uses the template engine to render ProcessWire pages when calling Page::render. By default, the module tries to find a Twig template matching the same name as the ProcessWire template:

  • /site/templates/views/home.html.twig corresponds to /site/templates/home.php
  • /site/templates/views/about.html.twig corresponds to /site/templates/about.php

ProcessWire templates have access to a $view API variable which can be used to pass data to the template engine. As the template engine is now responsible to output markup, ProcessWire templates can be seen as Controllers. They process the request and pass data to the View layer via the $view API variable.

Examples

Consider the following ProcessWire template in /site/templates/home.php

// Form has been submitted, do some processing, send mail, save data... 
if ($input->post->form) {
  // ...
  $session->redirect('./');
}

// Forward some data to twig
$view->set('nav_items', $pages->get('/')->children());

The corresponding Twig template in /site/templates/views/home.html.twig might look like this:

<h1>{{ page.title }}</h1>

<ul class="nav">
{% for item in nav_items %}
    <li><a href="{{ item.url }}">{{ item.title }}</a></li>
{% endfor %}
</ul>

<form name="form">
    <input type="text" name="email">
    <input type="submit" value="Submit">
</form>

Note that the ProcessWire template does not echo out any markup. It just contains business logic and uses the $view API variable to pass data to the Twig template. That's it! The most simple MVC pattern available in ProcessWire. 😎

wanze/template-engine-factory 适用场景与选型建议

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

它主要适用于以下技术方向: 「template-engine」 「processwire」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 wanze/template-engine-factory 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 12.37k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 27
  • 点击次数: 13
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 26
  • Watchers: 6
  • Forks: 10
  • 开发语言: PHP

其他信息

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