claudioivp
claudioivp

Reputation: 549

Java Freemarker macro with optional css classes

I'm trying to do a macro on freemarker, but I'm having problems to implement css class as parameter. My object have some default css classes and I would like to add optional classes.

<#macro Button href extra...>
    <a href="${href}" class="ui-button"
    <#list extra?keys as attr>
    ${attr}="${extra[attr]?html}"
    </#list>
    >Anchor Button</a>
</#macro>

1) <@Button href="link.html"></@Button>
2) <@Button href="link.html" id="button1" class="marginrightnone"></@Button>

The line 2) only is rendering the "id" parameter. If I delete class="ui-button" of the macro, then it renders correctly.

What I could to do to render two or more class parameters???

Upvotes: 1

Views: 2526

Answers (1)

Dave Newton
Dave Newton

Reputation: 160191

You need to construct a string containing all the class parameters and use that as the value of a single HTML class attribute in the template.

You can't have an arbitrary number of class attribute/value pairs and still be legal HTML.

The simplest that would be basically what you have now would be to create a local with the "ui-button" value in it. As you iterate over extra?keys check for a "class" key and if found, append it to the local class (along with a leading space). The template would use that constructed value:

<a href="${href}" classes="${local_classes}"

Upvotes: 2

Related Questions