What class should be used for param thymesVar - spring-boot

In IntelliJ I'm using the thymesVar comment to resolve variables used in Thymeleaf expressions.
I'm wondering what should be in there to resolve the param variable.
For example, when I have this in the Thymeleaf template:
<div th:if="${param.error}" class="alert alert-danger">
<p>Invalid username and password.</p>
</div>
Then what should be the type
<!--/*#thymesVar id="loginCommand" type="something.to.resolve.param"*/-->

The answer is a little late and you've probably learned it by now.
But for other readers, param.error is a String array.
So type="java.lang.String[]".
You can double check this by using th:text=${param.error.class.name}.

Related

How to get value from input thymleaf and post to controller with th href spring boot

I have problem with thymleaf spring boot, my input is like this
<input class="-Text" type="text" style="border:none" placeholder="1234567890xx" th:value="${sn}" name="sn"/>
I want to get parse the value from input to my th href thymleaf code like this
<a id="btnx" th:href="#{/pages/input-order-manual/finish-confirm/?sn=__${sn}__}" class="btn btn-primary btn-lg btn-block">Confirm</a>
But when I try to run, this value always return null for my sn variable, what must I do, to make this code run?
For any help,
Thank you in advance
Insert full code. Run code in debug, add break point before return template and make sure you have sn variable in model. Also you can concat th:href (th:href="#{/pages/input-order-manual/finish-confirm/?} + ${sn}")

Translation in parameter of translation with Thymeleaf

Using Thymeleaf 3.0.9, suppose I have this translation:
hello=Hello {0}
In my template, I have:
<span th:text="#{hello(${user.getName()})}"></span>
Now suppose user.getName() can be null, so I want to display something nice in that case:
<span th:text="#{hello(${user.getName()?:'Unknown'})}"></span>
This works fine.
Now I also want to tranlate Unknown, how can I do this?
I tried:
<span th:text="#{hello(${user.getName()?:#{unknown})}"></span>
But that does not seem to work.
I think you can use the #messages utility object:
<span th:text="#{hello(${user.getName()?:#messages.msg('unknown')})}"></span>
And add property in the messages file(s):
unknown=Unknown

Correct approach for rendering optional param in blade

I noticed that below construction is not correct for Blade
<div class="constant #if($some_condition)optional#endif">
cause this is converted to in PHP which is invalide
<div class="constant <?php if($some_condition): ?>optional<?php endif; ?>">
To make this statement work I need to put close tag on different line but it looks very bad, especially if there are many others attribute.
<div class="constant #if($some_condition)optional
#endif" data-id="1" tabindex="2" data-etc="...">
What is the right way of rendering such kind of conditions in Blade?
Try the short version of if statement.
$some_condition ? 'optional' : ''
Using the code above, if the condition is true it will return the the string 'optional', if the condition is not true it will return an empty string I mean this ''. Note, an empty string as attribute do nothing inside Html element.
In your case it should look like this:
<div class="constant {{ $some_condition ? 'optional' : '' }}">

How to return view with flash message?

What i need is this:
return view('protected.standardUser.includes.documents',compact('documents'))->with('successMsg','Property is updated .');
The i could use it like this:
#if(Session::has('successMsg'))
<div class="alert alert-success"> {{ Session::get('successMsg') }}</div>
#endif
Any suggestion?
It looks like you're mixing two different ways of passing data to a view, which I'm guessing is the problem. The documentation seems to indicate it's a one or the other type situation. You also seem to be mixing up view()->with() and redirect()->with(), which work differently. Try this:
return view('protected.standardUser.includes.documents')->with('documents', $documents)->with('successMsg','Property is updated .');
And
#if(!empty($successMsg))
<div class="alert alert-success"> {{ $successMsg }}</div>
#endif
I order to use session stored flash messages, need to use following code into route method.
$request->session()->flash('successMsg','Saved succesfully!');
and then do
#if(Session::has('successMsg'))
<div class="alert alert-success"> {{ Session::get('successMsg') }}</div>
#endif
that's it
I'm new in Laravel and I was facing the same issue... I just tried as below::
\Session::flash('flash','Message info');
And it works!!
This works for me!
return view('protected.standardUser.includes.documents',compact('documents'),
['successMsg'=>'Property is updated .']);
just remove the with() and pass it in the return view()

Laravel: Render queried Data whith Blade functions

Currently I'm getting some data from the database and after that I want to render it within my Blade template.
In my queried data I have blade functions like url('/foo') combined with some html. And here is the problem.
When I'm using {!! $child->description !!} the HTML is rendered correctly, but my Blade function won't work:
Function: url('/foo)
Output: http://myurl.de/url('/foo')
When I'm using the "normal" Syntax like {{ $child->description }} the generated URL is correct (http://myurl.de/foo), but the HTML is not rendered.
So my question is:
How can I use my queried Blade function within rendered HTML? ^^
/Edit
Okay, perhaps my question is too abstract. So I want to show you my problem based on my example. (generated template image - only on german, sorry)
Every form is a database entry like:
categoryName
categoryParent
...
categoryDescription
As you can see on my image, the categoryDescription is the small text under my first input field with the small button.
I want to use this script abstract as possible so that I can fill the entry with every content I want to fill in.
In this case my content is:
lore ipsum <a class="btn btn-primary btn-sm pull-right" href="url('foo')">dolor</a>
As you can see there is the mentioned Blade-function (url) and the HTML.
{!! !!} - this dont escapse string so if u have something from database like,
something it would output it like that.
While in other hand {{ }} this would give you just "something" without , it is used to protect you from injections.
Maybe blade error.{{}}
lore ipsum <a class="btn btn-primary btn-sm pull-right" href="{{url('foo')}}">dolor</a>
Laravel Blade

Resources