I have an array of items that represent the file and dir structure of a directory on the server.
The $items array is constructed like this:
Array
(
[folder1] => Array
(
[folder1_1] => Array
(
[0] => filenameX.txt
[1] => filenameY.txt
)
)
[pages] => Array
(
)
[0] => filename.txt
[1] => filename1.txt
)
what we want, is essentially <ul> with <li> for every node.
the resulting HTML should be something like
folder1/
folder1_1/
filenameX.txt
filenameY.txt
pages/
filename_1.txt
filename_2.txt
Now, my question has to do with nested includes with laravel's blade templating engine.
I have a view list.blade.php with the following contents
<div class="listing">
#include('submenu', array('items', $items))
</div>
and I pass it the array like this:
View::make('list')->with('items', $items)
the included template (submenu.blade.php) has the following:
<ul>
#foreach($items as $key=>$value)
#if (is_array($value))
<li>{{$key}}/
#include('submenu', array('items', $value))
</li>
#else
<li>{{$value}}</li>
#endif
#endforeach
</ul>
I #include the same template from within itself but with the new data, in case the $value is an array (directory)
First of all, is this at all possible?
If not, is there another way to achive the desired result?
TIA,
Yes, this is indeed possible.
However, there's an issue in your includes, you have:
#include('submenu', array('items', $value))
It should be:
#include('submenu', array('items' => $value))
It's worth noting also another hidden blade statment, #each. You can use this instead of looping through the array yourself, something like this:
<ul>
#each('item.detail', $items, 'item')
</ul>
Then you create a new blade file named item.detail and pop what you previously had in your loop in that file. It helps to clean up your view from having more and more nested loops.
The data for the item when you are inside your new blade file will be held in the third parameter, in this case $item
Instead of using array, use an eloquent collection. Instead of using #include, use \View::make. It cleans up the code a bit. Here is an example drop down menu for the Foundation 5 framework, using an eloquent model with parent/child relationship:
My model has a parent->child relationship
public function children() {
return $this->hasMany('Category', 'parent_id');
}
I generate my results like so in my controller
$categories = \Category::where('parent_id', '=', '0')->with('children')->get();
Blade template: _partials.dd-menu.blade.php
<ul class="{{$class}}">
#foreach($items as $item)
<?php
$active = $item->id == \Input::get('category') ? 'active' : '';
$hdd = $item->children->count() ? 'has-dropdown' : '';
?>
<li class="{{$hdd}} {{$active}}">
{{$item->name}}
#if ($item->children->count())
{{ View::make('_partials.dd-menu')->withItems($item->children)->withClass('dropdown')}}
#endif
</li>
#endforeach
In your parent blade:
<nav class="top-bar" data-topbar role="navigation">
<ul class="title-area">
<li class="name">
<h1>Categories</h1>
</li>
<!-- Remove the class "menu-icon" to get rid of menu icon. Take out "Menu" to just have icon alone -->
<li class="toggle-topbar menu-icon"><span>Menu</span></li>
</ul>
<section class="top-bar-section">
<!-- Right Nav Section -->
{{ View::make('_partials.dd-menu')->withItems($categories)->withClass('right')}}
</section>
</nav>
Related
see this apple icons, these are rendering from foreach loop in view, defined variable in controller . i can change each icons but not able to assign link to each icon.
Hello guys i am stuck in linking using laravel controller.
my code is something like that: (i have a service page which has sub services page there is only one view which is using controller variable to showing data. i want each page button have different links. links can be static so i can putt it from controller. dont have any database attached to this solution. i want each href as a separate link)
This is view:
<?php $index=-1; ?>
#foreach ($third_fold_3_technologies_list as $item)
<?php $index++; ?>
<span class="{{ $item }}"></span>
#endforeach
This is controller:
$third_fold_3_technologies_list = [
'icon-appleinc',
'icon-appleinc',
'icon-appleinc',
];
You can use associative array.
$third_fold_3_technologies_list = [
'http://<link1>' => 'icon-appleinc',
'/<link2>' => 'icon-appleinc',
'<link3>' => 'icon-appleinc',
];
Then in your view
#foreach ($third_fold_3_technologies_list as $link => $class)
<span class="{{ $class }}"></span>
#endforeach
I have some navigation links and I want to get the current route in laravel 5 so that I can use the class active in my navigations. Here are the sample links:
<li class="active"> Home </li>
<li> Contact </li>
<li> About </li>
As you can see, in the Home link there is the active class which means that it is the current selected route. But since I am using laravel blade so that I would not repeat every navigation links in my blades, I can't use the active class because I only have the navigation links in my template.blade.php. How can I possibly get the current route and check if it is equal to the current link? Thank you in advance.
$uri = Request::path()
Check out the docs
You can use named routes like
Route::get('/', ['as'=>'home', 'uses'=>'PagesController#index']);
and then in the view you can do the following
<li {!! (Route::is('home') ? 'class="active"' : '') !!}>
{!! link_to_route('home', 'Home') !!}
</li>
So I got it working with some little tricks. For example the url in the address bar is http://localhost:8000/tourism/places.
If I will use the Request::path() method, I will get tourism/places. But what I wanted to get is the tourism part only so what I did is this:
$currentPath = Request::path();
$values = explode("/", $currentPath);
$final = $values[0];
Now the $final variable will have the value "tourism".
Now to make the variable load globally, I included the code in the AppServiceProvider.php inside the public function boot().
public function boot()
{
$currentPath = Request::path();
$values = explode("/", $currentPath);
$final = $values[0];
view()->share('final', $final);
}
And lastly in my blade template,
<li #if ($final== "tourism") class="active" #endif>
Places
</li>
Simple way is always better, just use the same function you use to generate links and it will give you all, including the flexibility to use wild cards
<a class="nav-link {{ Request::url() === route("products.list", [$group_key]) ? 'active' : '' }}"
href="{{ route("products.list", [$group_key]) }}"
>
LInk text
</a>
Plus, you still are going to have only one place to manage URL-s - your router files.
This question is in relation to: Group by in Laravel 5
Essentially, what I have have is a series of "Groups" and each group can have a as many child groups as they want. So it could look something like this:
Group 1
Group 1 Child
Group 1's child
Group 1 1's Child
Group 2
At the moment, I am only able to display the group, and their children, using:
<ul>
#foreach($contents as $content)
<li>{{$content->title}}</li>
#if($content->children->count() > 0)
<ul>
#foreach($content->children as $childContent)
<li>{{$childContent->title}}</li>
#endforeach
</ul>
#endif
#endforeach
</ul>
If I want to add more layers of children, I have to code this, with another if statement as well as a foreach statement. Obviously this is not practical when a group has n^3,... number of children.
Is there a dynamic, while-loop approach to solving this problem? Any help would be greatly appreciated!!
What you need is a recursive partial - a one that would load itself as long as there are some more group levels to be displayed.
// list_group.blade.php
<li>
{{ $content->title }}
#if($content->children->count() > 0)
<ul>
#foreach($content->children as $childContent)
#include('list_group', array('content' => $childContent))
#endforeach
</ul>
#endif
</li>
//in your template
<ul>
#foreach($contents as $content)
#include('list_group', array('content' => $content))
#endforeach
</ul>
I have an array which I created in php with a recursive function, I do not know how many dimensions, how can I use in Smarty ?
I trying use this code :
{foreach $myArr as $items}
<li>
{$items.title}
{if $item.submenu}
<ul>
{foreach $items.submenu as $items2}
<li>{$items2.title}</li>
{/foreach}
</ul>
{/if}
</li>
{/foreach}
But this code is for just 2 levels, may be my array have 3 or 4 or ... levels.
UPDATE:
I found the solution, in my solution I use Smarty functions :
{function name=menu level=0}
<ul>
{foreach $data as $items}
<li>
<a href="{$items.url}">
{$items.title}
</a>
{if is_array($items.submenu)}
{menu data=$items.submenu level=$level+1}
{/if}
</li>
{/foreach}
</ul>
{/function}
{menu data=$menuItems}
I would go a different approach.
My suggestion:
Instead of creating arrays with menus. Create two or one class/es.
First class is a Menu class which holds the items in a key/value manner.
Second class would be a menu item.
That way you could all necessary iterations/logic do through a function call in the model
and the printing in the view.
something like:
class Menu {
protected $_items = array();
protected $_parent_child = array();
public function add_menu_item($name, $id, $parent_id) {
if (array_key_exists($id, $this->_items))
return;
$this->_items[$i] = array('name' => $name, 'id' => $id, 'parent' => $parent_id) // model with data
$this->_parent_child[$parent_id] = $this->_items[$id];
}
public function get_nodes_by_parent($id=null /*for root*/) {
if (array_key_exists($id, $this->_parent_child))
return $this->_parent_child[$id];
return array(); // or null or something
}
}
Menu template:
{foreach($menuClass->get_nodes_by_parent() as $item ) {
<ul>
<li>{$item['name']}</li>
{include 'sub_cats' id=$item['id'], menuclass=$menuclass}
</ul>
{endforeach}
Sub category template:
{foreach($menuClass->get_nodes_by_parent($id) as $item ) {
<ul>
<li>{$item['name']}</li>
{include 'sub_cats' id=$item['id']}
</ul>
{endforeach}
This is not fully complete nor tested. But that would be my approach.
I want to build a menu from an array in Laravel. What I'm currently doing is putting the array in a view
$menu = ['home', 'users' => ['create users' , 'update user', 'activity log']];
and then looping through it to generate the menu:
<section>
<!-- Left Nav Section -->
<ul class="left">
<li class="divider"></li>
#foreach($menu as $key => $nav)
<li class="has-dropdown">
{{ $key }}
<ul class="dropdown">
#foreach($nav as $subnav)
<li>
{{ $subnav }}
</li>
#endforeach
</ul>
</li>
#endforeach
</ul>
</section>
Is there any other way that I can achieve the same result without putting the data in the view?
I also tried creating a constructor function in the controller:
public function __construct() {
$menu = ['home', 'users' => ['create users' , 'update user', 'activity log']];
return $menu;
}
But I guess that is not how it works. I appreciate any ideas on how I can go about this. Thanks in advance
View composers to the rescue!
They are executed every time before a view is rendered, so you can use this to pass standard data to them.
If you're using controller layouts you can bind data to the layout from within the constructor. Just make sure you call the parent constructor first so that the layout is instantiated properly.
public function __construct()
{
parent::__construct();
$this->layout->menu = ['home', 'users' => ['create users' , 'update user', 'activity log']];
}
That will bind a $menu variable to the layout, and will also be available to any nested views that are used with Blades #include.
Have a look at view composers:
http://www.laravel.com/docs/views#view-composers