Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better class attribute rendering #181

Open
GeertDD opened this issue Oct 28, 2014 · 3 comments
Open

Better class attribute rendering #181

GeertDD opened this issue Oct 28, 2014 · 3 comments

Comments

@GeertDD
Copy link
Contributor

GeertDD commented Oct 28, 2014

When setting the firstClass and/or lastClass options to null or an empty string, like this:

{{ knp_menu_render('AcmeDemoBundle:Builder:mainMenu', { 'firstClass': null, 'lastClass': null }) }}

A menu with the following class attributes is rendered:

<ul>
    <li class="current ">...</li>
    <li class="">...</li>
</ul>

Even though this is completely valid markup, it would be nice if the trailing space after the class current would be gone, as well as the empty class attributes:

<ul>
    <li class="current">...</li>
    <li>...</li>
</ul>
@Jbekker
Copy link

Jbekker commented Oct 29, 2014

I agree on this, I wouldn't mind to create a pull request to fix this. I think all empty attributes shouldn't be printed, right?

Editing the attribributes macro in Resources/views/knp_menu.html.twig to something like I did below is al we need to do:

{% macro attributes(attributes) %}
{% for name, value in attributes %}
    {%- if value is not empty -%}
        {{- ' %s="%s"'|format(name, value is sameas(true) ? name|e : value|e|trim)|raw -}}
    {%- endif -%}
{%- endfor -%}
{% endmacro %}

@stof
Copy link
Collaborator

stof commented Jun 15, 2015

If the value of an attribute is null or false, it is already omitted.

However, the class attribute is indeed not handled properly when empty as string concatenation will still produce an empty string

@dbu
Copy link
Collaborator

dbu commented Jun 21, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants