Execute code freemarker into a freemarker ftl - spring

I am trying to execute freemarker code within a freemarker ftl, I explain a little what I have:
We have a module with Spring that solves the FTL views and prints its content, even here everything works perfectly, but sometimes we will want to introduce more code in that view and we need to do it without having to deploy the module again, that is why we are entering Freemarker code in a String variable and passing that variable to the view through the model. But the problem appears here, I don't know how to manage that variable, the most I can do is paint it $ {myVar}, but the Freemakrer code appears as if it were a simple HTMl (that is, Freemarker does not execute it as such).
Is there a way to do a kind of include with that variable that has Freemarker code?

We do something alike with dymanic parts in a view. You can use interpret and <#var> to achieve that. Something like
<#assign varTempl = myVar?interpret >
<#varTempl />
The first line will parse your template, the second line will print it.

Related

org.thymeleaf.exceptions.TemplateProcessingException: Only variable expressions returning numbers or booleans are allowed in this context

I have been using thymeleaf th:onclick attribute to call javascript function with parameters as below
th:onclick="|myFunction('${parameter1}')|"
But with thymeleaf 3.1.10 this has been removed. and they are suggesting to use th:data attribute.
I however found workaround on as below and both of them are working perfectly.
th:attr="onclick=|myFunction('${parameter1}')|"
th:onclick="#{myFunction('${parameter1}')}">
Now i am not sure if these workarounds are correct way to do things and if yes which one is the better way.
The first will work like you want -- however, you are bypassing the the security restriction and now your pages are vulnerable to javascript injection (which is the original reason this change was made).
The second one just plain doesn't work. It doesn't expand out the variable ${parameter1}, instead just encoding it as a url like this:
onclick="myFunction?$%7Bparameter1%7D"
You really should be doing it as shown on the page.
th:data-parameter1="${parameter1}" onclick="myFunction(this.getAttribute('data-parameter1'));"

Go Template - calling another template with multiple parameters

I'm using an application that is getting me some data, and then renders a config file based on a given Go Template. You basically pass a template you've made as a parameter, and app does it's job with it. The template is getting bigger and bigger, so I wanted to wrap some common stuff into sub-templates (I mean, {{ define x }}). The problem I'm occuring is that the sub-template should be passed serveral parameters, which are not a part of my 'dot', and I can't really find a way to do this in Go.
The best answer I've found is to write some 'dict' function myself, and then use it inside the template, but that would mean I basically need to fork the whole application I'm using to render the template, do like 10-15 line changes, and then use this modified versions, which is a nonsense.
I'm wondering if there's any real solution for my problem without having to do some crazy forking and writing custom methods on application side?
Edit:
I've already checked Calling a template with several pipeline parameters before, although it's not answering my question, since I need a way to do this using only template file.

How do I JS escape a template in Golang

I have a template and I want to JS escape the output to inject into another template. I could use template.JSEscape to do this after I've executed the template but it would seem more efficient to do it in one step, within a template.
I'd like to do something like...
var x = "{{js}}...some other template...{{end}}"
Is this possible?

Validating Xml Chef template

Google hasn't helped me with this:
We just started using Chef deployment. (And I'm embarrassingly new to it)
I have a web.config template that will have variables set by a data bag.
Using some free online tools, I was able to find and fix syntax problems in my data bag. JSONLint
Is there a way to check the template itself?
Online Xml validation tools like this one fail once a Ruby tag <% is found... and this makes sense because it's not valid Xml. Is there a tool/way to validate the Xml in a template so that the Ruby tags are valid?
All I want to know is if syntactically speaking, the file is valid. Logic errors would be dealt with another way... if that makes sense.
You could substitute the values that are supposed to be set by your <%=...%> tags and validate that since your final .xml file won't contain any of those tags if you did your template correct.

How do I do substitutions for localised strings in freemarker

I'm using spring and freemarker and have the basics working.
I've got a properties file like
help.text=For further information please see the help page.
I'm currently outputting localised messages using
${rc.getMessage("help.text")}
However I'm having trouble figuring out how I can pass in my substitution variables. Can you help?
Cheers,
Peter
If I read the Spring API documentation about RequestContext (your rc?) correctly, then
${rc.getMessage("help.txt", ["yourHelpUrl"])}
might work, because getMessage can receive an additional List argument with message args, which you can supply via a FreeMarker sequence.
<#import "/spring.ftl" as spring/>
<#assign args = ["yourHelpUrl"]>
<#spring.messageArgs "help.txt" args/>
I always do the substitution variables in my Java code somewhere and then dump the fully localized text into a Map where it's accessed by Freemarker like this:
${localizedValues["help.txt"]}
If you are correctly configuring freemarker in the spring MVC context then the right way to do it is:
Import the spring macros in the template
Use the mensaje macro
<#import "/spring.ftl" as spring />
<#spring.messageText "code", "Default message"/>
See the documentation: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/view.html#view-velocity

Resources