aalaap aalaap - 4 months ago 31
HTML Question

How do I format long lines in Twig templates, according to coding standards?

I have the following code in my templates, in which I'm generating routes with parameters:

<a class="btn btn-block btn-abcd" href="{{ path(
'auth.login',
{
'type': constant('User::TYPE_CANDIDATE'),
'provider': constant('UserProvider::PROVIDER_XING')
}
) }}">...</a>


I've split it up into lines the way I'd do it with PHP code. I'm just not sure if this is the recommended practice or not. The coding standards page on the Twig site doesn't address this specific issue.

Answer

A commonly used technique for readability, is that whenever you feel you need to shorten lines, you do it at an open parenthesis/bracket/brace. Not necessarily on all of them, but one that makes sense.

Here is my recommendation:

<a class="btn btn-block btn-abcd"
    href="{{ path(
        'auth.login', {
            'type': constant('User::TYPE_CANDIDATE'),
            'provider': constant('UserProvider::PROVIDER_XING')
        }
    ) }}">
    Link text
</a>

One problem with single-line reformatting, is that a lot of mess detectors and linters will scoff at lines longer than 50 characters.