-
Notifications
You must be signed in to change notification settings - Fork 11
EN.03.05.Mock Custom Tag and Function
Document of mock custom tag and function.
When configure function
and tag
section at project.yaml, be able to implement mock tag and function by Groovy script.
Root directory of tag. Specify a relative path from project root path.
Root directory of function. Specify a relative path from project root path.
tag:
root: ./tag
function:
root: ./function
Support Custom Directive in freemarker as custom tag, object registered to Shared Variable as custom function.
Create tagname.groovy
at tag root direcotry. Support naming like s.text
in Struts2.
In this script available original variables of Freemarker and Builtin Variables.
variable name | type | content |
---|---|---|
_env | freemarker.core.Environment | Environment object |
_params | java.util.Map | attribute map of tag |
_loopVars | freemarker.template.TemplateModel[] | loop variables |
_body | freemarker.template.TemplateDirectiveBody | body part of tag |
_writer | java.io.Writer | output result of Writer |
_data | java.util.Map | content of data file |
Create span tag with special class, as span.groovy
.
Add output result of tag to _writer
. The script that specified class and evaluate the internal of @span
tag is as follows.
_writer.write("""<span""")
// add class attribute
_writer.write(""" class="special-span" """)
// write tag attributes
for (entry in _params) {
_writer.write(""" ${entry.key}=\\"${entry.value}\\"""")
}
_writer.write(""">""")
if (_body != null) {
// render inner body.
_body.render(_writer)
}
_writer.write("""</span>""")
Call created tag script, as follows.
<body>
<@span>aaaa</@span>
</body>
Create functionname.groovy
at function root direcotry.
In this script available original variables of Freemarker and Builtin Variables.
staticUrl
is mock implementation of function, returns given string of URI with host name of Aeromock.
if (arguments.isEmpty()) {
throw new RuntimeException("Argument required")
}
return "http://localhost:3183${arguments[0]}"
Call created function script, as follows.
<body>
${staticUrl('/img/sample.jpg')}
</body>
variable name | type | content |
---|---|---|
arguments | java.util.List | argument of function |
_data | java.util.Map | content of data file |
Support Helpers as function。Tag is not available.
Create functionname.groovy
at function root direcotry.
In this script available original variables of Handlebars.java and Builtin Variables.
variable name | type | content |
---|---|---|
argument | depends on argument of function | argument of function |
options | com.github.jknack.handlebars.Options | @See Options.java |
_data | java.util.Map | content of data file |
staticUrl
is mock implementation of function, returns given string of URI with host name of Aeromock.
if (argument == null || argument.length() == 0) {
throw new IllegalArgumentException("Argument required")
}
return "http://${HOST}${argument}"
Call created helper, as follows.
<li>staticUrl(original) = <img src="{{staticUrl "/img/sample.jpg"}}"/></li>
Support Helpers of Jade4j as function。Tag is not available.
Create functionname.groovy
at function root direcotry.
In Jade4j, not support builtin variables and data in data file be referenced in this script.
Helpers of Jade4j do not have restraints of interface, so need to define Helper class in the script.
Create helper of math.round
that be able to round a numeric value as math.groovy
. Define Helper class and return that instance in this script. Be careful to be different approach like Freemarker, and Handlebars.java.
class MathHelper {
long round(double number) {
return Math.round(number)
}
}
return new MathHelper()
Call created helper, as follows.
p= math.round(1.44)
There is Velocimacros in Velocity, so custom tag and function is not supported.
Groovy Templates has function of script own self、so custom tag and function is not supported.
Utility Object of Thymeleaf as function。Tag is not available.
Create functionname.groovy
at function root direcotry.
In Thymeleaf, not support builtin variables and data in data file be referenced in this script.
Utility Object of Thymeleaf does not have restraints of interface, so need to define Utility Object class in the script.
Create Utility Object of helper
as helper.groovy
at function root. Define Utility Object class and return that instance in this script.
class HelperUtilityObject {
long round(double number) {
return Math.round(number)
}
String hello(String message) {
return "Hello!! $message"
}
}
return new HelperUtilityObject()
Call created Utility Object, as follows.
<h4>function</h4>
<ul>
<li><span th:text="${#helper.hello('neko')}">hoge</span></li>
<li><span th:text="${#helper.round(1.1)}">hoge</span></li>
</ul>