There are two ways to create customs tags with the play framework.
- By defining a groovy template in app/view/tags
- Directly in pure java by having a class extend FastTags
The latest is NOT documented.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
So, similar to how JavaExtensions work by extending the JavaExtensions class, to create a FastTag you need to create a class that extends FastTags. Each method that you want to execute as a tag needs to conform to the following method structure.
Note the underscore before the name of the tag.
To understand how to build an actual tag, the easiest way is to look at the source code for a FastTag, and see one in action.
Here is the source straight from git hub.
https://github.com/playframework/play/blob/master/framework/src/play/templates/FastTags.java
Below are a few I have copied, so that I can explain how this works.
So, this first method is the verbatim tag, and simply calls the toString method on the JavaExtensions, and passes in the body of the tag. The body of the tag would be anything between the open and close tag. So
The body value would be
The second example, is slightly more complex. It is a tag that relies on a parent tag to function.
This code works by outputting an HTML option tag, and sets the selected value, by checking which value is selected from the parent tag. The first 3 lines just get data, and set up the data ready to output. Then, the final 3 lines outputs the result of the tag.
There are many more examples in the source code I have linked to, with varying degrees of complexity, but hopefully this will be a good starting point for you.
To ensure that your tags do not conflict between projects, or with the core Play tags, you can set up namespaces, using the class level annotation @FastTags.Namespace.
So, for a hello tag, in a namespace of my.tags, you would do the following
and then in your templates, you would reference the hello tag as