I have a form where I display four text field inputs. The current app()->getLocale() input is displayed on the left, and the below code is for the remaining 3 locales which are displayed on the right:
#foreach(['ca','en','es','nl'] as $lang)
#if(app()->getLocale() == $lang) #continue #endif
<li>
<a href="#{{ $lang }}" class="#if($loop->first) active #endif"
</li>
#endforeach
These are all menu tabs that are hidden, only the first one should be displayed as active, hence:
#if($loop->first) active #endif
The problem however is, that when the current locale is ca, also the $loop->first() will be ca. And this one cannot be the active one, since it will never be displayed on the right side.
I am trying to find an easy fix without too many if else things. Also, the array ['ca','en','es','nl'] will be changed for some data that comes from the config, so there will be more locales later and ca will not always be the first one. So I cannot do checks with #if(app()->getLocale() == 'ca') since that will change in the future as well.
Instead of:
['ca','en','es','nl']
with:
array_diff(['ca','en','es','nl'], [app()->getLocale()])
and remove this:
#if(app()->getLocale() == $lang) #continue #endif
This will remove the item representing the current language from your array.
Related
I'm having a problem with #if syntax.
I want to show some message but it's doesn't work.
It works only on one type. but I need in 3 types.
#if($article->type == 'analytic', 'news', 'interview')
SHOW THIS
#endif
You have to use like below
#if (in_array($article->type, array("analytic", "news", "interview")))
SHOW THIS
#endif
Update
Array can be define also like this which will short your code
["analytic", "news", "interview"]
First, I think you should probably learn programming as a whole first, then start using Laravel.
If what you want is to show users a certain content based on if the $article->type is one of the values you listed i.e. ["analytic", "news", "interview"];
You can do this:
#if (in_array($article->type, ["analytic", "news", "interview"]))
SHOW THIS
#endif
There are couple of other alternatives as well, but the above is the most performant solution off the top of my head.
I am trying to pick the master blade template dynamically as per the current user roll logged in. (here it should go to the 'shopowner' auth block)
#auth('shopmanager')
#extends('theme::Admins.shopmanager.layout.master')
#endauth
#auth('shopowner')
#extends('theme::Admins.shopowner.layout.master')
#endauth
but this always gives error as it tries to compile the 'shopmanager' master template. It is not going into the 'shopmanager' #auth block because it's not printing anything if I print inside that block.
It only works if I completely comment out that line.
P.S.:
This is the master theme::Admins.shopmanager.layout.master template file
which must not be loaded.
#extends('theme::Admins.outline.layout.master')
#include('theme::Admins.shopmanager.layout.common.header')
#include('theme::Admins.shopmanager.layout.common.left-sidebar') // The error throws from inside this view.
#include('theme::Admins.shopmanager.layout.common.footer')
#section('title-head', __('Shop Manager'))
I can wrap the #auth check around #include lines but the point is, this complete file should be skipped from the compilation.
SOLVED
As per my learning, #extend(...) will always be compiled regardless of outer wrap conditions. so must be moved to dynamic variable based blocks.
#auth('shopmanager')
#php
$masterTemplate = 'theme::Admins.shopmanager.layout.master';
#endphp
#endauth
#auth('shopowner')
#php
$masterTemplate = 'theme::Admins.shopowner.layout.master';
#endphp
#endauth
#extends($masterTemplate)
Try below code ,i hope this ans help you:
#if(Auth::check())
#if(Auth::user()->role=='shopmanager')
#extends('theme::Admins.shopmanager.layout.master')
#else
#extends('theme::Admins.shopowner.layout.master')
#endif
#endif
If you have multi-authentication that time try below :
#if(Auth::guard('shopmanager')->user())
#extends('')
#else
#extends('')
#endif
In my case, I'm handling manager, customer and admin with this code.
Manager : Auth::guard('manager')->user()
Admin : Auth::user()
Customer : Auth::guard('customer')->user()
I'm trying to create a continous system for an online magazine's website that I'm building. I want to obviously display artwork differently than I do with other pieces (say a poem or an essay), but I'm having trouble composing the loop that works with both of them. Here's the situation:
I have a $current_genre variable that tells me which genre is currently being read (this is all done via an admin panel). As below, the loop is supposed to identify what the current genre is and then display a certain "tile" accordingly:
#foreach ($pieces as $piece)
#if ($current_genre == 'Artwork')
<div>
<p>Display for images</p>
</div>
#else
<div>
<p>Display for other items</p>
</div>
#endif
#endforeach
I'm not sure what the problem is, as this looks fine to me. It certainly isn't the controller, because I echo the $current_genre variable in other places throughout the page, so I'm not sure what's going on.
EDIT for clarification: The main problem is that no matter what I do, the artwork always appears as a regular piece should, and never as I had it set under the #if($current_genre == 'Artwork') section
Thanks in advance for any help.
Assuming genre is the property of $peice, you can implement the loop as:
#foreach($pieces as $piece)
{
#if($piece->genre == 'Artwork')
** Some Code **
#else
** Some Other Code **
#endif
}
#endforeach
If this does not answer your question,
you could make the question clearer by showing how you are getting the $current_genre variable.
$current_genre is not defined how can you compare like that #if ($current_genre == 'Artwork') you have to access the #if ($piece->current_genre == 'Artwork') like that. hope this will help you
I have conditions in blade and working fine but little bit worry if somebody comes and use inspect element so he can add href and link and make button as example working if it was disabled, so what is the good way to secure my code
#if (auth()->user()->customer->package_id>0)
<li class="btn_disabled"> Allready Purchased</li>
#elseif(auth()->user()->customer->package_id==null)
<li class="buttonprice">Purchase</li>
#endif
With blade, it is possible to show a default value if there is no data to #yield, like so:
#yield('section', 'Default Content');
But I would really like to show a view instead of a string text. The reason is that I want to #yield a navigation menu into a layout. Most of the times the navigation menu will be exactly the same, so a default option of some sort should be optimal here, but I would like a way to override the menu.
Is it possible to achieve this?
To check if the section exists you could use:
array_key_exists('fooSection', View::getSections())
and to check if the section is empty you could use:
empty(View::getSections()['fooSection'])
For example:
#section('fooSection')
Foo
#stop
#section('barSection')
Bar
#stop
<h1>If Section Exists</h1>
#if (array_key_exists('fooSection', View::getSections()))
#yield('fooSection')
#else
#yield('barSection')
#endif
<h1>If Section isn't Empty</h1>
#if (!empty(View::getSections()['fooSection']))
#yield('fooSection')
#else
#yield('barSection')
#endif
Hope this helps.