Skip to content

Commit 5e17d56

Browse files
committed
add content template component model
1 parent 99d95ee commit 5e17d56

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package design.aem.models.v2.content;
2+
3+
import design.aem.components.ComponentProperties;
4+
import design.aem.models.ModelProxy;
5+
import design.aem.utils.components.ComponentsUtil;
6+
import org.apache.commons.jexl3.*;
7+
8+
import static design.aem.utils.components.ComponentsUtil.*;
9+
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
10+
11+
public class ContentTemplate extends ModelProxy {
12+
13+
protected ComponentProperties componentProperties = null;
14+
protected ComponentProperties requestComponentProperties = null;
15+
public ComponentProperties getComponentProperties() {
16+
return this.componentProperties;
17+
}
18+
public ComponentProperties getRequestComponentProperties() {
19+
return this.requestComponentProperties;
20+
}
21+
22+
public static final String REQUEST_COMPONENT_PROPERTIES = "design.aem.models.v2.content.ContentTemplate.componentProperties";
23+
public static final String FIELD_CUSTOM_TEMPLATE_JEXL = "customTemplateJEXL";
24+
25+
protected void ready() {
26+
/*
27+
Component Fields Helper
28+
29+
Structure:
30+
1 required - property name,
31+
2 required - default value,
32+
3 optional - name of component attribute to add value into
33+
4 optional - canonical name of class for handling multivalues, String or Tag
34+
*/
35+
setComponentFields(new Object[][]{
36+
{"templatePath", ""},
37+
{FIELD_CUSTOM_TEMPLATE_JEXL, ""},
38+
{"templateUse", "templatePath"},
39+
{FIELD_VARIANT, DEFAULT_VARIANT}
40+
});
41+
42+
componentProperties = ComponentsUtil.getComponentProperties(
43+
this,
44+
componentFields,
45+
DEFAULT_FIELDS_STYLE,
46+
DEFAULT_FIELDS_ACCESSIBILITY);
47+
48+
String customTemplate = componentProperties.get(FIELD_CUSTOM_TEMPLATE_JEXL,"");
49+
String customTemplateOutput = "";
50+
51+
requestComponentProperties = (ComponentProperties)getRequest().getAttribute(REQUEST_COMPONENT_PROPERTIES);
52+
53+
if (isNotEmpty(customTemplate) && requestComponentProperties != null) {
54+
55+
JexlEngine jexl = new JexlBuilder().create();
56+
JxltEngine jxlt = jexl.createJxltEngine();
57+
JexlContext jc = new MapContext(requestComponentProperties);
58+
59+
//try to evaluate default value expression
60+
try {
61+
JxltEngine.Expression expr = jxlt.createExpression(customTemplate);
62+
63+
//add componentProperties as a variable as well
64+
jc.set("componentProperties", requestComponentProperties);
65+
66+
customTemplateOutput = (String)expr.evaluate(jc);
67+
68+
} catch (JexlException jex) {
69+
LOGGER.warn("could not evaluate default value expression customTemplate={}, requestComponentProperties={}, jex.info={}", customTemplate, requestComponentProperties, jex.getInfo());
70+
} catch (Exception ex) {
71+
LOGGER.error("could not evaluate default value expression customTemplate={}, requestComponentProperties={}, error={}", customTemplate, requestComponentProperties, ex);
72+
}
73+
74+
}
75+
componentProperties.put("customTemplateJEXLOutput", customTemplateOutput);
76+
77+
}
78+
}

0 commit comments

Comments
 (0)