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
Related
To test image urls in a ruby project I can call a function as follows
validate_url("some_valid_url", "valid")
validate_url("inv#alid-url", "invalid")
The function looks like this:
def validate_url(image_url, state)
assert state === 'valid' ? new_product(image_url).valid? : new_product(image_url).invalid?,
"#{image_url} should always be " + state.upcase
end
is there a way to rewrite the line:
assert state === 'valid' ? new_product(image_url).valid? : new_product(image_url).invalid?
to something like
assert new_product(image_url).state.What-To-Do-Here?
which would then be equals to the following if state contains the string "valid"
assert new_product(image_url).valid?
You can do....
assert new_product(image_url).send(state + '?'), "#{image_url} should always be #{state.upcase}"
It would be prudent to ensure that state can only contain "valid" or "invalid"
I am stuck at how to properly use nested conditions as proposed in the Jenkins syntax.
https://jenkins.io/doc/book/pipeline/syntax/#when
This is my current stage:
stage('Build'){
when{
anyOf{
allOf{
expression{env.BRANCH_NAME != 'master'}
expression{env.AUTO_BUILD == true && env.BUILD_OPT == snapshot && env.BRANCH_NAME !=~ /feature.+/}
}
expression{env.AUTO_BUILD == false}
}
}
steps{
echo env.AUTO_BUILD
echo env.BUILD_OPT
echo env.BRANCH_NAME
}
From my point of understanding is, if I set env.AUTO_BUILD = false, then this stage should be executed, since it is enclosed in an anyOf OR it would execute if my branch was e.g. develop and AUTO_BUILD = true, BUILD_OPT = snapshot.
However, this was not the case when I set AUTO_BUILD = false. The stage was not executed. Am I missing something?
There are two issues here in the declarative conditionals. First, looking at:
allOf {
expression{env.BRANCH_NAME != 'master'}
expression{env.AUTO_BUILD == true && env.BUILD_OPT == snapshot && env.BRANCH_NAME !=~ /feature.+/}
}
the issue here is that !=~ is not a valid operator for "does not match regular expression" in Groovy. You can replace it with !(env.BRANCH_NAME =~ /feature/) like so:
allOf {
expression{env.BRANCH_NAME != 'master'}
expression{env.AUTO_BUILD == true && env.BUILD_OPT == snapshot && !(env.BRANCH_NAME =~ /feature/)}
}
to achieve the behavior you desire.
Secondly, in the conditional:
expression{env.AUTO_BUILD == false}
the expression is checking for a boolean type in env.AUTO_BUILD. If the value is being assigned a string 'false', then the type check will fail and the conditional will not behave as expected. Inputting the environment env.AUTO_BUILD assignment as a boolean env.AUTO_BUILD = false and not a string env.AUTO_BUILD = 'false' will rectify this for you.
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>
I'm writing a script in bash, and I'm trying to read from an array. When I iterate through the array with the code below:
for item in "${!functionDict[#]}"
do
echo "{ $item : ${functionDict[$item]} }" >> array.txt
done
it outputs (in "array.txt"):
{ month_start_date($year_selected, $month_selected) : return $date; }
{ logWarning($message) : return logEvent($message, $c_event_warning); }
{ daysPastLastQuarterX($curYear, $curMonth, $curDay, $selected_year, $selected_quarter, $nDays) : return false;:return false;:return false;:return false;:return true;:return $delta > $nDays; }
{ setExcelLabelCell($sheet, $cell, $label, $width) : }
{ asCurrencyString($value) : return formatCurrency($value); }
{ getNumericMonthName($m) : return $numericMonth; }
{ normalize_for_PDF(&$text) : }
However, I'm having trouble querying individual elements from the array.
I've tried:
string='month_start_date($year_selected, $month_selected)'
echo "test_output: ${functionDict[$string]}"
but I get
test_output: <blank>
I've also tried inserting some RegEx wildcards, in case there is some whitespace around the key.
echo 'size of array: '"${#functionDict[#]}"
echo "TEST: functDict[logWarning] = ${functionDict[.*'logWarning($message)'.*]}"
I get
size of array: 157 //I didn't copy/paste all the elements in the array in this post
TEST: functDict[logWarning] = <blank>
Alas, I'm stuck. The content that I'm trying to get back are the "return _" items, or just a "blank" for the keys that don't have any "return" items.
Credits to answer goes to #gniourf_gniourf. (See comments)
There was an extra leading space in all of the keys, which I hadn't included in my test queries.
(Providing an answer here so that people know this question has been solved. Hopefully this is okay practice on SO)
I am trying to add some logic in this. But I am not sure how to add a second condition…
For instance:
foreach (FolderAssetInfo e in folderAssetsList)
{
var foundAsset = databaseAssetsList.Find(a => a.AssetFullName == e.AssetFullName);
//I want to add an AND logic inside the parenthesis such as:
//a => a.AssetFullName == e.AssetFullName && a.AssetFirstName == e.AssetFirstName)
if (foundAsset != null)
{
Console.WriteLine(Found it!);
}
}
How can I do that?
Your syntax is perfect:
var foundAsset = databaseAssetsList.Find(a => a.AssetFullName == e.AssetFullName && a.AssetFirstName == e.AssetFirstName);
Basically, the Predicate<T> is just syntax that will return a boolean value. Your syntax in the comment (a.AssetFullName == e.AssetFullName && a.AssetFirstName == e.AssetFirstName) will return a boolean, as written, so it will work fine for the predicate.
The one place where you do have a syntax error is your call to Console.WriteLine - this will require you to add quotation marks:
Console.WriteLine("Found it!"); // Quotes are required here!