How to use HTML in grails messages.properties for mail sending - html

In grails, I use a GSP template to render an HTML email sent out with the mail plug-in. This works fine, however the GSP template uses a param which is in turn retrieved from my messages.properties file. Now I want to use HTML, e.g. <br/> inside the messages.properties, but it in the mail it appears as text and the tag is not interpreted.
I already tried to use .decodeHTML() on the parameter in the GSP, but it didn't work.
Where do I have to encode / decode or is it possible at all to use HTML tags inside messages.properties?
<%# page contentType="text/html"%>
<html>
<body>
${variableWithHTMLTextFromMessagesProperties}
</body>
</html>

Can you not do the localisation in the GSP using the message tag, similar to the following? Controller -
sendMail {
to "my#email.com"
subject "cheese"
html g.render(template:"myTemplate")
}
And then in your _myTemplate.gsp -
<%# page contentType="text/html" %>
<html><head></head>
<body>
<p>test: <g:message code="a.test"/></p>
</body>
</html>
And then in messages.properties -
a.test=this <br/><br/> is a test
This way works fine for me (Grails 1.3.1, mail 0.9), I get 2 line breaks in the html email received. Any reason you can't do it this way?

Found the solution here. Easiest way is just to use <%=variableWithHTMLTextFromMessagesProperties%> instead of ${variableWithHTMLTextFromMessagesProperties}. This stops the HTML escaping.

I made my own solution with a custom taglib.
def htmlMessage = { attrs, body ->
out << g.message(attrs, body).decodeHTML()
}
Then to define a message, it has to be with escaped html:
my.html.message={0,choice,0#There is no elements|1#There is <strong>1</strong> element|1<There are <strong>{0}</strong> elements}
For html just:
<g:htmlMessage code="my.html.message" args="[qElements]" />
The result is an i18n html generated with number in strong font. Like this:
"There are 9 elements"

From the docs:
<g:encodeAs codec="HTML">
Profile for user ${user.name} is:
<g:render template="/fragments/showprofile"/>
</g:encodeAs>

Related

How to use/display string with html tags in typescript

var bannerText = stringFormat("This is my {0}test content{1} here", "<b>", "</b>");
<div>
<p> {para1}</p>
<p> {bannerText}</p>
</div>
html tags gets printed instead of applying them as text
Observed output
This is my <b>test content</b> here
Needed output
This is my test content here
<div [innerHTML]="bannerText"></div>
However, keep in mind Angular's docs about security, which says Angular recognizes the value as unsafe and automatically sanitizes it, which removes the <script> tag but keeps safe content such as the <b> element..
check the below code it might help you.use innerHtml
document.getElementById("name").innerHTML="This is <u>my test</u> sample"
<div id="name"></div>
.
Observed output This is my <u>test content</u> here
Whatever method you are using is escaping the html e.g. < / > etc.
Fix
You need some version of set HTML.
e.g. React : dangerouslySetInnerHTML, Pure JS: Use innerHTML
Warning
Be careful, setting html exposes your app to XSS : https://en.wikipedia.org/wiki/Cross-site_scripting

How can you load html text from pure html?

Is there a way to include the content of one HTTP request (containing either text/html or text/plain) in another HTML file? Of course this can be done via AJAX or on the server side, but I'm interested in a pure browser HTML way. Perhaps using a <link> tag, or some HTML5 method I'm not familiar with?
For example:
index.html:
<!DOCTYPE html>
<html>
<head></head>
<body>
<p>This is my text loaded from the original document</p>
<p>This is text I want to load from another file: <span id="other"><!-- link other resource here --></p></span>
</body>
</html>
otherResource.html:
<p>This is from another resource</p>
You can try to use an iframe.
<iframe src="page.html" width="300" height="300"></iframe>
If you are thinking about including text from another page, such as a footer or header, and just trying to find a way to include that, you might try a template engine.
Template Information: http://www.creativebloq.com/web-design/templating-engines-9134396
For example with EmbedJS from the link above:
new EJS({ url: "template.ejs" }).render({ name: "Jack" });
would allow you to include a template in a file called template.ejs.
Another option is to use a tool, such as Dreamweaver to accomplish it.
Finally you could do it with a script before you put the html on the server.

MVC Razor View: Output text as both raw and HTML parsed?

I am having a hard time figuring out how to do this...
I am essentially saving a huge blog post in a property called "Body" in a class called "Post". In body I will have various things like
<p> Hello world </p>
<p> Some random paragraph </p>
<codeblock> Here is an example of a basic HTML page
<html>
<body>
<h1> Hello Guys ! </h1>
</body>
</html>
</codeblock>
Then I want to have a code block and thus I want the HTML/CSS/Javascript/etc to just be parsed to the page as HTML encoded/decoded so I literally want the tags and angle brackets to show up on the page instead of being parsed as whatever they are.
I also have a HTML tag called which is ended by . It's nothing special it just indents and adds some specific CSS with it. I want the markup before the and after the tag to render the HTML tags as necessary.
Currently I am literally outputting the contents of the Body property using
#Html.Raw(post.Body)
Nothing special when I save it to the DB:
#Html.TextAreaFor(model => model.Body)
So for those that are still having this issue, this is how I resolved it.
1) I included prettyprint. See link below
https://google-code-prettify.googlecode.com/svn/trunk/README.html
2) When editing a post I just add the following code
//Bunch of code
Example:
var http = require("http");
var server = http.createServer(function(req, res){
console.log(req.url);
resp.write("<html><body>" + req.url + "</html></body>");
resp.end();
});
server.listen(3000);
</pre>
In my Razor View I have the following code:
<div class="blog margin-bottom-40" onload="prettyPrint()">
//Bunch of other code up here for my view
<div class="blogpost">
#Html.Raw(post.Body)
</div>
</div>
My blog is www.techiejs.com
Feel free to have a look and if you need another file from my solution let me know. Currently my git repository is private.

How to remove all HTML tags except anchor tags

Is there any method to remove HTML tags from a string without removing anchor tag links?
For example, this is my input:
<html>
<body>
Yahoo
<p>This is test content </p>
Google
</body>
</html>
And my desired output:
http://www.yahoo.com Yahoo
This is test content
http://www.google.com Google
Use Sanitize.
Tags and attributes (only the mentioned tags and attributes are allowed, nothing else).
<%= sanitize #article.body, tags: %w(table tr td), attributes: %w(id class style) %>
Here's the documentation.
You can use Nokogiri parser to parse your HTML and save the value of the href attribute whenever you encounter an <a> tag.
After lot of research this gem solves my problem:
https://github.com/premailer/premailer
But i had to modify the it's html_to_plain_text module to not remove ruby variables.
You can use Nokigiri to parse HTML.
x = Nokogiri::HTML(html_content)
output = []
x.at_css('body').children.each do |tag|
if tag.class == Nokogiri::XML::Element
output << tag.attributes if tag.respond_to?(:attributes)
output << tag.children if tag.respond_to?(:children)
end
end
puts output
[{"href"=>#<Nokogiri::XML::Attr:0x3fef80461c98 name="href" value="http://www.yahoo.com">}, [#<Nokogiri::XML::Text:0x3fef804617d4 "Yahoo">], [#<Nokogiri::XML::Text:0x3fef80461310 "This is test content ">], {"href"=>#<Nokogiri::XML::Attr:0x3fef80461054 name="href" value="http://www.google.com">}, [#<Nokogiri::XML::Text:0x3fef80460b7c "Google">]]
You can format the output array as you want

Am I delusional about Html encoding?

I seem to be hitting a logical contradiction. I have a view with...
</br> in the markup and when I load the page it shows a new line. Inspecting the source I can see a </br>.
Then I put #Html.Raw("</br>") and in the source I get </br>
However all the documentation says that by default razor will html encode all strings. So why does Html.Raw show an encoded string instead?
Shouldn't it be the other way around?
</br> is incorrect, you probably meant <br/>.
This being said, here's how it works:
<br/> generates <br/>
#("<br/>") generates <br/>
#Html.Raw("<br/>") generates <br/>
The Html.Raw helper is used when you do not want to get HTML encoded output in the resulting HTML. By default the # Razor function HTML encodes its argument.
From W3schools (I know it ain't W3C official)
Differences Between HTML and XHTML
In HTML, the <br> tag has no end tag.
In XHTML, the <br> tag must be properly closed, like this: <br />.
I don't know about razor it's #Html function but you don't need the '/' at all