#{extends '../main.html'/}# #{set title:'Webpieces QuickStart'/}# #{set tab:'management'/}# #{renderTagArgs 'docHome.html'/}# #{renderTagArgs 'quickStartList.html'/}#

Custom Tags

There are two ways to write a custom tag. One is simply creating an html file with the extension '.tag' and then wiring it into 2 locations.

  1. The build.gradle file so we can check for tag mispellings
  2. The webpieces platform so it knows about this new tag to compile html code correctly

Please note that creating your own tag is much like a shortcut for using renderPageArgs tag or renderTagArgs tag as well. You could use those which are built in or you can create your own tag.

To create your own tag, first create helloworld.tag:

*[

Hello ${name}$. Your id is ${id}$

]*

Next, modify MyHtmlTagLookup.java which we set as an override to the default webpieces HtmlTagLookup.java to the following so the tag is known:

*[package org.webpieces.helloworld.base.tags;

import javax.inject.Inject;

import org.webpieces.templating.api.ConverterLookup;
import org.webpieces.templating.api.HtmlTagLookup;
import org.webpieces.templating.api.RouterLookup;
import org.webpieces.templating.api.TemplateConfig;
import org.webpieces.templating.impl.tags.CustomTag;

public class MyHtmlTagLookup extends HtmlTagLookup {

	@Inject
	public MyHtmlTagLookup(TemplateConfig config, RouterLookup lookup, ConverterLookup converter) {
		super(config, lookup, converter);
		//add any custom tags you like here...
		put(new CustomTag("/org/webpieces/helloworld/base/tags/mytag.tag"));
		put(new IdTag(converter, "/org/webpieces/helloworld/base/tags/id.tag"));
		put(new MyFieldTag(converter));

		put(new CustomTag("/org/webpieces/helloworld/myapp/helloworld.tag"));
	}

}
]*

Then, let's modify dynamicHelloWorld.html to use this new tag like so:

*[
    
    
        #{helloworld name:name, id:id/}#
    
]*

Now, visit http://localhost:8080/helloworld/asdfewee/3434344 to see the displayed content. Notice that we have to pass in the name and the id to the tag since the tag requires tag and name. Now, if we want the gradle build to work, one last step is needed. Since during the gradle build we verify all tags are spelt correctly, you must add the tag to the list of customTags like so:

*[compileTemplateSetting {
  //Since the groovy plugin has no way of knowing about your custom tags, list them here or the compile will
  //fail (This catches mispellings and such so you don't release a broken app to production)
  customTags = [ "mytag", "anothertag", "id", "helloworld" ]
}]*

Subclassing HtmlTag

The next method is subclassing HtmlTag.java, RenderTagArgs.java, or RenderPageArgs.java. At it's surface, HtmlTag simply has two methods to implement:

*[public interface HtmlTag {

	void runTag(Map args, Closure body, PrintWriter out, GroovyTemplateSuperclass template, String srcLocation);

	String getName();

}]*

getName method just returns the actual tag name that is used so if you return 'bumhole' then, you can use the tag *[#{bumhole/}#]*. The second method runTag gives you a PrintWriter to generate html too as well as the tag arguments in a Map and the body in a closure that was not run yet.

In general, calling these two methods gives you the page args(different from the tag arguments):

*[Binding binding = parentTemplate.getBinding();
Map pageArgs = binding.getVariables();]*

Well, that's it for creating custom Tags. There is also one last thing GroovyGen.java that could be subclassed but if you do, you would need to push those changes to webpieces right now so that the generator code can be looked up. This piece allows even more advanced configurability.

Next up is explaining the main Server.java and other critical pieces of code that was generated for you but you know own and maintain. We will explain that so you can better maintain it.

Next Server.java Explained