model {
Iterable<Map> cars
}
xmlDeclaration()
cars {
cars.each {
car(make: it.make, model: it.model)
}
}
4 Markup Views
Version: 6.3.0
4 Markup Views
4.1 Introduction
Markup Views are written in Groovy, end with the file extension gml
and reside in the app/views
directory.
The Markup Views plugin uses Groovy’s MarkupTemplateEngine and you can mostly use the Groovy user guide as a reference for the syntax.
Example Markup View:
This produces the following output given a model such as [cars:[[make:"Audi", model:"A5"]]]
:
<?xml version='1.0'?>
<cars><car make='Audi' model='A5'/></cars>
For further examples see Groovy’s MarkupTemplateEngine documentation.
All Markup views subclass the MarkupViewTemplate class by default.
The MarkupViewTemplate
superclass implements the MarkupView trait which in turn extends the the GrailsView trait.
4.2 Installation
To activate Markup views, add the following dependency to the dependencies
block of your build.gradle
:
implementation "org.graceframework.plugins:views-markup:6.3.0"
To enable Gradle compilation of Markup views for production environment add the following to the buildscript
dependencies
block:
buildscript {
...
dependencies {
...
classpath "org.graceframework.plugins:views-gradle:6.3.0"
}
}
Then apply the org.grails.plugins.views-markup
Gradle plugin after any Grace core gradle plugins:
...
apply plugin: "org.graceframework.grace-web"
apply plugin: "org.graceframework.plugins.views-markup"
This will add a compileMarkupViews
task to Gradle that is executed when producing a JAR or WAR file.
4.3 Markup View API
All Markup views subclass the MarkupViewTemplate class by default.
The MarkupViewTemplate
superclass implements the MarkupView trait which in turn extends the the GrailsView trait.
Much of the API is shared between JSON and Markup views. However, one difference compared to JSON views is that you must use this
as a prefix when refering to properties from the parent class. For example to generate links this will produce a compilation error:
cars {
cars.each {
car(make: it.make, href: g.link(controller:'car'))
}
}
However, the following works fine:
cars {
cars.each {
car(make: it.make, href: this.g.link(controller:'car'))
}
}
Notice the this
prefix when refering to this.g.link(..)
.
4.4 Configuration
Markup views configuration can be altered with app/conf/application.yml
. Any of the properties within the MarkupViewConfiguration class and Groovy’s TemplateConfiguration class can be set.
For example:
grails:
views:
markup:
compileStatic: true
cacheTemplates: true
autoIndent: true
...
Alternatively you can register a new MarkupViewConfiguration
bean using the bean name markupViewConfiguration
in app/conf/spring/resources.groovy
.