Laravel 4 passing data to #include - laravel

I have a view called admin.users. In that view I include header and footer using #include directive. I write all my views-including stuff in routes.php, so for admin.users it is:
Route::get('users', function() {
// ...
return View::make('admin.users')->with('num', $usersNum);
}
And in users.blade.php:
#include('admin.partials.header')
// ....
#include('admin.partials.footer')
Is it possible to pass "users num" to header view in order to show that variable? And is it a good practice the way I'm combining views, because I read about controller layouts but actually I decided to have only rest controllers while I include view only in routes.php (like load static markup and after that communicate with server by ajax)

Have a look at 'ViewComposers' (http://laravel.com/docs/responses#view-composers). This is a great way to share data with your views and keep your routes file clean.
In global.php (or any other place really) add:
View::composer('admin.users', function($view)
{
// Do your $usersNum logic here
...
$view->with('num', $usersNum);
});
If at some point you want this data to be available in admin.dashboard as well, just rewrite to:
View::composer(array('admin.users', 'admin.dashboard'), function($view)

As I said in my comment (and what seems to have fixed your issue) you should create an admin.template view with your header and footer in it, and start your views with #extends('admin.template').

Related

How to make a laravel 5 view composer

I'm still learning Laravel and I'm working on a small project to help me understand better. In the project, I am in need of a global array, so that I may display it or its attributes on every view rendered. sort of on a notification bar, so that each page the user visits, he/she can see the number of notifications (which have been fetched in the background and are stored in the array).
I have done some research, and realized that I have to fetch and compile the array in a view composer I think. But everywhere I go, I cant seem to understand how to make a view composer.
I need to fetch the relevant rows from the database table, and make the resulting array available to each view rendered (I'm thinking attaching it somehow to my layouts/default.blade.php file.). Please help, any and all advice is greatly appreciated:)
You can now inject services on your view
More info here: https://laracasts.com/series/whats-new-in-laravel-5-1/episodes/2
You have to use Sub-Views of laravel blade. I guess your functionality is like a sidebar or like a top bar which will be rendered at every page.
//Your Controller pass data
class YOUR_CONTROLLER extends Controller {
public function index()
{
$data = YOUR_DATA;
return view('YOUR_VIEW_FILE', get_defined_vars());
}
}
//In Your View File
#extends('LAYOUTS_FILE')
#section('YOUR_SECTION')
#include('YOUR_SUB_VIEW_FOR_NOTIFICATION')//You need not pass any data passed all data will be available to this sub view.
#endsection
In your sub view
//Do what ever you want looping logic rendering HTML etc.
//In your layout file just yield or render the section that's it
#yield('YOUR_SECTION')
More explanation can be found Including Sub-Views

How to pass value to layout without passing value from all functions in Laravel?

In my project I am using a layout file and other view files. In my layout, there is a place to display users data. But I using this layout for different pages, so when I uses this layout for each pages, I need to pass user details from corresponding functions to view page. Is there any other short cut to pass user data only once, to layout directly??
Can anyone please reply??
Use Laravel View Composers
Official documentation
Here is an example :
'layout' is your view
View::composer('layout', function($view)
{
$view->with('count', User::count());
});
Put this into your "/app/start/global.php" file

Laravel 4, view composers against #include

I noticed if I pass a second parameter to #include like this:
#include('sidebars.pages', array('categories' => Category::all()))
Then it is possible to replicate the concept of render partials within views and render partials within partials like in Rails.
Do I still need view composers with this functionality?
I appreciate any help!
Try View Composers to bind data to views. Works best for partial views
// View Composer Example
View::composer(array('sidebars.pages'), function($view)
{
$view->with('categories', Categories::all());
});
#include('sidebars.pages')
While that may be possible it's not the documented use of #include. I'd use caution when doing it like that, and personally, I wouldn't be calling a model within your view. Bind the data you require from your route or controller. Make use of a presenter to perform any presentation logic to keep your views absolutely clean.
#include injects all currently defined variables into the nested partial view. So if you bound all the categories to the parent view, then used #include('sidebars.pages'), that view would also have the categories bound to it.

Laravel usage "#layout" in Ajax Requests

Hi there stackoverflow!
In my laravel views I am using a default layout by calling
#layout('layouts.default')
to the same controller I am sending Ajax Requests yet I can't put 'if' to #layout if its a ajax call. Because if ajax request has made to controller it also produce header, footer and content(header and footer are in default layout). So to avoid this I made one copy without #layout of my view.
However its so boring to edit two files for making changes. Can't I add a code to my view something like that?:
#if(!$ajaxrequest)
#layout('layouts.master')
#endif
I want this because my codes in controllers are too messy
A slight variation is to put the logic for the layouts in your main layout template. e.g.
layouts/app.blade.php:
#if (Request::ajax())
#include('layouts.ajax-app')
#else
#include('layouts.default-app')
#endif
Your views just extend the main layout as usual e.g.
#extends('layouts.app')
#section('content')
Content goes here...
#endsection
And then create a default layout file (default-app.blade.php) and an ajax layout file (ajax-app.blade.php). The advantage of doing it this way is that any of your blade templates can be loaded via Ajax, without having to clutter up controller methods with lots of duplicated logic.
You can't have the #layout call after the if statement like that (see the notice in red under "Blade Templating" in the docs. You'll have to set the public $layout and call $this->layout->nest instead of View::make (see "The Basics" on the page linked to above).
You can use something like this in your view template:
#extends( 'layouts.' . (isset($layout) ? $layout : 'default'))
Also apply check in your controller(or Supercontroller) for AJAX request, if it is set $layout variable to needed layout. Otherwise "default" layout will be taken.

How to get view model directly in layout?

I'm working on a practice where I minify most of my javascript in static files, and then each view has something like this:
#section Script
{
#Html.Action("MinifyJavaScript", "Resource", new { viewPath = "~/Views/User/Register.Js.cshtml", model = Model })
}
Which in turn renders the tiny, non-static, pieces of javascript code, like the one below:
#model UserRegisterModel
<script>
(function ($, b) {
$(function () {
b.views.user.register('#Url.Action("ValidateInput", "User")');
});
})(jQuery, bruttijjimo);
</script>
This allows me to cache javascript in views more heavily, since only the parts that vary with the model can change (and are generally treated as partial views)
Now I'd like to further upgrade this practice by removing the need of a layout section, and by convention, render the javascript partial view (which is passed the same model as the view) right after the view. I already created the method to compress the javascript in the view. And there's also the convention that javascript for a view must be in a .js.cshtml file, and share the model with the view.
What I'd need is to grab the model for the view from the layout, and the name of the view, too, and render it there.
This would only work or be needed for the actual view, since partials use a script manager if they need to emit javascript.
So: how can I grab the model for the view from the layout? The rest I can figure out, but this one is eluding me..
So: how can I grab the model for the view from the layout?
#Model should give you what you are looking for.
This being said I didn't quite understand what you are doing but the fact that you have javascript inside your view instead of having it in a separate javascript file and leaving the caching stuff to the browser and reinventing some wheels kind of feels wrong.

Resources