Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

field.tag as FastTag (Issue #134) #38

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions framework/src/play/templates/FastTags.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import play.cache.Cache;
import play.data.validation.Error;
import play.data.validation.Validation;
Expand All @@ -19,6 +22,7 @@
import play.exceptions.TemplateNotFoundException;
import play.libs.Codec;
import play.mvc.Router.ActionDefinition;
import play.mvc.Scope.Flash;
import play.mvc.Scope.Session;
import play.templates.GroovyTemplate.ExecutableTemplate;

Expand Down Expand Up @@ -99,6 +103,52 @@ public static void _form(Map<?, ?> args, Closure body, PrintWriter out, Executab
out.println(JavaExtensions.toString(body));
out.print("</form>");
}

/**
* The field tag is a helper, based on the spirit of Don't Repeat Yourself.
* @param args tag attributes
* @param body tag inner body
* @param out the output writer
* @param template enclosing template
* @param fromLine template line number where the tag is defined
*/
public static void _field(Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
Map<String,Object> field = new HashMap<String,Object>();
String _arg = args.get("arg").toString();
field.put("name", _arg);
field.put("id", _arg.replace('.','_'));
field.put("flash", Flash.current().get(_arg));
field.put("flashArray", field.get("flash") != null && !field.get("flash").toString().isEmpty() ? field.get("flash").toString().split(",") : new String[0]);
field.put("error", Validation.error(_arg));
field.put("errorClass", field.get("error") != null ? "hasError" : "");
String[] pieces = _arg.split("\\.");
Object obj = body.getProperty(pieces[0]);
if(obj != null){
if(pieces.length > 1){
for(int i = 1; i < pieces.length; i++){
try{
Field f = obj.getClass().getField(pieces[i]);
if(i == (pieces.length-1)){
try{
Method getter = obj.getClass().getMethod("get"+JavaExtensions.capFirst(f.getName()));
field.put("value", getter.invoke(obj, new Object[0]));
}catch(NoSuchMethodException e){
field.put("value",f.get(obj).toString());
}
}else{
obj = f.get(obj);
}
}catch(Exception e){
// if there is a problem reading the field we dont set any value
}
}
}else{
field.put("value", obj);
}
}
body.setProperty("field", field);
body.call();
}

/**
* Generates a html link to a controller action
Expand Down
11 changes: 0 additions & 11 deletions framework/templates/tags/field.tag

This file was deleted.