GrapesJsEmail.xml
4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?xml version="1.0" encoding="UTF-8"?>
<!--
This software is in the public domain under CC0 1.0 Universal plus a
Grant of Patent License.
To the extent possible under law, the author(s) have dedicated all
copyright and related and neighboring rights to this software to the
public domain worldwide. This software is distributed without any
warranty.
You should have received a copy of the CC0 Public Domain Dedication
along with this software (see the LICENSE.md file). If not, see
<http://creativecommons.org/publicdomain/zero/1.0/>.
-->
<screen xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://moqui.org/xsd/xml-screen-3.xsd">
<actions>
<entity-find-one entity-name="moqui.basic.email.EmailTemplate" value-field="emailTemplate" auto-field-map="[emailTemplateId:emailTemplateId]"/>
<if condition="!emailTemplate || !emailTemplate.htmlLocation || !emailTemplate.htmlPublishedVersionName">
<return error="true" message="Email Template not found"/>
</if>
<set field="dataPre" from="ec.resource.getLocationReference(emailTemplate.htmlLocation).getText(emailTemplate.htmlPublishedVersionName)"/>
<if condition="dataPre == null">
<return error="true" message="Email Template does not exist at ${emailTemplate.htmlLocation} with version ${emailTemplate.htmlPublishedVersionName}"/>
</if>
<set field="doc" from="org.jsoup.Jsoup.parse(dataPre)"/>
<script><![CDATA[
doc.outputSettings().escapeMode(org.jsoup.nodes.Entities.EscapeMode.xhtml).prettyPrint(false);
doc.traverse(new org.jsoup.select.NodeVisitor() {
public void head(org.jsoup.nodes.Node node, int depth) {
if (node instanceof org.jsoup.nodes.TextNode) {
def text = node.getWholeText();
if(text.trim().length() > 0) {
def newText = "";
def lines = text.split("\n");
for (line in lines) {
if (line.startsWith("<br>")) {
line = line.replaceFirst(/^<br>/, "");
}
if (line.endsWith("<br>")) {
newText += line;
} else {
newText += line + "<br>";
}
}
if (newText.startsWith("<br>")) {
newText = newText.replaceFirst(/^<br>/, "");
}
node = org.jsoup.nodes.TextNode.createFromEncoded(newText);
}
}
}
public void tail(org.jsoup.nodes.Node node, int depth) {
// No action needed on tail
}
});
]]></script>
<set field="dataPre" from="doc.html()"/>
<script><![CDATA[
String location = emailTemplate?.htmlLocation ?: "template.ftl";
freemarker.template.Template newTemplate;
Reader templateReader = null;
try {
templateReader = new StringReader(context.dataPre);
// Use the getFtlConfiguration method from ec.resource.templateRenderers.ftl
newTemplate = new freemarker.template.Template(location, templateReader, ec.resource.templateRenderers.ftl.getFtlConfiguration());
} catch (Exception e) {
throw new org.moqui.BaseArtifactException("Error while initializing template at " + location, e);
} finally {
if (templateReader != null) {
try { templateReader.close(); }
catch (Exception e) { logger.error("Error closing template reader", e); }
}
}
StringWriter sw = new StringWriter()
try {
newTemplate.createProcessingEnvironment(ec.contextStack, sw).process();
} catch (Exception e) { throw new org.moqui.BaseArtifactException("Error rendering template at " + location, e); }
context.renderedText = sw.toString();
]]></script>
</actions>
<widgets>
<render-mode><text type="html"><![CDATA[${renderedText}]]></text></render-mode>
</widgets>
</screen>