dotkernel / frontend

DotKernel Frontend Application. Based on Mezzio microframework using Laminas components.
https://v5.dotkernel.net
MIT License
23 stars 5 forks source link

Refactor templates #469

Closed alexmerlin closed 2 months ago

alexmerlin commented 2 months ago

We barely touched the template files and they are full of dead-weight that we carry around from version to version. Since we prepare to release a new major version, we should take the opportunity to clean up these files. The below suggestions mention only specific examples, but the refactoring should be done at project level, on all templates (where they apply).


Move form preparation from template to controller:

Example:

{% set dummy = form.prepare() %}

Unless necessary, this should be executed from the controller.

So, this:

return new HtmlResponse(
    $this->template->render('user::register', [
        'form' => $form,
    ])
);

should look like this:

return new HtmlResponse(
    $this->template->render('user::register', [
        'form' => $form->prepare(),
    ])
);

Since #468 parsing form elements contains an unnecessary step:

Example:

{% set firstName = details.get('firstName') %}
{% set elementPlaceholder = firstName.getAttribute('placeholder')|raw %}
{% set dummy = firstName.setAttribute('placeholder', elementPlaceholder) %}
{{ formElement(firstName) }}

We can remove the two placeholder-related lines, so it becomes:

{% set firstName = details.get('firstName') %}
{{ formElement(firstName) }}

Then, if we are not doing anything else with the form element, we can also get rid of the variable:

{{ formElement(details.get('firstName')) }}

Move Form / FormElement operations from template to controller / form:

  1. If a form attribute will always be the same, we should set it in the form itself:
$this->setAttribute('method', RequestMethodInterface::METHOD_POST);
  1. If a form attribute might change, we should set them at controller level:

    $form = new RegisterForm();
    $form->setAttribute('action', $this->router->generateUri('register'));
  2. If a form element attribute can be determined at controller level, we should do so - see this example.