Skip to content

EN.03.05.Mock Custom Tag and Function

Akinori Yamada edited this page Aug 18, 2014 · 2 revisions

Document of mock custom tag and function.

project.yaml

When configure function and tag section at project.yaml, be able to implement mock tag and function by Groovy script.

tag.root

Root directory of tag. Specify a relative path from project root path.

function.root

Root directory of function. Specify a relative path from project root path.

Example

tag:
  root: ./tag

function:
  root: ./function

Freemarker

Support Custom Directive in freemarker as custom tag, object registered to Shared Variable as custom function.

Create Mock Tag

Create tagname.groovyat 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

Example

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.

span.groovy
_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.

test.ftl
<body>
<@span>aaaa</@span>
</body>

Create Mock Function

Create functionname.groovyat function root direcotry.

In this script available original variables of Freemarker and Builtin Variables.

Example

staticUrl is mock implementation of function, returns given string of URI with host name of Aeromock.

staticUrl.groovy
if (arguments.isEmpty()) {
  throw new RuntimeException("Argument required")
}
return "http://localhost:3183${arguments[0]}"

Call created function script, as follows.

test.ftl
<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

Handlebars.java

Support Helpers as function。Tag is not available.

Create Mock Function

Create functionname.groovyat 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

Example

staticUrl is mock implementation of function, returns given string of URI with host name of Aeromock.

staticUrl.groovy
if (argument == null || argument.length() == 0) {
    throw new IllegalArgumentException("Argument required")
}
return "http://${HOST}${argument}"

Call created helper, as follows.

test.hbs
<li>staticUrl(original) = <img src="{{staticUrl "/img/sample.jpg"}}"/></li>

Jade4j

Support Helpers of Jade4j as function。Tag is not available.

Create Mock Function

Create functionname.groovyat 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.

Example

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.

math.groovy
class MathHelper {

    long round(double number) {
        return Math.round(number)
    }

}

return new MathHelper()

Call created helper, as follows.

test.jade
p= math.round(1.44)

Velocity

There is Velocimacros in Velocity, so custom tag and function is not supported.

Groovy Templates

Groovy Templates has function of script own self、so custom tag and function is not supported.

Thymeleaf

Utility Object of Thymeleaf as function。Tag is not available.

Create Mock Function

Create functionname.groovyat 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.

Example

Create Utility Object of helper as helper.groovy at function root. Define Utility Object class and return that instance in this script.

helper.groovy
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>
Clone this wiki locally