CakePHP 3.4多次使用setTemplates()

时间:2017-12-09 20:18:18

标签: cakephp cakephp-3.4

我正在尝试在视图中多次使用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>'
];

我开始像这样设置模板

$this->Form->setTemplates($bootstrapTemplate);

$this->Form->control('title', ['class' => 'form-control', 'label' => __('Titel')]);

// OUTPUT - correct
// <div class="form-group text required"><label for="title">Titel</label><input type="text" name="title" class="form-control" required="required" maxlength="255" id="title"></div>

$this->Form->setTemplates($bootstrapTemplateInputGroup);
echo $this->Form->control('price', ['class' => 'form-control', 'id' => 'price_eur', 'label' => __('Preis EUR')]).'</div>';

// OUTPUT - correct
<div class="form-group number required"><label for="price_eur">Preis EUR</label><div class="input-group"><div class="input-group-addon">€</div><input type="number" name="price" class="form-control" id="price_eur" required="required" step="any"></div></div>

现在我想切换回$bootstrapTemplate,这似乎不起作用。而是使用$bootstrapTemplateInputGroup

$this->Form->setTemplates($bootstrapTemplate);
echo $this->Form->control('user_zip', ['class' => 'form-control', 'id' => 'user_zip', 'label' => __('PLZ')])

// OUTPUT - wrong
<div class="form-group text"><label for="user_zip">PLZ</label><div class="input-group"><div class="input-group-addon">€</div><input type="text" name="user_zip" class="form-control" id="user_zip"></div></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>

我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

input不会覆盖完整的现有模板集,它会将其与给定模板合并,即第二次调用时设置的push()模板将保持更改。

您必须使用底层模板来从pop()到(存储)和$this->Form->templater()->push(); $this->Form->templater()->add($bootstrapTemplateInputGroup); $this->Form->templater()->pop(); (恢复)模板堆栈以避免这种情况:

FormHelper::create()

或使用templates方法的FormHelper::control()选项仅将模板应用于特定的echo $this->Form->control('title', ['templates' => $bootstrapTemplate, /*...*/]); echo $this->Form->control('price', ['templates' => $bootstrapTemplateInputGroup, /*...*/]); echo $this->Form->control('user_zip', ['templates' => $bootstrapTemplate, /*...*/]); 来电:

SELECT DISTINCT
    coins.price_btc,
    coins.price_eur,
    coins.price_usd,
    coins.currency_name,
    coins.currency_symbol,
    SUM ( market_transactions.quantity ) OVER ( PARTITION BY market_transactions.market_coin_id ) * coins.price_eur AS holdings_eur,
    SUM ( market_transactions.quantity ) OVER ( PARTITION BY market_transactions.market_coin_id ) * coins.price_usd AS holdings_usd,
    SUM ( market_transactions.quantity ) OVER ( PARTITION BY market_transactions.market_coin_id ) * coins.price_btc AS holdings_btc,
    SUM ( market_transactions.quantity ) OVER ( PARTITION BY market_transactions.market_coin_id ) AS holdings 
FROM
    market_transactions
    INNER JOIN coins ON coins.id = market_transactions.market_coin_id 
WHERE
    market_transactions.user_id = 1 
ORDER BY
    coins.currency_symbol

另见