Groovy templates




















Movie Page will show movie's data in read-only mode. From this page, user can decide to "Edit" or "Delete" movie. If user choose Edit, it will go to Edit Movie Page.

If user choose Delete, it will lead to Delete Movie Page. Upon successful update, the controller also will redirect to Movie Page to view updated movie data.

Desson Ariawan Programmer. Tutorials Spring. Uses Apache Tomcat as the default embedded container. Groovy Templates Groovy templating engine. Lombok Java annotation library which helps to reduce boilerplate code. It's a simple Movie class, with this structure: ID Primary key of this entity Title Release Year Running time in minutes Tags example: action, comedy, drama Language Country Yes, cannot compete with imdb, but should be good enough for our example.

Alternatively, if the developer is also the one who writes the templates, it is possible to declare the types of the expected variables directly in the template. In this case, even if you call createTemplate , it will be type checked:. An additional interest of using type checked models is that performance should improve. By telling the type checker what are the expected types, you also let the compiler generate optimized code for that, so if you are looking for the best performance, consider using type checked templates.

Also, there are other templating solutions that can be used along with Groovy, such as FreeMarker , Velocity , StringTemplate and others. Template engines version 3. Introduction Groovy supports multiple ways to generate text dynamically including GStrings , printf and MarkupBuilder just to name a few.

Template framework The template framework in Groovy consists of a TemplateEngine abstract base class that engines must implement and a Template interface that the resulting templates they generate must implement. SimpleTemplateEngine - for basic templates StreamingTemplateEngine - functionally equivalent to SimpleTemplateEngine , but can handle strings larger than 64k GStringTemplateEngine - stores the template as writeable closures useful for streaming scenarios XmlTemplateEngine - works well when the template and output are valid XML MarkupTemplateEngine - a very complete, optimized, template engine.

SimpleTemplateEngine Shown here is the SimpleTemplateEngine that allows you to use JSP-like scriptlets see example below , script, and EL expressions in your template in order to generate parametrized text.

Advanced Usage Note If you happen to be embedding your template directly in your script as we did above you have to be careful about backslash escaping. StreamingTemplateEngine The StreamingTemplateEngine engine is functionally equivalent to the SimpleTemplateEngine , but creates the template using writable closures making it more scalable for large templates.

The conference committee. GStringTemplateEngine As an example of using the GStringTemplateEngine , here is the example above done again with a few changes to show some other options. See you in December, Groovy-Dev.

The template format 7. Basics Templates consist of Groovy code. With some configuration , you can have the output pretty printed, with newlines and indent automatically added. Support methods In the previous example, the doctype declaration was rendered using the yieldUnescaped method. If TemplateConfiguration getDeclarationEncoding is not null:. Includes The MarkupTemplateEngine supports inclusion of contents from another file.

Including a file as raw contents, without escaping it, can be done like this:. Eventually, inclusion of text that should be escaped before rendering can be done using:. Fragments Fragments are nested templates. Layouts Layouts, unlike fragments, refer to other templates.

Rendering contents 7. Creation of a template engine On the server side, rendering templates require an instance of groovy. Configuration options The behavior of the engine can be tweaked with several configuration options accessible through the TemplateConfiguration class:. See the auto escape section. See the auto formatting section. See the custom templates section. See the internationalization section. Once the template engine has been created, it is unsafe to change the configuration.

Automatic formatting By default, the template engine will render output without any specific formatting. Automatic escaping By default, contents which is read from the model is rendered as is.

It is slower than using the version with calls to yield which perform direct streaming of the markup instead. Internationalization The template engine has native support for internationalization. Default text. Custom template classes By default, templates created inherit the groovy. The custom base class has to extend BaseClass like in this example:.

Type checked templates 7. Optional type checking Even if templates are not type checked, they are statically compiled. This time, when the template is compiled at the last line, an error occurs:. Here is the output of the above program,. Till now, we have seen that the Groovy template text is hard-coded directly into the source files. Now, let us see how to externalize the template text by keeping it in a file called 'mydata.

The content of the file is as follows, mydata. The above XML file has three place-holders for name , designation and location. Back to the source code, we have defined a map of values for these three identifiers like,. Till now, we have been using SimpleTemplateEngine for simple cases and if we want to go for template processing with performance in mind, then GStringTemplateEngine will be a better choice. XmlTemplateEngine takes care of reading the template text and writing the substituted text from and back to the XML file for fine tuning purposes and it takes care of formatting also.

The Template API is very short and simple and it can be used widely for text processing and substitution. In the beginning of this article, we happened to see about Templates along with examples.

To get a feel over the API, this various samples have been provided in the later end of the article. The following are some of the popular articles published in Javabeat. Also you can refer the list of groovy articles and groovy books from our website. From the output it can be seen that the values are successfully replaced in the relevant placeholders. The StreamingTemplateEngine engine is another templating engine available in Groovy.

This is kind of equivalent to the SimpleTemplateEngine, but creates the template using writeable closures making it more scalable for large templates. Specifically this template engine can handle strings larger than 64k.

The XmlTemplateEngine is used in templating scenarios where both the template source and the expected output are intended to be XML.



0コメント

  • 1000 / 1000