LoginSignup
5
5

More than 5 years have passed since last update.

CakePHP 3.0(beta)のStringTemplateTraitを試してみた

Last updated at Posted at 2014-09-11

StringTemplateTrait

前提条件

  • CakePHP 3.0(beta1)を利用(2014/09/11)

Cake3ではHelper等がレンダリングするHtmlをStringTemplateTraitという機能を使うことで実現しています。

この機能をうまく使うことで出力されるHtmlを自由に修正することが出来ます。

※例) ページャーやformののHtml等

テンプレートの書き換え自体はStringTemplate,InstanceConfigTraitが行います。

使い方1

 テンプレートの書き換えはconfigメソッドで行います。

 以下の場合はSetFlashのテンプレートを書き換えます。

<?php $this->Session->config('templates.flash', '<div>真テンプレート</div>'); ?>
<?= $this->Session->flash(); ?>

使い方2

 中カッコ({})を2個重ねることで変数が展開されます。
 以下の場合は$messageが展開されます。

<?php $this->Session->config('templates.flash', '<div>{{message}}</div>'); ?>
<?= $this->Session->flash('flash', ['message'=>'ここの変数が展開される']); ?>

継承

 毎回configで指定するのは手間なので継承して、$this->templater()->config()で書き換えおくと便利かもしれません

class FormHelper extends \Cake\View\Helper\FormHelper {
    public function __construct(\Cake\View\View $View, array $config = []) {
        parent::__construct($View, $config);

        // テンプレートの書き換え
        $this->templater()->config('error', '<div class="error-message">エラーです!=>{{content}}</div>');
    }
}

テンプレート外出し

 configで指定するテンプレート自体を外出しすることもできます。

// ctpファイル
// templatesに外出しするテンプレートファイル名を指定する
<?= $this->Form->create($account, ['templates'=>'template']); ?>

//App/Config/template.php
$config = [
    'button' => '<button{{attrs}}>{{text}}</button>',
    'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
    'checkboxContainer' => '<div class="checkbox">{{input}}{{label}}</div>',
    'dateWidget' => '{{year}}{{month}}{{day}}{{hour}}{{minute}}{{second}}{{meridian}}',
    'error' => '<div class="error-message">エラーです!=>{{content}}</div>',
    'errorList' => '<ul>{{content}}</ul>',
    'errorItem' => '<li>{{text}}</li>',
    'file' => '<input type="file" name="{{name}}"{{attrs}}>',
    'fieldset' => '<fieldset>{{content}}</fieldset>',
    'formstart' => '<form{{attrs}}>',
    'formend' => '</form>',
    'hiddenblock' => '<div style="display:none;">{{content}}</div>',
    'input' => '<input type="{{type}}" name="{{name}}"{{attrs}}>',
    'inputsubmit' => '<input type="{{type}}"{{attrs}}>',
    'label' => '<label{{attrs}}>{{text}}</label>',
    'legend' => '<legend>{{text}}</legend>',
    'option' => '<option value="{{value}}"{{attrs}}>{{text}}</option>',
    'optgroup' => '<optgroup label="{{label}}"{{attrs}}>{{content}}</optgroup>',
    'select' => '<select name="{{name}}"{{attrs}}>{{content}}</select>',
    'selectMultiple' => '<select name="{{name}}[]" multiple="multiple"{{attrs}}>{{content}}</select>',
    'radio' => '<input type="radio" name="{{name}}" value="{{value}}"{{attrs}}>',
    'radioContainer' => '{{input}}{{label}}',
    'textarea' => '<textarea name="{{name}}"{{attrs}}>{{value}}</textarea>',
    'formGroup' => '{{label}}{{input}}',
    'checkboxFormGroup' => '{{input}}{{label}}',
    'groupContainer' => '<div class="input {{type}}{{required}}">{{content}}</div>',
    'groupContainerError' => '<div class="input {{type}}{{required}} error">{{content}}{{error}}</div>',
    'submitContainer' => '<div class="submit">{{content}}</div>',
];
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5