How to escape special characters in Groovy's SimpleTemplateEngine? - groovy

By default, groovy's SimpleTemplateEngine uses <% %> as the scriptlet container. How do I escape these characters if I want to print out <% %>?

Looks like it might be a bug
http://jira.codehaus.org/browse/GROOVY-399

The book I've red (which might be outdated) says that you need to have variable that contains <% and %> and then you need to output those variables.

Related

Hygen template generator converting quotes to '

I am using Hygen to populate template files and want to print an array of strings eg.
[`'${item}'`]
but the single quotes are being converted to '
All other special characters are unaffected but single or doublequotes get converted so I'm not sure what's going on.
Turns out
<%= %>
tags in the template file escapes some characters, whereas...
<%- %>
does not!

replace variable value on ruby template via puppet

I am looking the way to have the result of this ruby template file:
ServerName 1.server.foo
knowing that if I run
$ facter -p fqdn
1.server.foo.internet.com
I would probably play with <%= #fqdn %> and .gsub?
server-id: <%= #fqdn %>.gsub(/.internet.com/, '')
The entire expression needs to be in the <%= %> tag, so try
server-id: <%= #fqdn.regsubst(/.internet.com/, '') %>
The template syntax is documented at https://puppet.com/docs/puppet/5.5/lang_template_erb.html with examples of expressions used in <%= %> tags.
I'd also note that ERB templates have been replaced by native Embedded Puppet EPP templates, so it may be better to convert now.
using EPP with regsubst it works!
server-id: <%= $facts[fqdn].regsubst(/.internet.com/, '') %>

NodeJS Printing object json to html

I am trying to print a NodeJS object as simple json in EJS template file.
I am defining the object in NodeJs and trying to print it in EJS file like this:
<%= JSON.stringify(objName) %>
This is encoding double quotes (") is some format and giving me output like this:
{"_id":"5c3587b78ff1928c5124bf6d","name":"Sourabh Bajaj","role":10,"roleName":"InstituteAdmin","mobileNumber":"+919166677890","email":"sorbhb#gmail.com","mobileVerified":true,"emailVerified":true,"instituteId":"5c3586308ff1928c5124bf24","passwordResetKey":"","success":true,"errorCode":200};
If I don't stringify it, it give me [Object object] as output.
Found the answer. EJS template somehow encodes double quotes when you use <%= %>.
If you don't want that, use <%- %> tags instead.

How to include space and new line in regular expression?

I have an HTML file which contains the below code snippet.
<div class="col-sm-2 col-sm-offset-1">
<div class="countBox success" id="success">
<h2>467</h2>
Passed Tests
<span class="glyphicon glyphicon-eye-open"></span>
</div>
</div>
I have a regular expression (.*)</h2>\r\nPassed to get the value 467. It is worked till yesterday. But, it is not working now. I have tried by replacing single slash by double slash to new line and row. Used "\s+" to cover whitespace. All failed in error. Could anyone please guide me on how to get the value as 467 by using regular expression for the above code snippet?
It is better to catch <h2>(\d+)</h2> to ensure only a h2 header with a number inside. By the way, \r\n is only one convention (in windows) to represent end of line, but in unix it is only \n so to be more platform independent, you can do \r?\n (marking the \r as optional) and you have to get on the whitespace in front of Passed., so a good (but not probably the best) regexp would be:
<h2>(\d+)<\/h2>\r?\n\s*Passed
See demo.

puppet resource with duplicate attributes fails instead of overwriting

If I have puppet resources with duplicate attributes, why does it fail instead of overwriting?
file { '/tmp/the_inlinetemplate.txt' :
content => inline_template("What do you get if multiply 6 by 9 ? <%= 6 * 7 %> . \n")
content => inline_template("My address <%= ipaddress %> \n")
}
You just cannot define the same parameter twice. From documentation:
A resource declaration is an expression that describes the desired state for a resource and tells Puppet to add it to the catalog
In your example it is impossible to decide which file content is desired:
"What do you get if multiply 6 by 9 ? <%= 6 * 7 %> . \n"
or
"My address <%= ipaddress %> \n"
I thought about several different answers, but the bottom line is simply that that's how Puppet chooses to deal with the uncertainty in what you are asking it to do. Rather than adopting an arbitrary rule to deal with the situation (why should it choose the last-appearing property binding instead of the first, or a random one?) it insists that you be clear and unambiguous up front.
This provides a degree of protection against performing unintended actions. Although the content of file /tmp/the_inlinetemplate.txt in particular probably doesn't matter in the grand scheme of things, Puppet applies the same principle to all resource declarations, many of which are rather more consequential.

Resources