verbb / navigation

A Craft CMS plugin to create navigation menus for your site.
Other
90 stars 23 forks source link

linkAttributes additional attributes do not render expected markup #287

Closed acalvino4 closed 2 years ago

acalvino4 commented 2 years ago

Description Using node.linkAttributes with additional attributes does not render expected markup

Steps to reproduce

  1. Include {{ node.linkAttributes([{attribute: 'class', value: "text-base text-gray-500 hover:text-gray-900"}]) }} in twig template as directed by the documentation.
  2. Observe class="" href="https://example.com/url/path" 0='{"attribute":"class","value":"text-base text-gray-500 hover:text-gray-900"}' output into your markup.

Expected Behavior Observe class="text-base text-gray-500 hover:text-gray-900" href="https://example.com/url/path" output in my markup.

Additional info

Additional context Since I know linkAttributes already injects a class attribute based on cp contents (as evidenced by class="" in the output above), I tried this using a different attribute and got the same result. So it seems there is either a bug or the documentation is incorrect.

As a side note, a much nicer syntax for this would be

{{ node.linkAttributes({
    class: "text-base text-gray-500",
    id: "a-heading",
    ...
}) }}

since it avoids the verbosity and confusion of ([{, and the verbosity of attribute and value in there. Just a thought.

Thanks for looking into this!

engram-design commented 2 years ago

I'm pretty sure the docs are wrong, as it should be exactly as you describe:

{{ node.linkAttributes({
    class: "text-base text-gray-500",
    id: "a-heading",
    ...
}) }}

That should work - apart from merging attributes defined in the node, which is fixed in 1.4.25

acalvino4 commented 2 years ago

Thanks, I'll try it out! Thanks for such a fast response!