CakePHP 3.4多次使用setTemplates()
我试图在一个视图中多次使用CakePHP 3.4多次使用setTemplates(),cakephp,cakephp-3.4,Cakephp,Cakephp 3.4,我试图在一个视图中多次使用setTemplates(),但它没有按预期工作 我有两个模板,我想设置时,我需要他们 $bootstrapTemplate = [ 'inputContainer' => '<div class="form-group {{type}}{{required}}">{{content}}</div>', ]; $bootstrapTemplateInputGroup = [ 'inputContainer' => '
setTemplates()
,但它没有按预期工作
我有两个模板,我想设置时,我需要他们
$bootstrapTemplate = [
'inputContainer' => '<div class="form-group {{type}}{{required}}">{{content}}</div>',
];
$bootstrapTemplateInputGroup = [
'inputContainer' => '<div class="form-group {{type}}{{required}}">{{content}}</div>',
'input' => '<div class="input-group"><div class="input-group-addon">€</div><input type="{{type}}" name="{{name}}"{{attrs}}/></div>'
];
我的预期输出是$bootstrapTemplate
的模板,如:
<div class="form-group text required"><label for="user_zip">PLZ</label><input type="text" name="user_zip" class="form-control" required="required" maxlength="255" id="user_zip"></div>
PLZ
我做错了什么?
FormHelper::setTemplates()
不会覆盖完整的现有模板集,而是将其与给定模板合并,即第二次调用的输入
模板集将保持更改
您必须使用底层模板器从模板堆栈中push()
到(存储)和pop()
(还原)以避免以下情况:
$this->Form->templater()->push();
$this->Form->templater()->add($bootstrapTemplateInputGroup);
$this->Form->templater()->pop();
或者使用FormHelper::create()
方法的templates
选项将模板仅应用于特定的FormHelper::control()
调用:
另见
echo$this->Form->control('user_-zip',['templates'=>$bootstrapTemplate,
解决方案。它更简单,可读性更强。
<div class="form-group text required"><label for="user_zip">PLZ</label><input type="text" name="user_zip" class="form-control" required="required" maxlength="255" id="user_zip"></div>
$this->Form->templater()->push();
$this->Form->templater()->add($bootstrapTemplateInputGroup);
$this->Form->templater()->pop();
echo $this->Form->control('title', ['templates' => $bootstrapTemplate, /*...*/]);
echo $this->Form->control('price', ['templates' => $bootstrapTemplateInputGroup, /*...*/]);
echo $this->Form->control('user_zip', ['templates' => $bootstrapTemplate, /*...*/]);