NodeJS Printing object json to html - node.js

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.

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!

Does heist support substituting (strings / JSON) into an arbitrary location within a template?

Regarding heist, I've got a template such as:
<script>
var json = ???;
</script>
<h1>Example</h1>
Is there a way to substitute the ??? string with another string?
I think the following function might be the solution https://hackage.haskell.org/package/heist-1.0.1.0/docs/Heist-Splices-Json.html#v:bindJson but I have difficulty understanding that function and or what markup to use in the template.
No. You cannot substitute anything inside a <script> tag because the text inside a script tag is not treated as HTML. It's treated as plain text. If this was not done, you wouldn't be able to write JS code like if ( x < 42 ) because the less than would get treated as the beginning of a tag.

groovy rename node issue using xmlSlurper

i have a xml like below where i just need to rename the node name to another.
<a x=1>
<b>c</b>
</a>
and i want to change it to
<p:a x=1>
<b>c</b>
</p:a>
i need to do it using xmlSlurper so how do i do it? how can i do the node rename. Does it need to rewrite the whole xml into another document etc? or can i do it within the document?
def xmlDoc = new XmlSlurper(false,false).parse('my.xml')
First you need to fix your XML. The value for attribute x needs quotes:
<a x="1">
<b>c</b>
</a>
Then to rename the root node:
xmlDoc.replaceNode {
'p:a'(it.children())
}
XmlSlurper reads XML into an object structure. Once you read it in, you can do whatever you want with it, but XmlSlurper has nothing to do with that.
Use MarkupBuilder to write out XML from that read-in object structure.

How do I escape an apostrophe in my variable name within ejs

I have an ejs file in my sails application that looks like this
<a href='/<%= viewname %>?where={"name":"<%= profile.pname %>"}'>
This works for most of the names with the exception of one that has an apostrophe in it. Essentially EJS parses the apostrophe as a sing quote which closes the href and makes the name I'm passing incorrect
http://localhost:1337/myviewnamwe?where={"name":"tom
where it should be
http://localhost:1337/myviewnamwe?where={"name":"tom's diner}
I would use URL encoding for the critical part: {"name":"<%= profile.pname %>"}
More info:
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/uri/rdoc/URI/Escape.html
http://www.w3schools.com/tags/ref_urlencode.asp

How to escape special characters in Groovy's SimpleTemplateEngine?

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.

Resources