如何创建cakephp 3自定义模板

时间:2017-03-07 04:28:57

标签: cakephp cakephp-3.0

我是cakephp 3的新手。

我想创建将生成这样的代码的表单助手。

 <div class="form-group">
        <label for="inputEmail3" class="col-sm-3 control-label">Email</label>
        <div class="col-sm-9">
            <input type="email" class="form-control" id="inputEmail3" placeholder="Email" required="">
        </div>
</div>

我正在使用此

<?php 
        $myTemplates = [
            'inputContainer' => '<div class="form-group">{{label}}<div class="col-sm-9">{{input}}</div></div>',
            'inputContainerError' => '<div class="input {{class}} {{type}}{{required}} error">{{content}}{{error}}</div>'
        ];
    ?>
    <?= $this->Form->create('Users',['templates' => $myTemplates]) ?>                       
    <?= $this->Form->input('email',['class' => 'form-control','templates' => ['formGroup' => '{{input}}{{label}}']]) ?>

但它会产生错误的

2 个答案:

答案 0 :(得分:0)

Per the docs - 在运行时使用setTemplate更改主题内联:

$myTemplates = [
    'inputContainer' => '<div class="form-group">{{label}}<div class="col-sm-9">{{input}}</div></div>',
    'inputContainerError' => '<div class="input {{class}} {{type}}{{required}} error">{{content}}{{error}}</div>'
];
?>
<?= $this->Form->create('Users') ?>
<? $this->Form->setTemplates($myTemplates); ?>
<?= $this->Form->input('email',['class' => 'form-control','templates' => ['formGroup' => '{{input}}{{label}}']]) ?>

您无法在表单上使用templates选项作为动态模板,它只会在/config中查找列出模板项的配置文件。

答案 1 :(得分:0)

首先创建模板代码

<?php $myTemplates = [
'label' => '<label class="col-sm-3 col-form-label">{{text}}</label>',
'select' => '<div class="col-sm-9"><select class="form-control" name="{{name}}"{{attrs}}>{{content}}</select></div>',
'input' => '<div class="col-sm-9"><input class="form-control" type="{{type}}" name="{{name}}"{{attrs}}></div>',
'inputContainer' => '<div class="form-control">{{content}}</div>',
'inputSubmit' => '<input type="{{type}}"{{attrs}}>',
'inputContainer' => '<div class="form-group row">{{content}}</div>',
'submitContainer' => '<div class="form-group row"><label class="col-sm-3 col-form-label">&nbsp;</label><div class="col-sm-9 ">{{content}}</div></div>',
]; ?>

现在在表单中使用此模板代码

<?php echo $this->Form->create($schoolAccount); ?>  
<?php $this->Form->setTemplates($myTemplates); ?>
<?php echo $this->Form->control('account_holder',['label'=>'Name of Account Holder']); ?>
<?php echo $this->Form->end(); ?>