示例#1
0
 public static void _set(
     Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
   // Simple case : #{set title:'Yop' /}
   for (Map.Entry<?, ?> entry : args.entrySet()) {
     Object key = entry.getKey();
     if (!key.toString().equals("arg")) {
       BaseTemplate.layoutData
           .get()
           .put(
               key,
               (entry.getValue() != null && entry.getValue() instanceof String)
                   ? __safe(template.template, entry.getValue())
                   : entry.getValue());
       return;
     }
   }
   // Body case
   Object name = args.get("arg");
   if (name != null && body != null) {
     Object oldOut = body.getProperty("out");
     StringWriter sw = new StringWriter();
     body.setProperty("out", new PrintWriter(sw));
     body.call();
     BaseTemplate.layoutData.get().put(name, sw.toString());
     body.setProperty("out", oldOut);
   }
 }
示例#2
0
 /**
  * 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 && !StringUtils.isEmpty(field.get("flash").toString())
           ? 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) {
       try {
         String path = _arg.substring(_arg.indexOf(".") + 1);
         Object value = PropertyUtils.getProperty(obj, path);
         field.put("value", value);
       } 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();
 }