I have registered a handlebars helper by the name of "t" that will translate a string. I have a Handlebars template which uses the value of a variable that will be a word:
{{{word}}}
I would like to translate the value of that variable using my helper, but cannot do
{{t "{{{word}}}" }} or {{t {{{word}}} }}
I am inexperienced with Handlebars and am wondering - What is the simplest way to achieve this?
{{t word}}
Simply pass the reference into your helper without brackets.
You only need brackets for outputting the value of the reference to the page:
Handlebars HTML-escapes values returned by a {{expression}}. If you
don't want Handlebars to escape a value, use the "triple-stash", {{{.
UPDATE:
If you need to perform some kind of operation (such as escaping HTML) on the value of word, you can make a second helper and pass that into your first helper using ( parentheses ):
{{t (myOtherHelper word)}}
Related
I am using the html/template functionality to assemble a page and on of the variables I'm supplying to the template is URI in the form "/some/path/etc" which is used as a parameter to a JS function called in a onClick="..".
No matter what, the string used in this configuration will be escaped with backslashes : "\/some\/path\/etc"
As you can see in the playground example below, I tried all the .HTML(), .JS() etc. functions but nothing seems to stop the escaping.
See Go Playground example here: https://play.golang.org/p/2gdghTpQHKP
How can I get this URI "as is" into the template result?
Thanks to mkopriva for his comment.
As far as I could see there is no way (as mkopriva mentioned) to handling a HRML attribute value fragment in a Go HTML template.
So the options are:
Leave it as is (it seems that at least in my use case the URI even works in the further processing with the escaped forward slashes)
first concatenate the complete attribute, so that the "HTML Attribute" way will accept it
Write a construction function that takes parts and assembles the final attribute value inside the template execution
Hardcode the value in some form
I'd like to use a variable inside a YAML literal block scalar.
Here's what I'd like to do:
markup: |
<title>
{{ title }}
</title>
Can that be done somehow?
I appreciate that this example would be trivial to execute without using a literal block scalar, but my actual use case inside a Foundation 6 stack would contain more markup and more variables than what I'm showing here.
There is no such thing as a variable inside a literal block scalar.
First of all there are no variables in YAML (the word variable, occurs only once in the YAML specification, in an example document, nr. 2.28).
And second, this is called literal for a reason. No interpretation is done of any of the characters.
Of course it is possible that some program that loads your document does something with the text between curly braces ({}). E.g interprets it as a jinja2 template. But without knowing what such a program does or expects, it is equally valid to expect something like that for the information between angle brackets (<>).
Therefore within YAML there is no way to use variables, neither inside of literal block-style scalars, nor outside them.
As for the templating: I have worked with program that generated YAML from a template and applied templates on the loaded string scalars (by recursively walking the tree). Your example could be either.
If we have value 'cats', I want the substring 'at' (need to delete first and last letters). How can we accomplish that in a Nunjucks template?
You can do this using the JavaScript slice method, like so:
{{cats.slice(1, -1)}}
In my data model myVar contains <b>hello</b> and when I bring it like this ${myVar} the output I get is literally <b>hello</b> rather than hello.
Any idea how to correct this?
Certainly you have HTML escaping on, so try <#noescape>${myvar}</#noescape>.
Update: Since 2.3.24 a new kind of auto-escaping was introduced, which doesn't use #escape, and hence nor #noescape. When that's used, write ${myvar?no_esc} to avoid escaping. Or, you can put the value into the data-model already as a TemplateHTMLOutputModel (created with HTMLOutputFormat.fromMarkup(myString)), and then FreeMarker will know without ?no_esc that it need not be escaped.
I have a .tpl file which has %% variables in it.
Example:
%%GLOBAL_Error%%
Is this a smarty template engine file?
I want to check if this variable is not empty, but I can't do this with regular smarty syntax.
{if isset($GLOBAL_Error) }
How can i do this?
I got response that this is Twig template engine :S
By default, smarty use { and } as delimiters. So, you can write code:
{if $GLOBAL_Error}{/if}
But delimiters could be changed to %% and code must be in such form:
%%if $GLOBAL_Error%%%%/if%%
See also: http://www.smarty.net/docsv2/en/language.escaping.tpl
It might be, but it might not be. Smarty tag delimiters are configurable, so it's possible the developer set them to %% and %%. GLOBAL_Error may not be a variable, but a function call with no parameters. In that case, you'd need something like:
%%if (%%GLOBAL_Error%%) %%
That's some pretty ugly syntax though. It could be anything, maybe a string token for some search/replace before or after the template is compiled. If you're already using Smarty with standard delimiters, then it's unlikely that %%GLOBAL_Error%% is meant to be parsed by Smarty - it might even be a mistake or relic of old code. The only way to know for sure is to ask the author or just look at the codebase.