Question

I have the following chunk of code for writing out a series of button elements, containing an icon and text:

def tagcloud = { attrs, body ->

    def mb = new MarkupBuilder(out)

    mb.ul('class': 'list-inline') {
        def tag = it
        attrs.tags.split(",").each {
            li {
                button('class': 'btn btn-default', 'type': 'submit') {
                    i('class': 'fa fa-tag', '')
                    mb.yield('test')
                }
            }   
        }
    }
}

However, I am finding when using the yield function that the markup builder is outputting the yield call as a tag in my html, rather then the raw text:

<li>
    <button class="btn btn-default" type="submit">
      <i class="fa fa-tag"></i>
      <yield>test</yield>
    </button>
</li>

Based on my research, this is the recommended way of doing this: HTML using Groovy MarkupBuilder, how do I elegantly mix tags and text?

Does anyone know why this text is being wrapped liked this?

I am using Grails 2.3.8.

Était-ce utile?

La solution

Simply this way:

def tagcloud = { attrs, body ->

    def mb = new MarkupBuilder(out)

    mb.ul('class': 'list-inline') {
        def tag = it
        attrs.tags.split(",").each {
            li {
                button('class': 'btn btn-default', 'type': 'submit') {
                    i('class': 'fa fa-tag', '')
                    mkp.yield('test')
                }
            }   
        }
    }
}

mkp is a special namespace used to escape away from the normal building mode of the builder and get access to helper markup methods such as 'yield' and 'yieldUnescaped'. See the javadoc for getMkp() for further details.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top