Cakephp 3 multiple custom template formhelpers

后端 未结 2 1659
一整个雨季
一整个雨季 2021-02-06 11:04

So I\'m at work (working with sensitive data might I add for posterity\'s sake), and the powers that be decide we need to use the all powerful and least documented new tool by C

2条回答
  •  生来不讨喜
    2021-02-06 11:35

    Let's say you need all inputs to use a custom markup in a form to show the label after the input (default is before) and a different class for the hardcoded error-message for errors:

    $this->Form->create($entity, ['templates' => [
        'formGroup' => '{{input}}{{label}}',
        'error' => '
    {{content}}
    ' ]]);

    If you want to only customize a single input, pass the same 'templates' key to the FormHelper::input() options like so:

    $this->Form->input('fieldname', ['templates' => [
        'formGroup' => '{{input}}{{label}}',
        'error' => '
    {{content}}
    ' ]]);

    If you need to define multiple templates and re-use them whenever you want, here's something you can try (mind I am writing it here, never used it before):

    // in bootstrap (as this is a config i believe
    Configure::write('templates', [
        'foo' => [....],
        'bar' => [....]
    ]);
    
    // in any view
    $this->Form->templates(Configure::read('templates.foo'));
    $this->Form->create(...);
    ....
    

    You could also create your own helper and define the templates there, etc.

    It really all depends on what you want to achieve but that should give you a good understanding of how templates work (not just in forms by the way).

提交回复
热议问题