Custom Tag Library in grails not passing HTML tags out properly - grails

I am learning grails using the definitive guide to grails 2 (using grails 2.3.7), and when I'm looking at the custom tag library it gives an example custom tag as follows:
def repeat = { attrs, body ->
int n = attrs.int('times)
n?.times { counter ->
out << body(counter +1)
}
}
so when I use this tag like so:
<g:repeat times="3">
Hello number ${it}<br>
</g:repeat>
I expect to get three separate lines on my rendered HTML:
Hello number 1
Hello number 2
Hello number 3
Instead I get:
hello number 1<br>hello number 2<br>hello number 3<br>
I have found methods that look like they should help, like decodeHTML() however I am thus unable to change the output that I want, and I'm not sure what I'm doing wrong.
I have tried doing:
out <<body.decodeHTML()
but I get a null pointer error...

That does not make sense unless there is something else in your taglib or something unusual in the GSP which is invoking the tag.
Does your taglib maybe have something like defaultEncodeAs='html' in it?

Use the tag like this:
<g:repeat times="3">
Hello number ${it}<br/>
</g:repeat>
HTML5 can render <br>, but it seems you are using version 4 or lower.

Related

How to render HTML predefined tag in Razor view?

I want to render the HTML predefined tag (between h2 to h6) based on what is set in my model. Below is the snippet. I am facing issue in my closing tag. closing tag is not processed and it is considered as text and it is truncated in the page view source.
string subArticleLevel = "h2";
if(subarticle.SubTitleLevel!=null)
{
subArticleLevel = subarticle.SubTitleLevel;
}
<#subArticleLevel>#subarticle.SubTitle</#subArticleLevel>
You can use Html.Raw method with explicit code block notation.
#Html.Raw("<")#(subArticleLevel)#Html.Raw(">")#(subarticle.SubTitle)
#Html.Raw("</")#(subArticleLevel)#Html.Raw(">")
Or
Simply use #: prefix to denote it is a start of an html block, if you are already in a code block. The below should work fine.
#{
string subArticleLevel = "h2";
string subarticleSubTitle = "test";
#:<#subArticleLevel>#subarticleSubTitle</#subArticleLevel>
}
I'm not sure what version of MVC you are on but if you have c# 6 you might just use c# string interpolation.
#($"<{subArticleLevel}>{subarticle.SubTitle}</{subArticleLevel}>")
I haven't tried it but if you are getting html encoding you could try.
#Html.Raw($"<{subArticleLevel}>{subarticle.SubTitle}</{subArticleLevel}>")
If you don't have c# 6 available you can try .
#Html.Raw(string.Format("<{0}>{1}</{0}>", subArticleLevel, subarticle.SubTitle))

How to show String new lines on gsp grails file?

I've stored a string in the database. When I save and retrieve the string and the result I'm getting is as following:
This is my new object
Testing multiple lines
-- Test 1
-- Test 2
-- Test 3
That is what I get from a println command when I call the save and index methods.
But when I show it on screen. It's being shown like:
This is my object Testing multiple lines -- Test 1 -- Test 2 -- Test 3
Already tried to show it like the following:
${adviceInstance.advice?.encodeAsHTML()}
But still the same thing.
Do I need to replace \n to or something like that? Is there any easier way to show it properly?
Common problems have a variety of solutions
1> could be you that you replace \n with <br>
so either in your controller/service or if you like in gsp:
${adviceInstance.advice?.replace('\n','<br>')}
2> display the content in a read-only textarea
<g:textArea name="something" readonly="true">
${adviceInstance.advice}
</g:textArea>
3> Use the <pre> tag
<pre>
${adviceInstance.advice}
</pre>
4> Use css white-space http://www.w3schools.com/cssref/pr_text_white-space.asp:
<div class="space">
</div>
//css code:
.space {
white-space:pre
}
Also make a note if you have a strict configuration for the storage of such fields that when you submit it via a form, there are additional elements I didn't delve into what it actually was, it may have actually be the return carriages or \r, anyhow explained in comments below. About the good rule to set a setter that trims the element each time it is received. i.e.:
Class Advice {
String advice
static constraints = {
advice(nullable:false, minSize:1, maxSize:255)
}
/*
* In this scenario with a a maxSize value, ensure you
* set your own setter to trim any hidden \r
* that may be posted back as part of the form request
* by end user. Trust me I got to know the hard way.
*/
void setAdvice(String adv) {
advice=adv.trim()
}
}
${raw(adviceInstance.advice?.encodeAsHTML().replace("\n", "<br>"))}
This is how i solve the problem.
Firstly make sure the string contains \n to denote line break.
For example :
String test = "This is first line. \n This is second line";
Then in gsp page use:
${raw(test?.replace("\n", "<br>"))}
The output will be as:
This is first line.
This is second line.

dust js partials not working inside loop

I'm trying to do something similar to the below code.(trying to print an partial inside a loop)
{#projects
{<greeting}
Hello {.name}
{/greeting}
{/projects}
But im getting an output like below:
Hello
Hello
....
As you can see that partial is not printed inside the loop.
My actual code http://jsfiddle.net/WKxzb/1/
Most of the documentation examples suggest something like this fiddle: http://jsfiddle.net/cnftm/
In short, in greeting.tl:
Hello, {name}!
then in projects.tl:
{#projects}
{>greeting /}
{/projects}
If where you store name didn't happen to be named name:
{#projects}
{>greeting name=someOtherVar /}
{/projects}
Anything you want to be parametrized has to be, ahem, passed as a parameter, and no body is supported.
The exception to these is blocks with inline partials, which are like this fiddle: http://jsfiddle.net/PWYBr/1/
In short, in greeting.tl:
Hello, {+name}Anonymous Coward{/name}!{~n}
then in projects.tl:
{#projects}
{>greeting/}
{<name}
{name}
{/name}
{/projects}
This works great for loops, but you can't reuse the greeting partial ANYWHERE else on the page... basically, whoever writes {<name} last wins, such that this will do weird things:
{>greeting/}
{<name}everybody{/name}
{#projects}
{>greeting/}
{<name}
{name}
{/name}
{/projects}

How do I disable Transformations in TYPO3 RTE Editor?

I created a custom extension for TYPO3 CMS.
It basically does some database queries to get text from database.
As I have seen, TYPO3 editor, transforms data before storing it in database so for example a link <a href="....." >Link</a> is stored as <link href>My Link Text</link> and so on for many tags like this.
when I query data from DB, I get it as it is stored in DB (<link href>My Link Text</link>)
so links are not displayed as they shoud. They display as normal text..
As far as I know there are two ways to go:
disable RTE transformations (how to do that?)
use lib.parseFunc_RTE (which i have no Idea how to configure it properly)
any idea?
thanks.
I guess you're not using Extbase and Fluid? Just as a reference, if you are using Extbase and Fluid for your extension you can render text from the RTE using Fluid:
<f:format.html>{bodytext}</f:format.html>
This uses lib.parseFunc_RTE to render the RTE text as HTML. You can also tell it to use a different TypoScript object for the rendering:
<f:format.html parseFuncTSPath="lib.my_parseFunc">{bodytext}</f:format.html>
Useful documentation:
parseFunc
Fluid format.html
I came across the same problem, but using EXTBASE the function "pi_RTEcssText" ist not available anymore. Well maybe it is, but I didn't know how to include it.
Anyway, here's my solution using EXTBASE:
$this->cObj = $this->configurationManager->getContentObject();
$bodytext = $this->cObj->parseFunc($bodyTextFromDb, $GLOBALS['TSFE']->tmpl->setup['lib.']['parseFunc_RTE.']);
This way I get the RTE formatted text.
I have managed to do it by configuring the included typoscript:
# Creates persistent ParseFunc setup for non-HTML content. This is recommended to use (as a reference!)
lib.parseFunc {
makelinks = 1
makelinks.http.keep = {$styles.content.links.keep}
makelinks.http.extTarget < lib.parseTarget
makelinks.http.extTarget =
makelinks.http.extTarget.override = {$styles.content.links.extTarget}
makelinks.mailto.keep = path
tags {
link = TEXT
link {
current = 1
typolink.parameter.data = parameters : allParams
typolink.extTarget < lib.parseTarget
typolink.extTarget =
typolink.extTarget.override = {$styles.content.links.extTarget}
typolink.target < lib.parseTarget
typolink.target =
typolink.target.override = {$styles.content.links.target}
parseFunc.constants =1
}
}
allowTags = {$styles.content.links.allowTags}
And denied tag link:
denyTags = link
sword = <span class="csc-sword">|</span>
constants = 1
nonTypoTagStdWrap.HTMLparser = 1
nonTypoTagStdWrap.HTMLparser {
keepNonMatchedTags = 1
htmlSpecialChars = 2
}
}
Well, just so if anyone else runs into this problem,
I found one way to resolve it by using pi_RTEcssText() function inside my extension file:
$outputText=$this->pi_RTEcssText( $value['bodytext'] );
where $value['bodytext'] is the string I get from the database-query in my extension.
This function seems to process data and return the full HTML (links, paragraphs and other tags inculded).
Note:
If you haven't already, it requires to include this file:
require_once(PATH_tslib.'class.tslib_pibase.php');
on the top of your extension file.
That's it basically.

Grails: out vs return

An strange problem occourse on grails 1.2.4 on my machine only....
We are using an custom taglib which can be accessed from services via gspTagLibraryLookup-bean from AppContext.
On my local machine
<my:span value="abc" title="${my.write(text:'123')}"/>
writes:
123<span title="">abc</span> <!-- what i see -->
<span title="123">abc</span> <!-- what my collegue see -->
my:write is defied as:
def write = {out << attrs.text}
But: If i use return instead of out, the html generate what my collegue see.
Anyone know the difference?
Argument value for the title attribute is evaluated before passing it to <my:span>. So if you define write as {out << attrs.text}, and use it in <my:span>, the write function will write to out before span function does, and return nothing - so 123 will be written to output before <span>, and the title attribute will be empty.
If you define write as {return attrs.text}, its evaluation won't write anything to out, and return 123 which will be inserted as value of title attribute.
Not sure why the first definition works on your collegue's machine.

Resources