I have my jade file and I have a select input setup with the following(using Laravel as well):
option(value="1", selected!='{!! $client->single_check == 1 ? "true" : "false" !!}') Yes
option(value="0", selected!='{!! $client->single_check == 0 ? "true" : "false" !!}') No
I am fairly new to Jade so I am trying to figure out how to use this correctly. Obviously selected="true" doesn't work it has to be selected=true, or even a way just to make it say "selected" or it just doesn't show selected at all. Does anyone know the correct way I should be doing this? If I take away the "!=" and just make it "=" it wont work. If I take away the quote marks it wont work either. I have a feeling this is something simple like I'm just not finding it in the documents.
This morning I tried creating a mixin as well and using it inside the option tag but it didnt work either.
option(value="1", +lv('{{ $client->single_check == 1 ? "selected" : "" }}')) Yes
option(value="0", +lv('{{ $client->single_check == 0 ? "selected" : "" }}')) No
Mixin:
mixin lv(content)
!{content}
If anyone has a better way to do this let me know and I will mark yours as the answer! For now I just created a whole new option mixin:
+lopt('1', '{{ $client->single_check == 1 ? "selected" : ""}}', 'Yes')
+lopt('0', '{{ $client->single_check == 0 ? "selected" : ""}}', 'No')
Mixin:
mixin lopt(val, sel, text)
| <option value="!{val}" !{sel}>!{text}</option>
Related
I have a condition to check what URL it is so I can set a proper tab class to be active.
I have code
`{{ Request::is('popular') || Request::is('/') || Request::is('category/*/popular') || Request::is('category/*') ? 'nav-active' : '' }}`
The last condition is wrong, for example when the URL is http://localhost:8000/category/goodwin/new that will still be correct, which I don't want. How can I write a condition to check if there is anything after category but only after one /
P.S. I have other conditions for new and top posts which work correctly
You can use url()->current() and then do string analysis on that. E.g. with preg_match and a regex, or by simply counting the number of slashes.
Example:
preg_match("/(.*)\/category\/(.*)\/(?!popular)(.*)/", url()->current());
The regex (.*)\/category\/(.*)\/(?!popular)(.*) checks whether the URL matches .../category/*/* except where the final * is popular.
That would allow you to do:
{{ (Request::is('popular') ||
Request::is('/') ||
Request::is('category/*/popular') ||
Request::is('category/*')) &&
preg_match("/(.*)\/category\/(.*)\/(?!popular)(.*)/", url()->current()) == 0
? 'nav-active' : '' }}
I would consider moving away from the ternary operator as this is getting quite bulky. You should probably also put this logic in the controller and just pass a variable to the view.
Here is the application.properties file:
myVar=${SOME_VAR:#{null}}
result=myVar is #{myVar != null && myVar.length() > 0 ? '' : 'not'} populated
What I am trying to get is if the environment variable SOME_VAR is set (and not blank), the property result should be myVar is populated, otherwise myVar is not populated.
The code I put above doesn't work (the line to set result), and I have also tried different combinations of #{} and ${}, including wrapping myVar, but no success so far.
What is the correct way to do? Thanks.
You wont be able to refer the myVar field directly if your member variable are private. So you should put your condition directly on the property value.
please check below expression as per your requirement.
#Value("myVar is #{ '${SOME_VAR}' != null && '${SOME_VAR}'.trim().length() > 0 ? '' : 'not'} populated")
private String result;
The #{ } is an expression language feature, while ${ } is a simple property placeholder syntax.
I ended up doing
result=myVar is #{'${SOME_VAR:#{null}}' != '#{null}' && '${SOME_VAR:#{null}}'.trim().length() > 0 ? '' : 'not'} populated
Is there a syntax to specify inline if and else if statement in Laravel blade template?
Normally, the syntaxt for if and else statement would be :
{{ $var === "hello" ? "Hi" : "Goodbye" }}
I would now like to include else if statement, is this possible?
{{ $var === "hello" ? "Hi" : "Goodbye" else if $var ==="howdie ? "how" : "Goodbye""}}
You can use this code in laravel blade:
{{ $var === "hello" ? "Hi" : ($var ==="howdie ? "how" : "Goodbye") }}
remember not every short code is a good one. in your example there's no single way to hit this else if because you're saying
if($var === "hello")
{
// if the condetion is true
"Hi";
}
else
{
// if the condetion is false
"Goodbye";
}
// error here
else if($var ==="howdie")
{ "how"; }
else
{ "Goodbye"; }
this's wrong you can't use two elses respectively. you've structure your conditions like
if (condition) {
# code...
} elseif (condition) {
# code...
} else {
}
the same in the ternary operators
(condition) ? /* value to return if first condition is true */
: ((condition) ? /* value to return if second condition is true */
: /* value to return if condition is false */ );
and beware of (,) in the second condition.
and as you see your code is just going to be tricky, unreadable and hard to trace. so use the if else if if you've more than one condition switching
and revise your logic.
<select id="days" class="Polaris-Select__Input" name="days" aria-invalid="false">
<option value="10" #if($settingsData->days == "10") selected #endif >at 10 Days</option>
</select>
#if($settingsData->days == "10") selected #else not selected #endif
with this code you can write single line if-else laravel blade with four condition.
{
{
$a == 10
? "10"
: $a == 20
? "20"
: $a == 30
? "30"
: $a == 40
? "40"
: "nothing";
}
}
$pandit->pandit_id != (auth()->user() ? "not pandit" : (auth()->guard('pandit')->user() ? auth()->guard('pandit')->user()->id : "vendor"
I believe that is two if else statements in one line. I cant imagine way to make it inline but i would have done something like this.
#if($var=="hello" || $var=="Hi")
{{$var === "hello" ? "Hi" : "Howdie"}}
#else
{{"Goodbye"}}
#endif
I have the following including of templade blade in main layout:
#if (Auth()->user()->verified == "0")
#include('common.verify-error')
#endif
So, how to display #include('common.verify-error') on the all URL excluding /test, /out
Should I do this in controller or simple if condition in template?
You can use is() method with mask:
#if (auth()->user()->verified == 0 && !request()->is('test/*') && !request()->is('out/*'))
#include('common.verify-error')
#endif
To start, there's probably a few different approaches to this. Personally, I'd do something along the lines of a middleware class...or inject a class in the view, etc.
However, he's another solution that may suit your needs better.
EDIT: Made a change. I'm assuming you use named routes (and should, in the event you want to change urls, etc. You won't have to deal with changing all these if statements for example).
#if (Auth()->user()->verified == "0" && !Route::is('test') && !Route::is('out'))
#include('common.verify-error')
#endif
I am in the middle of rewriting my Spring MVC application from JSP pages to Thymeleaf templates. I am however experiencing the following problem.
When I am using ternary operator with results that are of different types namely java.lang.String and java.lang.Integer the string is always presented as 0 if the condition in ternary operator is not fulfilled.
<p th:text="#{free_transfers} + ': ' + (${i ne T(java.lang.Integer).MAX_VALUE}
? ${i} : '∞')">Cumulated free transfers: ∞</p>
The resulting HTML is however
<p>Free transfers: 0</p>
if i is equal Integer.MAX_VALUE.
At first I thought that this is because of the fact that the second argument is of type int so I explicitly added the conversion to character string.
<p th:text="#{free_transfers} + ': ' + (${i ne T(java.lang.Integer).MAX_VALUE}
? ${#strings.toString(i)} : '∞')">Cumulated free transfers: ∞</p>
however this does change anything and the result is still
<p>Free transfers: 0</p>
Does anybody have any idea how to achieve the expected result
<p>Free transfers: ∞</p>
?
I have also tried these ones but without any success.
|#{free_transfers}: ${i ne T(Integer).MAX_VALUE ? #strings.toString(i) : "∞"}|
|#{free_transfers}: ${i ne T(Integer).MAX_VALUE ? i : "∞"}|
It should be all within one ${} expression also you might not need toString just use i
${i ne T(java.lang.Integer).MAX_VALUE ? i : '∞'}
There is a problem at the beginning with the order of "+" sign and ":" sign. This one works:
<p th:text="'Free transfers :'+ (${i ne T(java.lang.Integer).MAX_VALUE}
? ${i} : '∞')">Cumulated free transfers: ∞</p>
The problem was in fact in the part that provided the value of i variable. Instead of Integer.MAX_VALUE it provided 0, so no wonder it was displayed as 0.