]> juplo.de Git - juplo-dialect/commitdiff
juplo:variables accepts expressions as arguments
authorKai Moritz <kai@juplo.de>
Fri, 8 Jul 2016 16:34:55 +0000 (18:34 +0200)
committerKai Moritz <kai@juplo.de>
Mon, 18 Jul 2016 15:12:43 +0000 (17:12 +0200)
juplo.variables now also accepts an expression as argument.
If the parameter can not be parsed as an expression, it is handeld as before
(i.e. parsed as JSON, if possible, tried to be reolved, if not). If the
expression evaluates to an empty string, the parameter is ignored.

src/main/java/de/juplo/thymeleaf/ImportVariablesAttrProcessor.java

index e864ab17bdb00e6628d7904ccf2add16ba4eaa68..aac37a6cd6f25dfb95d7fa12f291d5a5abfa6a84 100644 (file)
@@ -22,9 +22,13 @@ import org.thymeleaf.context.IContext;
 import org.thymeleaf.context.VariablesMap;
 import org.thymeleaf.dom.Element;
 import org.thymeleaf.dom.Node;
+import org.thymeleaf.exceptions.TemplateProcessingException;
 import org.thymeleaf.processor.ProcessorResult;
 import org.thymeleaf.processor.attr.AbstractAttrProcessor;
 import org.thymeleaf.resourceresolver.IResourceResolver;
+import org.thymeleaf.standard.expression.IStandardExpression;
+import org.thymeleaf.standard.expression.IStandardExpressionParser;
+import org.thymeleaf.standard.expression.StandardExpressions;
 import org.thymeleaf.templateresolver.ITemplateResolver;
 import org.thymeleaf.templateresolver.TemplateResolution;
 
@@ -65,7 +69,25 @@ public class ImportVariablesAttrProcessor extends AbstractAttrProcessor
       )
   {
     Configuration config = arguments.getConfiguration();
+
+    Configuration configuration = arguments.getConfiguration();
+
     String parameter = element.getAttributeValue(name);
+    try
+    {
+      IStandardExpressionParser parser =
+          StandardExpressions.getExpressionParser(configuration);
+      IStandardExpression expression =
+          parser.parseExpression(configuration, arguments, parameter);
+      parameter = (String)expression.execute(configuration, arguments);
+    }
+    catch (TemplateProcessingException e) { }
+
+    if (parameter != null && !parameter.trim().isEmpty())
+    {
+      LOG.info("ignoring empty parameter");
+      return ProcessorResult.OK;
+    }
 
     Iterator<Entry<String, Object>> it = null;