sharing variables across freemarker files - freemarker

I have 2 ftl files ( helloworld2.ftl and helloworld4.ftl ). I am trying to understand the variables sharing between ftl files. In helloworld4.ftl i have declared SharedVar and i am trying to access that varible in included file ie helloworld2.ftl. Am able to access. But at the end one set of extra error message im getting. why is it so?
I have used global instead of assign still same behaviour.
Contents of them follows
helloworld2.ftl :
<html>
<head>
<title>new file
</head>
<body>
<h1>${sharedVar}</h1>
</body>
</html>
helloworld4.ftl
<#assign sharedVar="sharedVar">
**********************
<#include "t2">
**********************
${sharedVar}*
ftl loading part,
Map<String, String> m = new HashMap<>();
m.put("t2", readFile("E://workspace//example//src//main//resources//templates//helloworld2.ftl"));
m.put("t4", readFile("E://workspace//example//src//main//resources//templates//helloworld4.ftl"));
for (Map.Entry<String, String> n : m.entrySet()) {
stringLoader.putTemplate(n.getKey(), n.getValue());
}
Output follows,
**********************
<html>
<head>
<title>new file
</head>
<body>
<h1>sharedVar</h1>
</body>
</html>
**********************
sharedVar*Aug 07, 2022 3:08:56 PM freemarker.log._JULLoggerFactory$JULLogger error
SEVERE: Error executing FreeMarker template
FreeMarker template error:
The following has evaluated to null or missing:
==> sharedVar [in template "t2" at line 7, column 17]

Related

How to inject a JS script in Qute Template Engine

I am using Quarkus with the qute template engine. I need to inject some dynamic js script to load on the HTML page. But qute convert js like this:
Template file hello.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>redirect by script</title>
</head>
<body>
<p>Hi {name}!</p>
<script>
{script}
</script>
</body>
</html>
Controller
#RequestScoped
public class Resource {
#Inject
#Location("hello.html")
Template hello;
#Route(path = "/s/:name", methods = HttpMethod.GET)
public Uni<String> rScript(RoutingContext rc) {
String s = "console.log('Hi from script');";
return Uni.createFrom().completionStage(() -> hello
.data("name", rc.request().getParam("name"))
.data("script", s)
.renderAsync());
}
}
The template render file like below and script will not run on browser:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>redirect by script</title>
</head>
<body>
<p>NOT_FOUND!</p>
<script>
console.log('test redirect by script')
</script>
</body>
</html>
How can I pass script data to qute template file?
You will want to turn off Character Escapes:
Either use raw:
<script>
{script.raw}
</script>
</body>
</html>
Or return a RawString:
#Route(path = "/s/:name", methods = HttpMethod.GET)
public Uni<RawString> rScript(RoutingContext rc) {
String s = "console.log('Hi from script');";
return Uni.createFrom().completionStage(() -> hello
.data("name", rc.request().getParam("name"))
.data("script", new RawString(s))
.renderAsync());
}
}

exist-db cardinality for parameter

I am new in exist-db and XQuery.
In exist-db I have this site map: The catalog "registranten" contains the catalog "data" (with xml-files) and the two files "regBasic.xql" and "regSearch.xql".
I am trying to search in the xml-files using the script regSearch.xql:
xquery version "3.0";
declare option exist:serialize "method=xhtml media-type=text/html";
declare variable $pageTitle := "Resultat";
declare variable $searchphrase := request:get-parameter("searchphrase", ());
<html>
<head>
<meta HTTP-EQUIV="Content-Type" content="text/html; charset=UTF-8"/>
<title>{$pageTitle}</title>
</head>
<body>
<h1>{$pageTitle}</h1>
<p>Søgestreng eller søgeord: "{$searchphrase}"</p>
<ul>
{
for $p in collection("/db/registranten/data")//grundtvig/indholdsregest/p[ft:query(., $searchphrase)]
return
<li>
from: {string(root($p)/grundtvig/filnavn)}<br/>
<i>$p</i>
</li>
}
</ul>
</body>
</html>
When I evaluate the script I get this error: "exerr:ERROR XPTY0004: The actual cardinality for parameter 2 does not match the cardinality declared in the function's signature: ft:query($nodes as node(), $query as item()) node(). Expected cardinality: exactly one, got 0. [at line 17, column 100, source: /db/apps/registranten/regSearch.xql]"
What does that mean and what is wrong with the script?
Your $searchphrase variable is an empty sequence, which likely suggests that you did not set the request parameter which you are asking for with request:get-parameter().

Get Raw Html From the controller Spring thymeleaf

I have a controller that create model attribute and passes to the view "partial.html" to generate output
partial.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Home page</title>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<p>
<span th:text="'Today is: ' + ${message}"></span>
</p>
</body>
</html>
and inside a controller method
model.addAttribute("message", search);
How to do I get Htlm Output to a string inside controller method?
like this
String htmlOutput="from partial.html";
Let's say you have a HTML file with two variable name and todayDate.
You want to process it and want to store it in a string / database / AWS S3.
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
</head>
<body>
<p>Hello</p>
<p th:text="${name}"></p>
<p th:text="${todayDate}"></p>
</body>
</html>
Your HTML file location is src/main/resources/templates/home.html
By using the below function you can get the final processed HTML as:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<p>Hello</p>
<p>Manoj</p>
<p>30 November 2019</p>
</body>
</html>
import org.thymeleaf.context.Context;
#GetMapping("/")
public void process() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("templates/");
templateResolver.setCacheable(false);
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML");
// https://github.com/thymeleaf/thymeleaf/issues/606
templateResolver.setForceTemplateMode(true);
templateEngine.setTemplateResolver(templateResolver);
Context ctx = new Context();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy");
Calendar cal = Calendar.getInstance();
ctx.setVariable("todayDate", dateFormat.format(cal.getTime()));
ctx.setVariable("name", "Manoj");
final String result = templateEngine.process("home", ctx);
System.out.println("result:" + result);
}
If you're using the usual Spring MVC approach, as Joanna says you're doing things in the wrong order. The Controller creates the model and specifies the view, and then after that the view is rendered by the Thymeleaf template that uses the model.
If, on the other hand, you're trying to render Thymeleaf templates yourself (rather than sending them to the user's browser directly, maybe for use in HTML email or to store prerendered pages in a database or something), then you'd need to create your own Thymeleaf Template Engine to use. Refer to the "Creating and configuring the Template Engine" section of the documentation for details. You can create your own Engine, and then use its process method to get the result of the template to put into a variable for further use.
You may looking for this, getting directly the result HTML, just ignore the email part of the post.
Then, you can create this:
final Context ctx = new Context(locale);
ctx.setVariable("name", recipientName);
ctx.setVariable("subscriptionDate", new Date());
ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music"));
ctx.setVariable("imageResourceName", imageResourceName);
// so that we can reference it from HTML
final String htmlContent = this.templateEngine.process("html/email-inlineimage.html", ctx);
So you have the htmlContext of rendered thymeleaf template (with vars)
Once the control goes out to view processor (JSP/Thymeleaf etc), it will not be coming back to controller. You will be able to get the raw html response in a customFilter, but not in the Controller.

Add inline images to spring email

How can I add, using localhost or Server, an image to be included in an e-mail that is sent via Spring with Thymeleaf?
This is my controllerMail.java:
final Map<String, String> inlineResources = new HashMap<String, String>();
Set<String> folderPath = new TreeSet<>();
for (File file : files) {
certificateFile = file.getCertificateFile();
String img = certificateFile.toString();
inlineResources.put("file", img);
}
inlineResources.put("img1", "/template/email/img/myImg.png");
And this my html mail:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title th:remove="all">Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body style="font-family:Trebuchet MS;">
<p>TEXT EMAIL</p>
<img style="max-width: 100%;" th:src="'cid:img1'" />
<img style="max-width: 100%;" th:src="'cid:file'" />
</body>
</html>
certificateFile return this path: /srv/dev/contents/jpgCache/certificate/10000/certificateName.jpg
So my mail.html is located on my project in src/main/resources in /template/email. In this case img1 is correct find on email (it is located in the same path /template/email/img) but file return this log error:
Invalid resource: /srv/dev/contents/jpgCache/certificate/10000/certificateName.jpg
Failed messages: javax.mail.MessagingException: IOException while sending message;
nested exception is:
java.io.FileNotFoundException: class path resource [/srv/dev/contents/jpgCache/certificate/10000/certificateName.jpg] cannot be opened because it does not exist
How i can fix this problem?
While the attachment of this file to email it works properly.
The solution for this problem is to use "file:" before the "pathImg":
String pathImg = certificateFile.toString().replace('\\', '/'); inlineResources.put("img", "file:"+pathImg);

Read/write to Parse Core db from Google Apps Script

I'm just starting to use Parse Core (as Google'e ScriptDB is being decommissioned soon) and am having some trouble.
So I'm able to get Parse Core db to read/write using just a standard HTML page as shown below:
<!doctype html>
<head>
<meta charset="utf-8">
<title>My Parse App</title>
<meta name="description" content="My Parse App">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/styles.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.2.18.min.js"></script>
</head>
<body>
<div id="main">
<h1>You're ready to use Parse!</h1>
<p>Read the documentation and start building your JavaScript app:</p>
<ul>
<li>Parse JavaScript Guide</li>
<li>Parse JavaScript API Documentation</li>
</ul>
<div style="display:none" class="error">
Looks like there was a problem saving the test object. Make sure you've set your application ID and javascript key correctly in the call to <code>Parse.initialize</code> in this file.
</div>
<div style="display:none" class="success">
<p>We've also just created your first object using the following code:</p>
<code>
var TestObject = Parse.Object.extend("TestObject");<br/>
var testObject = new TestObject();<br/>
testObject.save({foo: "bar"});
</code>
</div>
</div>
<script type="text/javascript">
Parse.initialize("PyMFUxyBxR8IDgndjZ378CeEXH2c6WLK1wK2JHYX", "IgiMfiuy3LFjzH0ehmyf5Rkti8AmVtwcGqc6nttN");
var TestObject = Parse.Object.extend("TestObject");
var testObject = new TestObject();
testObject.save({foo: "bar"}, {
success: function(object) {
$(".success").show();
},
error: function(model, error) {
$(".error").show();
}
});
</script>
</body>
</html>
However, when I try to serve that up using the HtmlService shown below, I get no response from Parse. Parse Core.html basically has all of the code I have above ( only thing I changed was to remove the css calls).
function doGet() {
var htmlPage = HtmlService.createTemplateFromFile('Parse Core.html')
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.NATIVE)
.setTitle('Parse Core Test');
return htmlPage;
}
Link to ParseDb Library for Apps Script
Here is the key to add the library: MxhsVzdWH6ZQMWWeAA9tObPxhMjh3Sh48
Install that library and it allows you to use most of the same methods that were used by ScriptDb. As far as saving and querying go they almost identical. Make sure to read the Library's notes, how to add the applicationId and restApiKey. It is a little different that you can silo data by classes which must be defined in the call to Parse.
Bruce here is leading the way on database connection for Apps Script, he has plenty of documentation on using Parse.com, and also his own DbConncection Drive that would allow you to use a number of back-end systems.
Excel Liberation - Bruce's Site.

Resources