Thymeleaf add value in style - thymeleaf

I have a div block that has styles. I need to add another style with a substitution value. I imagined it like this:
th:styleappend="background-position:-${value}%"
But it doesn't work. Is there a possibility to do this?

Most probably you are having a syntax issue here. Try the following code:
th:styleappend="'background-position:-' + #{${value}} + '%'"

Related

Is it possible to have tags with spaces?

I am using the ng-tags-input library, however, when my responses come back with spaces they are replaced with hyphens in the tag. Is there a way to prevent this? At a minimum can I get the original value?
Looks like I need replaceSpacesWithDashes on tags input trying now.

grails i18n line break

Using the Grails internationalization messages.properties I'm trying to create a multi-line message, but cannot seem to find a way to create a new line without using the <br> element, and I'd prefer to keep presentation logic out of the message. I've tried using "\n" but that doesn't get rendered.
I know I can use multiple messages "message.1=...", "message.2=...", but that doesn't seem as clean either.
Here's what I'd like to be able to do:
messages.properties
helptext=First Line\nSecond Line\nThird Line
page.gsp
<g.message code="helptext"/>
result:
First Line
Second Line
Third Line
Everything I've found either says to use <br> element, or do a replaceAll on \n, but I was hoping to not have to use extra processing to handle this.
I think you have to use <br> in the message directly.
//messages.properties
helptext=First Line<br>Second Line<br>Third Line
//Gsp
<p><g:message code="helptext"/><p>
\ gives the ability to break the line in the properties file but renders as a single line in view.
For me (i18n message properties in Grails 2.0 project) worked following line:
property = Line1\\nLine2\\nLine3
HTML tag BR worked also fine if displayed on HTML page, but was not any good for me, because I in my case this text needed to be a text string not HTML.
You could write a custom tag that converts \n into br tags as well. It would just need to call the messageSource bean and parse the results. Thus your messages would not have to be HTML-specific

Possible to dynamically change the name of a symbol in a partial?

I'm curious if there's a way to change the name of a symbol in a partial based off the parameters that's passed to it. For example, it's possible to do something like this
in a partial:
f.label("#{parameter}")
where parameter was passed into the partial when it was rendered. Now lets say instead we have something like
blog_path(domestic_or_international: "domestic")
would it be possible to use a similar method to change the symbol domesitc_or_international like I did in the first example? Thanks a bunch!
you can use the old hash syntax to do this.
blog_path(:"static_text_#{parameter}" => "domestic")

Trying to implement RenderPartial

I've tried a lot of combinations but still getting run time errors:
#{Html.RenderPartial("~/Views/_" + #Model.Id + ".cshtml")};
Is there something obvious I'm getting wrong. I have the Id field in model set correctly but I get syntax errors. Such as:
Compiler Error Message: CS1002: ; expected
You can't have the semi-colon at the end. Edit: You actually need it right after the call of the RenderPartial because you are using curly braces. Also, you do not need the # for the Model. You also do not need the .cshtml.
#{Html.RenderPartial("~/Views/_" + Model.Id);}
I'm also a little worried of how you are using the Model's ID for the name of the partial view. Be sure to check your paths and the names of your partial views.
You may want it to look something like this:
#{ Html.RenderPartial("_AwesomePartialView", Model);}
Check out this too: http://rachelappel.com/razor/partial-views-in-asp-net-mvc-3-w-the-razor-view-engine/

Convert Line breaks to html break for all field getters in Symfony project

I am working on a Symfony project and I currently have this:
<?php echo preg_replace('/\n/','<br />', $review->getComments()); ?>
and would very much like to be able to make all getters add html line breaks so i don't have to pepper my code with preg_replace. the $object->getFieldname methods are work automatically so I am looking to extend this somewhere to globally add a new method. What is the best approach here?
Seems like everyone forgot about nl2br() which is a function that does exactly that in PHP.
nl2br($review->getComments());
EDIT: At the time of this writing, everyone else uses preg_replace().
I think the best idea would be to add a getCommentsHtml() method onto your review object, which does something like:
return preg_replace('/\n/','<br />', $this->getComments());
Then you can use $review->getCommentsHtml() to format them using HTML. Also as Charlie mentioned, maybe str_replace would be better to use, as using a regular expression to change \n's into <br />'s may be a little bit of overkill :)
So if you don't want to have your code littered with replaces like this, I think putting a helper method on the classes that you'd like to format nicely would be the best way to go :)
How about:
str_replace("\n",'<br />', $review->getComments());

Resources