定制 alanrodas/twig-view 二次开发

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

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

alanrodas/twig-view

最新稳定版本:1.1

Composer 安装命令:

composer require alanrodas/twig-view

包简介

Twig powered View for CakePHP 2.x

README 文档

README

Build Status Latest Stable Version Total Downloads Coverage Status Bitdeli Badge

This plugin for the CakePHP Framework allows you to use the Twig Templating Language for your views.

In addition to enabling the use of most of Twig's features, the plugin is tightly integrated with the CakePHP view renderer giving you full access to helpers, objects and elements.

Installation

Make sure you have composer installed and configured with the autoloader registering during bootstrap as described here. Make sure you have a composer.json and add the following to your required section.

"wyrihaximus/twig-view": "dev-master"

Cache Permissions

Make the default view-cache folder writeable.

APP/Plugin/TwigView/tmp/views

Alternatively: Set where you want cache files to be stored.

define('TWIG_VIEW_CACHE', APP . 'tmp');

Using the View Class

To make CakePHP aware of TwigView edit your APP/Controller/AppController.php file and add the following:

class AppController extends Controller  {
	public $viewClass = 'TwigView.Twig';
}

Be sure to load the TwigView plugin in your bootstrap.php file with:

CakePlugin::load('TwigView', array('bootstrap' => true));

or:

CakePlugin::loadAll();

Now start creating view files using the .tpl extension.

Default Layouts

This plugin comes with all default layouts converted to Twig. Examples can be found in:

APP/Plugin/TwigView/examples

Themes

The plugin has support for themes and works just like the Theme view. Simply add the $theme property to your controller and you're set.

class AppController extends Controller  {
	public $viewClass = 'TwigView.Twig';
	public $theme = 'Rockstar';
}

This will cause the view to also look in the Themed folder for templates. In the above example templates in the following directory are favored over their non-themed version.

APP/View/Themed/Rockstar/

If you, for example, want to overwrite the Layouts/default.tpl file in the Rockstar theme, then create this file:

APP/View/Themed/Rockstar/Layouts/default.tpl

Using Helpers inside Templates

All helper objects are available inside a view and can be used like any other variable inside Twig.

{{ time.nice(user.created) }}

... where ...

{{ time.nice(user.created) }}
    ^    ^    ^    ^____key
    |    |    |____array (from $this->set() or loop)
    |    |_____ method
    |______ helper

Which is the equivalent of writing:

<?php echo $this->Time->nice($user['created']); ?>

A more complex example, FormHelper inputs:

{{
  form.input('message', {
    'label': 'Your message',
    'error': {
      'notempty': 'Please enter a message'
    }
  })
}}

Referencing View Elements

Elements must be .tpl files and are parsed as Twig templates. Using .ctp is not possible.

In exchange for this limitation you can import elements as easy as this:

{{ _view.element('Plugin.element') }}

Translating Strings

The trans filter can be used on any string and simply takes the preceding string and passes it through the __() function.

{{
  form.input('email', {
    'label': 'Your E-Mail Address'| trans
  })
}}

This is the equivalent of writing:

<?php echo $this->Form->input('email', array(
   'label' => __("Your E-Mail Address")
)); ?>

Translating multiple lines

The trans-block element will help you with that. This is especially useful when writing email templates using Twig.

{% trans %}
Hello!

This is my mail body and i can translate it in X languages now.
We love it!
{% endtrans %}

Accessing View Instance

In some cases it is useful to access $this, for example to build a DOM id from the current controller and action name.

The object is accessible through _view.

<div class="default" id="{{ _view.name|lower ~ '_' ~ _view.action|lower }}">

Precompiling all templates

Twig has to compile all the templates before they can be used. This adds a one time per template delay to the loading of a page. This can be countered by using the Compile Templates shell command. This commands scans for all the templates and compiles them with Twig for caching and performence gains.

./cake TwigView.compile_templates all

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-07-10