Blade template yield with default include - php

Is it possible to yield to another view but then default with an include statement?
Example:
#yield('header', #include('partials.header'))
The intended action is that if the subview does not include the header, then the current template will include the partials.header view.

It is possible. But you can't use blade tags inside blade tags. What you can do however is use View::make() instead of #include. #include actually compiles to a make call on the view factory.
#yield('header', View::make('partials.header'))

Related

Not having to extend and section in every file Laravel/Blade

Is it possible to make every view extend my main layout without having to put #extends('layout') and #section('content') in every file at the top? For example by using a provider? I'm new to Laravel and Blade.
Actually its better to use extend but in case you want to get rid of #extends or #section, you can always return your main layout from controller and pass views as parameters to the main layout.
mainlayout.blade.php:
//replace yield directive with content variable
// #yield('content')
{!!$content!!}
in controller:
//return view('test');
$content=view('test')->render();
return view('mainlayout',['content'=>$content]);
Now in test view you don't need #extends and #section.

Addd partial blade view template to all views?

I have a partial view with a script I want to include in all my views, I dont't wanna #include this partial in all my layouts and blade views but instead add it to all rendered views, my partial looks like this:
<script type="text/javascript">window.$app = {!! json_encode(app(App\Helpers\Javascript::class)->app) !!};</script>
How can I do this?
You could:
include it in your main(s) layout template (the one(s) handling <html> and <body> tags)
you could use a Laravel View Composer to add the JSON data to the required views. Those views would so include the JSONed data, that you'd echo in the right place to be handled by your JS.
you could check the Php-Vars-To-Js package that allows you to put some data from PHP in a custom JS namespace (eg window.yourNameSpace). And then use it from a View Composer or the specific controller's methods concerned by your views.

How to include a view.blade.php file in Laravel?

I have this file sidebar.blade.php inside views/content, I want to include this (sidebar.blade.php) file to my views home.blade.php.
Did you look at the docs?
Blade's #include directive, allows you to easily include a Blade view
from within an existing view. All variables that are available to the
parent view will be made available to the included view:
#include('shared.errors')
Even though the included view will inherit all data available in the
parent view, you may also pass an array of extra data to the included
view:
#include('view.name', ['some' => 'data'])

Is it possible to get the view / layout name in a Laravel (4) blade template

I have a layout in which I want to add classes to the body depending on which view is being displayed, i.e.:
<body class="layout-default page-index">
I can do this in Twig quite easily (OctoberCMS uses Twig) but I can't see a way to do it with Laravel's Blade templates (which I prefer anyway).
I'd rather not have to pass a variable to every View::make with the view name as this seems redundant.
Good question, very smart way to work with css.
You would use this typically by adding classes to the body tag, or the main container div.
within your routes or filters file:
View::composer('*', function($view){
View::share('view_name', $view->getName());
});
Within your view:
<?php echo str_replace('.','-',$view_name);?>
<?php echo str_replace('.','-',Route::currentRouteName());?>
These should get you everything you need.

What is Layout and what is View in ZF? When and whose variables should I use and why?

I can't understand when to use Layout's variables and when to use View's variables to get page segments on the page. Here is the picture form their Layout package tutorial ($this means the View instance everywhere):
Why Navigation, Content and Sidebar segments are got as Layout variables?
$this->layout()->nav;
But HeadTitle, HeadScript, HeadStylesheet are got straightly from View?
$this->headTitle(); // I know that this is a placeholder view helper.
// But this segment of the page logically belongs to Layout.
// and it has to be called smth like view->layout->placeholder
And why Header and Footer are from some partial method of the View but not Layout's properties?
$this->partial('header.phtml');
I've tried to change them and both ways work fine:
echo $this->nav; // I assigned navigation segment script to the View and it works;
I tried to assign Footer segment script to the Layout and it also works:
$layout->footer = $footer;
echo $this->layout()->footer; // it also works, it's displayed on the page
Any of the ways may be applied to any variable on the page. For example in Navigation segment I have a lot of variables to display and I can output them using both ways - one variable as Layout's property, another one sa View's property.
So what is the rule to use them right way? When should I use View's variables and when Layout's ones?
I agree that this isn't very clear from the documentation, and I don't think $this->layout()->nav is explained at all. A few points that might help:
$this->layout() is actually a call to the layout view helper, which returns the current instance of Zend_Layout.
Zend_Layout registers its own placeholder helper (with the key 'Zend_Layout'), and by default creates a 'content' variable in this.
the Zend_Layout class has a magic __get() method which proxies any member variable calls over to its registered placeholder container. So calling $this->layout()->content is another way of writing $this->placeholder('Zend_Layout')->content
the Zend_Layout class also has a magic __set() method that proxies stored data to the placeholder class. So $layout->footer = 'foo' is the same as calling $this->placeholder('Zend_Layout')->footer = 'foo' in the view
With that in mind:
Why Navigation, Content and Sidebar segments are got as Layout variables?
As these are accessing data stored in Zend_Layout's placeholder. You could also use $this->placeholder('Zend_Layout')->content
But HeadTitle, HeadScript, HeadStylesheet are got straightly from View?
These are view helpers.
And why Header and Footer are from some partial method of the View but not Layout's properties?
This is the standard way of accessing content from other templates.
In general, assume that using the view object is the correct way to access the data. Use the layout object/helper only if you know the data is in the layout placeholder.
The advantage of using placeholders over partials is that you can access and modify them in several different places, including in the view itself. For example say you had a sidebar which is stored in a partial. If you were to store this in the Zend_Layout placeholder instead (for example in a controller plugin), you can then override this for certain actions in the controller:
public function someAction()
{
$this->view->layout()->sidebar = 'Some other sidebar content';
}
or in the view script itself:
<?php $this->layout()->sidebar = 'Content for this page only'; ?>

Categories