How to process freemarker template from variable inside template? - freemarker

Simple example:
<div>${item.content}</div>
But inside of item.content variable may exist another template code, such as
<#if otherItem.image??><div class ...>...</#if>
Is that possible to tell freemarker compile this code inside variable?

?interpret creates a directive from a string. Note that it doesn't call the directive. Expressions that evaluate to directive can be called with <#someExpression />. Putting these together you get:
<div><#item.content?interpret /></div>

Related

Execute code freemarker into a freemarker ftl

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.

What's the easiest way to define a custom HTML-stringification?

Is there a way for me to define that when I just output a value in Go html templates the way the formatting is done instead of it printing a string representation without having to call a function to explicitcly convert it?
For example, let's say I have a type Person and I have a template with just {{.}} I want it to automatically create a link to that person's page but if I use the same template and passed a value of a different type some other HTML will be generated.
What I don't want to do is having to write something like {{.HTML}} or {{. | html}}. I'm already aware that these are possible but my question is specifically about how to avoid those.
I've played around with the thought of Person.String() having return the HTML code somehow without it being escaped but besides not getting that to work it also seems like an ugly solution.
Another solution I've thought about is to just pass everything as HTML into the template but then I couldn't access the attributes anymore (like {{.name}} to output just the name) and I'd also have to convert everything into HTML just in case it's used in the template.
Create a method that returns an template.HTML type. i.e.:
func (p *Person) HTML() template.HTML {
return fmt.Sprintf(`%s`, p.id, template.HTMLEscapeString(p.name))
}
Then in your template:
{{ .HTML }}

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?

Equivalent to c:set in thymeleaf [duplicate]

I am new to Thymeleaf and converting my Web page from JSP to Thymeleaf. I have a strut tag like this:
<c:set var="someVariable" value="${someValue}"/>
That variable can be used anywhere in JSP. Is there any such alternatives for this in Thymeleaf?
You can use local variables.
Declare an HTML element with a th:with attribute. For example
<div th:with="someVariable=${someValue}">
The documentation states
When th:with is processed, that [someVariable] variable is created as a
local variable and added to the variables map coming from the context,
so that it is as available for evaluation as any other variables
declared in the context from the beginning, but only within the bounds
of the containing tag.
Just a note, if you wish to assign more than one variable, separate them with a comma :
<div th:with="someVariable=${someValue},anotherVariable=${anotherValue}">
See the third example : Local Variable section of Thymeleaf documentation
declare with th:with="varName=${'str'}
ref with in src th:src="#{${varName}}"
in more detail:
<head th:with="component =${'/static/component'}, bizJs = ${'/static/js/biz'}">
<span th:text="${component}"></span>
<script th:src="#{(${component})}"></script>
<script th:src="#{${bizJs} + '/static/js'}"></script>
</head>

pyrocms files plugin variables

I want to pull a url segment variable into the pyrocms file plugin call. it would look like
{{files:listing folder="[segment(2)]"}}
or something of the sort. What is the trick for embedding
{{url:segments..}}
inside
{{files:listing folder="…}}
I am trying to setup this up for a conditional query for a photo gallery
If you take a look at the PyroCMS Tags documentation you will see this clearly documented in the "Tag Attributes" section.
You may also use the output from other tags as attribute values in your tags. For example if you wanted the url segment to default to the slug of the currently viewed page you could do this:
{{ url:segments segment="1" default=page:slug }}
Here is an example showing the proper use of quotes and braces when the tag used as the attribute value has an attribute itself.
{{ url:segments segment="1" default={foo:bar value="baz"} }}
Tip: Omit quotes and braces when using tags as attribute values. The only exception is when the tag you are using as the attribute value has its own attributes.
So you can do that easily with:
{{ files:listing folder={url:segments segment="2"} }}
Basically you don't need to pretend it's a string if it's not. You can just send the foo:bar arguments through, but it if has attributes you can "group" the attributes with the call via a single { and }.
Simple right? :)

Resources