I am copying the select tag from playframework to test out creating tags as well as fasttags(doing the option as a fasttag). But the only problem is I get this error when it should be looking for the fasttag…
The template tags/alvazan/option.html or tags/alvazan/option.tag does not exist.
My FastTags class is in the app/tags directory and is the following code….
package tags;
import groovy.lang.Closure;
import java.io.PrintWriter;
import java.util.Map;
import play.templates.FastTags;
import play.templates.JavaExtensions;
import play.templates.TagContext;
import play.templates.GroovyTemplate.ExecutableTemplate;
@FastTags.Namespace("alvazan")
public class TagHelp extends FastTags {
public static void _option(Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
Object value = args.get("arg");
TagContext ctx = TagContext.parent("alvazanselect");
Object selectedValue = ctx.data.get("selected");
boolean selected = selectedValue != null && value != null && (selectedValue.toString()).equals(value.toString());
out.print("<option value=\"" + (value == null ? "" : value) + "\" " + (selected ? "selected=\"selected\"" : "") + " " + FastTags.serialize(args, "selected", "value") + ">");
out.println(JavaExtensions.toString(body));
out.print("</option>");
}
}
My html then has this which is not found…
#{alvazan.option/}
The code here implies it would never look up a fasttag(where is the code that looks up fasttags hidden)…
public void invokeTag(Integer fromLine, String tag, Map<String, Object> attrs, Closure body) {
String templateName = tag.replace(".", "/");
String callerExtension = "tag";
if (template.name.indexOf(".") > 0) {
callerExtension = template.name.substring(template.name.lastIndexOf(".") + 1);
}
BaseTemplate tagTemplate = null;
try {
tagTemplate = (BaseTemplate)TemplateLoader.load("tags/" + templateName + "." + callerExtension);
} catch (TemplateNotFoundException e) {
try {
tagTemplate = (BaseTemplate)TemplateLoader.load("tags/" + templateName + ".tag");
} catch (TemplateNotFoundException ex) {
if (callerExtension.equals("tag")) {
throw new TemplateNotFoundException("tags/" + templateName + ".tag", template, fromLine);
}
throw new TemplateNotFoundException("tags/" + templateName + "." + callerExtension + " or tags/" + templateName + ".tag", template, fromLine);
}
}
TagContext.enterTag(tag);
Map<String, Object> args = new HashMap<String, Object>();
args.put("session", getBinding().getVariables().get("session"));
args.put("flash", getBinding().getVariables().get("flash"));
args.put("request", getBinding().getVariables().get("request"));
args.put("params", getBinding().getVariables().get("params"));
args.put("play", getBinding().getVariables().get("play"));
args.put("lang", getBinding().getVariables().get("lang"));
args.put("messages", getBinding().getVariables().get("messages"));
args.put("out", getBinding().getVariable("out"));
args.put("_attrs", attrs);
// all other vars are template-specific
args.put("_caller", getBinding().getVariables());
if (attrs != null) {
for (Map.Entry<String, Object> entry : attrs.entrySet()) {
args.put("_" + entry.getKey(), entry.getValue());
}
}
args.put("_body", body);
try {
tagTemplate.internalRender(args);
} catch (TagInternalException e) {
throw new TemplateExecutionException(template, fromLine, e.getMessage(), template.cleanStackTrace(e));
} catch (TemplateNotFoundException e) {
throw new TemplateNotFoundException(e.getPath(), template, fromLine);
}
TagContext.exitTag();
}
2 questions
- Why is this not working?
- Where is the code in playframework source that looks up the fasttag “class” instead of looking up an html file?
Why is this not working?
Okay, I can’t really answer this because in my case your FastTag worked. I wasn’t able to reproduce your error. I only did some minor adjustments like adding a body so I wouldn’t get any errors, but they shouldn’t be the cause of your error. But just to make sure, the proper way to use this tag would be:
Where is the code in Play! Framework source that looks up the FastTags “class” instead of looking up an html file?
I think you looked over some code in the
endTag()method of theGroovyTemplateCompilerin it’s last catch block you will find the following snippet, which does try to load the FastTags before tryinginvokeTag(). I’ve also added some extra comments for clarity.