Custom tags in Cambridge

14 views
Skip to first unread message

Gp

unread,
Nov 25, 2011, 2:34:41 AM11/25/11
to Cambridge Template Engine
Hi ,
I have recently started using play , I like and prefer cambridge very
much over groovy templates , but I want to know how to add custom
tags in cambridge , I Could not found any documentation ...can you
provide a example with some documentation

Also let me know if know any help in adding some features , If I can
be some help


Thanks ,
Gp

Erdinc Yilmazel

unread,
Nov 25, 2011, 2:11:13 PM11/25/11
to cambridge...@googlegroups.com
Creating custom tags is possible. To implement your custom tag you need to create an implementation of DynamicTag. Your dynamic tag can override any TagNode method to customize the way dynamic tag will be rendered. The most common use case would be converting a template snippet into its own tag. Lets say there is a template like the following that you want to reuse:

<form action="${form.action}" name="${form.name}" method="${form.method}" enctype="${form.encType}">
   <div a:foreach="form.inputs" a:as="input">
      <label for="${input.id}">${input.label}</label><input id="${input.id}" name="${input.name}" type="${input.type}"/>
   </div>
</form>

You would want to use this tag as follows:

<a:form form="yourFormObject"/>

You can create a FormTag class that extends DynamicTag that would render this. Of course you don't want to manually create the dom tree using the java api. You would want to use a template file as a helper. There comes the DynamicTagCreator helper class. This class can render any html snippet, create an instance of FormTag class for you which would contain all the nodes defined in this template snippet.

This exact example is available in the components module. Take a look at FormTag.java, FormTagProvider.java and form.html as an example on how to write your own custom tag.

I am back from my vacation now, I will create more complete examples/documentation over the weekend.

Erdinc
Reply all
Reply to author
Forward
0 new messages