how laravel blade to merge request segment into url router in layout? - laravel

I'm develop in Laravel, with Router, Request, Blade (I think that's all related)
I need product filter menu at layout.blade.php
It does filter and menu like below
locations > east, west, south, north
category > shop, f&b, services
is_new > new, all
So then I make the the router as below
Route::get('/at/{location_id?}/{location?}/{category_id?}/{category?}/{is_new?}',array('as'=>'deals','uses'=>'HomeController#index'));
Current solution on my menu
I need a menu in layout.blade.php and generate url based on the user selected location, category, is_new. and this layout used for other actions as well.
I don't know how to do these logic on layout. and the solution I got at the end kinda trick.
<ul id='nav-category' class="dropdown-menu extended">
#if (Request::segment(1)=='deals')
#foreach (Deal::$categories as $category)
<li><a href='{{route("deals",array("location_id"=>Request::segment(3),"location"=>Request::segment(4),"category_id"=>$category["id"],"category"=>Str::slug($category["title"])))}}'>{{$category['title']}}</a></li>
#endforeach
#else
#foreach (Deal::$categories as $category)
<li><a href='{{route("deals",array("location_id"=>"all","category_id"=>$category["id"],"category"=>Str::slug($category["title"])))}}'>{{$category['title']}}</a></li>
#endforeach
#endif
</ul>
Problem
using Request::segment to get the state don't seem very right
may have more conditions come in and mess up the blade
This is should be a common problem, but somehow I can't find a good practice. Any suggestion?

For the parameters within the url you can use;
Route::current()->parameter('location_id|location|category_id');
Meaning your never limited to changing the url pattern within your routes, only limited to what you call the parameter you are passing.
With Request::segment(1)=='deals', there is no other way apart from;
#if(Request::is('deals/*'))
As this will check the pattern of the url to be anything starting with 'deals/'.

Related

Laravel Localization Language Switcher using currentRouteName pass argument

I am having an issue with language switcher, when trying to execute a route with more than one parameter
I get following error message
Missing required parameters for [Route: client.edit] [URI: {language}/client/{id}/edit].
in the header having an switcher code Route::currentRouteName()
<ul class="dropdown-menu" role="menu">
<li>EN</li>
<li class="divider"></li>
<li>AR</li>
</ul>
I tried below code, it is not working
<?php if(isset(request()->id)) { ?>
<li>EN</li>
<li class="divider"></li>
<li>AR</li>
<?php } else { ?>
<li>EN</li>
<li class="divider"></li>
<li>AR</li>
<?php } ?>
I have such kind of question due to every time when we use some routes, where have to send 1 and more parameters (I'm not talking about `language`) like `{id}`, `{category}` or something else, for example in your route: `/client/{id}/edit` - this kind of situation can be greatly passed but with out native multi-language implementation.
By truth, I've tried to use several variations with Middleware, ServiceProviders and else, but, the best way that I've understand is to use ready-made solution with support and going forward to reach dev. goals.
That's why I recommend you to use this package (mcamara/laravel-localization). They have already made cool functionality like route localization, standard support of many countries and some features that could be useful in the future.
Also, you will have to edit you current scripts to remove old made-localization by yourself or someone else and follow the instructions. I hope you will enjoy!

Is it recommended to make general partials for modals, drop-downs etc in Laravel Blade?

I am looking for a very general way to include bootstrap components in my blade view. For example let's say I need a drop down in my view, should I make a partial called dropdown.blade.php with code as follows:
<div class="dropdown">
<a href="#" class="dropdown-toggle" type="button" data-toggle="dropdown">
<span class="glyphicon glyphicon-chevron-down"></span></a>
<ul class="dropdown-menu">
#foreach ($options as $option)
<li>{{$option["name"]}}</li>
#endforeach
</ul>
</div>
and use it in my view in the following way:
#include('partials.dropdown',
array("options"=>array(
["href"=>"#", "name"=>"Profile"],
["href"=>"#", "name"=>"Report"],
)))
Even we can make it more generic by adding options for button name etc. Is it a good or preferable way to do it or should we use copy-paste method from bootstrap website to our views every time? Is there any package that is doing this sort of work? Can we make it in more elegant way?
This seems like a good idea if you are going to re-use the component a lot. I think the more elegant way to do it would be to create custom blade directives:
https://laravel.com/docs/master/blade#extending-blade
Then you could do, for instance:
#dropdown($options, 'btn-primary')
I would also provide an argument for a custom element ID or name, so you can reference it elsewhere on the page as needed.
This gets a little more complex with things like modals. I think you'd want to register multiple blade directives so you could do something like
#startmodal
#modaltitle('Title')
#startmodalbody
Some body content
#endmodalbody
#endmodal

Ignore element inside only certain divs

I have a basic web scraper written which pulls short sections of text from a webpage and puts them into a list. My problem is that there are dynamic ads that appear on the page and mess up the lists.
The page I'm scraping is a Yelp restaurant listing page.
I pull out the biz-name (business name) and add it to the list and it works fine but when the ads appear the scraper pulls the biz-name also.
This is the structure but I can't figure out how to ignore the 'AD element' and just scrape the normal business names. I've cut it down a lot and removed the 'unimportant' elements.
This is with an AD:
<li class="yloca-search-result">
...
...
<a class="biz-name"...><span>San Lorenzo’s</span></a>
</li>
This is a normal listing:
<li class="regular-search-result">
...
...
<a class="biz-name"...><span>BigGrill</span></a>
</li>
I've been trying to make Nokogiri ignore the business name inside the <li class="yloca-search-result"> and only select the others inside the regular-search-result class.
I can't figure it out. Can someone point me in the right direction at least? Is it possible?
I figured it out. Wasn't difficult but I just couldn't see the answer.
ad = doc3.at_css("li.yloca-search-result")
ad.remove

Blade: extending a template in multiple subviews

I am using Blade to include multiple sub-views into a single set of tabs in a blade app. All of the sub-views have the same overall structure (a sidebar and a main section). Because of this, I have created a template and each sub-view extends the template. Here is the structure:
Main.blade.php
<div id="tabs">
<ul>
#foreach($views as $view)
<li>{{$view}}</li>
#endforeach
</ul>
#foreach($views as $view)
<div id="{{$view}}">
#include($view)
</div>
#endforeach
</div>
template.blade.php
{{-- formatting stuff --}}
#yield('content 1')
{{-- more formatting stuff --}}
#yield('content-2')
{{-- more formatting stuff --}}
tab-*.blade.php
#extends('template')
#section('content-1')
This is tab [whatever number]
#stop
#section('content-2')
Lorem Ipsum
#stop
The obvious problem here is that, as each sub-view extends the same template, the #yield('content') exists 3 times, and all 3 included sub-views have their own #section('content'). What appears to be happening is that the first sub-view's implementation of the content section is getting placed in all 3 yields.
My intent here is obvious. I want each included sub-view to implement it's own instance of the template, place it's content in the content section, THEN get pulled into the main page. Can this be done?
If I flatten the tab-*.blade.php files and stop using the template, it works fine. The problem here is that I end up with a lot of code repetition that would be better placed in a template. This is an emergency option, but I'd like to do it the "proper" way.
NOTE
I have tried to redesign this to avoid the problem, but I really can't come up with another solution. While #include is typically used to pull in small, static pieces (a header, a footer, etc.), I have not read anything that states this as a restriction. It seems to me that the best approach is to store each view once and pull them into the main page when needed, so that's how I've designed it.
Try using the #overwrite command instead of #stop
Example:
#extends('template')
#section('content-1')
Stuff goes here...
#overwrite
#section('content-2')
More stuff goes here...
#overwrite
Source: https://github.com/laravel/framework/issues/1058#issuecomment-17194530
I came up with a hack that seems to work. In template.blade.php, I use the following:
{{-- formatting stuff --}}
#yield(isset($tab) ? "content-1-$tab" : 'content-1')
{{-- more formatting stuff --}}
#yield(isset($tab) ? "content-2-$tab" : 'content-2')
{{-- more formatting stuff --}}
Then, in main.blade.php, I execute #include('view', ['tab'=>$view]). This seems to cause each instance of content-1 and content-2 do be named distinctly, allowing the 2 subviews to implement the sections separately. This also should allow any other views that implement this template to continue to function without having to change anything.

Blogger: Custom Social Share Links With Page URL

I'm working on a Blogger template with custom social sharing buttons. The problem I'm having is adding the Current Blog URL to the link.
Here is my code:
<li class="googleplus"><span>Google+</span></li>
If I leave it like that, then when I click to share the webpage, it actually shows data:blog.url, instead of the actual webpage URL.
I tried using:
<li class="googleplus"><a expr:href="https://plus.google.com/share?url=data:blog.url"><span>Google+</span></a></li>
However, that just makes the whole menu not appear at all(even omitted from source).
Is there a solution for this, or am I gonna have to use jQuery to grab the URL and insert it into the link?
This is what i use and it works perfectly
expr:share_url='data:post.url'
OR
<li class='google'>
<a expr:href='"https://plus.google.com/share?url=" + data:post.url' onclick='javascript:window.open(this.href, "", "menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600");return false;' rel='nofollow' title='Share this on Google+'>
<strong>
Google+
</strong>
</a>
</li>
use this code :
<a expr:href='"https://plus.google.com/share?url=" + data:post.url.canonical' expr:onclick='"window.open(this.href, \"_blank\", \"height=430,width=640\"); return false;"' target='_blank'><span>Google+</span></a>

Resources