How do I JS escape a template in Golang - go

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?

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 }}

Is there something similar to Twig filters that I can use in Laravel Blade

Twig has this awesome feature called Twig Filters which allow you to change the variables that you send to your view, from within the view without messing with the data model.
{ variable_name | filter_name }
This makes it super-duper readable and clean. Filters can be lowercase, encoding, raw text or you can build your own.
Question is simple: I really miss this kind of functionality, what's the best way to implement something like this in Laravel using Blade?
as i know Blade don't offer this functionality of filters, but u can create a helper file that can has many functions helper and every function has her logic.
in this link u can find how to create a helper file with functions and call them inside ur blade file tutorial how to create helper function

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 to pass multiple parameters into Label Template?

Is there a way to pass multiple parameters to Label Template function? It doesn't seem to work out of the box.
You should be able to use multiple fields. For example:
{{prop1}} - {{prop2}}
in the label template should work. Not sure about your custom function though. Looking at the docs here: https://github.com/kgiszewski/ArchetypeManual/blob/master/02%20-%20Configuration.md it looks like you can pass multiple arguments maybe?

Resources