No space between elements in a Thymeleaf each loop - thymeleaf

With Thymeleaf when I do :
<span th:each="item : ${X}">1</span>
I get :
<span>1</span>
<span>1</span>
<span>1</span>
...
How can I have this :
<span>1</span><span>1</span><span>1</span>...
?

It is processed with a new line due the next issue, to improve readability:
https://github.com/thymeleaf/thymeleaf/issues/113
At the moment, I don't think you could avoid the break line without patching it. So, if you want to have this behavior you could patch the class AbstractIterationAttrProcessor. How? Just create a class with the same name and package in your project, and copy the content of the original one:
Package: org.thymeleaf.processor.attr
Class Name = AbstractIterationAttrProcessor
Then change the conditional in line 133 to avoid the new line if it is an span element. It would be like next:
if (preserveWhitespace && index > 0 && !element.getOriginalName().equals("span")) {
parentNode.insertBefore(element, new Text(whitespace));
}
I've just added !element.getOriginalName().equals("span") to the condition. With this patch your span will be processed as:
<span>1</span><span>1</span>
I've opened an issue due your question:
https://github.com/thymeleaf/thymeleaf/issues/586

Related

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.

A tool/editor to surround single-line (Java/C#/JS) if statement with curly braces preserving it on the same line

Here's what I have:
if (something) throw new Exception("whatever");
I would like to find a tool which will do a one-time pass over all my code and change statements like the above one to this:
if (something) { throw new Exception("whatever"); }
Note that I want everything to stay on the same single line (if it was like this).
Any tool/editor/plugin/script/anything which can do the job, just once, for all my code - is helpful.
Thank you.
Apparently IntelliJ can do this via a combination of 2 settings:
Code Style > Java > If() statement > Force braces > Always
Code Style > Java > Keep when reformatting > Simple blocks in one line

Prestashop all translatable-field display none for product page

Just new in Prestashop (1.6.0.6), I've a problem with my product page in admin. All translatable-field are to display:none (I inspect the code with chrome).
So when I want to create a new product I can't because the name field is required.
I thought that it was simple to find the .js whose do that but it isn't.
If somebody could help me, I would be happy.
Thank you for your help
Hi,
I make some searches and see that the function hideOtherLanguage(id) hide and show translatable-field element.
function hideOtherLanguage(id)
{
console.log(id_language);
$('.translatable-field').hide();
$('.lang-' + id).show();
var id_old_language = id_language;
id_language = id;
if (id_old_language != id)
changeEmployeeLanguage();
updateCurrentText();
}
When I set the Id to 1 (default language), it works. It seems that when I load the page, the function is called twice and the last calling, the id value is undefined. So the show() function will not work.
If somebody could help me. Thank you.
In my console, I see only one error
undefined is not a function.
under index.php / Line 1002
...
$("#product_form").validate({
...
But I find the form.tpl template and set this lines in comment but nothing change.
EDIT: According to comment on this link http://forge.prestashop.com/browse/PSCFV-2928 this can possibly be caused by corrupted installation file(s) - so when on clean install - try to re-download and reinstall...
...otherwise:
I got into a similar problem - in module admin page, when creating configuration form using PrestaShop's HelperForm. I will provide most probable cases and their possible solutions.
The solution for HelperForm was tested on PS 1.6.0.14
Generally there are 2 cases when this will happen.
First, you have to check what html you recieve.
=> Display source code - NOT in developer tools/firebug/etc...!
=> I really mean the pure recieved (JavaScript untouched) html.
Check if your translatable-fields have already the inline style "display: none":
Case 1 - fields already have inline style(s) for "display: none"
This means the template/html was already prepared this way - most probably in some TPL file I saw codes similar to these:
<div class="translatable-field lang-{$language.id_lang}"
{if $language.id_lang != $id_lang_default}style="display:none"{/if}>
Or particularly in HelperForm template:
<div class="translatable-field lang-{$language.id_lang}"
{if $language.id_lang != $defaultFormLanguage}style="display:none"{/if}>
Case 1 is the most easy to solve, you just have to find, where to set this default language.
Solutions
HelperForm
Look where you've (or someone else) prepared the HelperForm object - something like:
$formHelper = new HelperForm();
...
Somewhere there will be something like $formHelper->default_form_language = ...;
My wrong first solution was to get default form language from context - which might not be set:
$this->context->controller->default_form_language; //THIS IS WRONG!
The correct way is to get the default language from configuration - something like:
$default_lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$formHelper->default_form_language = $default_lang->id;
...this particularly solved my problem...
Other form-creations
If there is something else than HelperForm used for form creations, the problem is still very similar.
You have to find where in files(probably tpls) is a condition for printing display:none for your case - then find where is the check-against-variable set and set it correctly yourself.
Case 2 - fields don't have inline style(s) for "display: none"
This means it is done after loading HTML by JavaScript. There are two options:
There is a call for hideOtherLanguage(), but there is wrongly set input language - that means no language will be displayed and all hidden.Solution for this one can be often solved by solving Case 1 (see above). In addition there can be programming error in not setting the after-used language id variable at all... then you would have to set it yourself (assign in JavaScript).
Some script calls some sort of .hide() on .translatable-field - you will have to search for it the hard way and remove/comment it out.
PS: Of course you can set the language to whatever you want, it is just common to set it to default language, because it is the most easier and the most clear way how to set it.

Custom Tag Library in grails not passing HTML tags out properly

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.

How to change my view file to get a appropriate output?

I have a controller like this :
def mytask = {
def user = User.findByLogin(params.id)
def mywork = user.schedules.daySchedules
[ mywork : mywork ]
}
Where I'm trying to find all the tasks assigned to a particular user. I have a corresponding view file :
<g:each in="${mywork}" var="tasks">
<div id = "todayswork">
${tasks.task}
</div>
<div id ="Dates">
${tasks.startTime}-
${tasks.endTime}
</div>
<hr/>
</g:each>
Logic works fine, I'm getting the output as I wanted. For example, if I go to http://localhost:8080/scheduleTest/daySchedule/mytask/anto my browser showing all the task for the user anto. But there is a problem in rendering it.
I'm getting the output as :
But I need the output something like this one:
How change my view file to get the appropriate output.
Thanks in advance.
It's hard to tell from your examples, but my guess is you need to be looping over the tasks item, which appears to be a List in a List.
This means change this:
<g:each in="${mywork}" var="tasks">
to this
<g:each in="${mywork[0]}" var="tasks">
// or
<g:each in="${mywork.tasks}" var="tasks">
Again, I'm not exactly sure where the problem is occurring, but one of those will fix it.
The reason you are getting the output is that Groovy will automatically perform a property expansion on a list if the property is not defined on that list. Example:
use(org.codehaus.groovy.runtime.TimeCategory) {
def d1 = 5.minutes.ago
def d2 = 1.week.from.now
assert [d1, d2].time == [d1.time, d2.time]
}
It's the same thing as writing list*.property, and returns a new list containing each property on the original items.

Resources