Add content to blade stack - laravel

I have a helper class to draw UI elements. Is it possible, in a method, to add content to a blade #section or #stack?
EDIT:
Here's an example that can clarify what i want to do: say i have some sidebar element that's being built using a #stack('sidebar'). I want to know if it is possible to add to this stack inside some helper class' method:
public function changeSidebar($content)
{
$this->View->addToStack('sidebar', $content);
}
I'm assuming, of course, the existence of the addToStack() method in View.
Thanks in advance.

Related

Customizing Optimizely (Episerver) block outer div css class

Is it possible to add css classes to auto-generated outer div for block element ?
I have a simple Block with a ViewModel and a Controller and I need modify this autogenerated outer div, or optionally remove it?
I looked at other answers to this question where it was recommended ie.
#Html.PropertyFor(model => model.CurrentBlock.ClientLogos, new
{
CustomTag = "ul",
CssClass = "list",
ChildrenCustomTagName = "li",
ChildrenCssClass = "list_item"
})
Problem is when I use this syntax, model has no CurrentBlock property ???
My model is bound to ViewModel, I'm not sure if it affects this. I am very new to Optimizely.
You can use custom tag and css class. In your example CurrentBlock is probably part of the viewModel, you can directly use model.ClientLogs. Alternativly if you want more control you can use editing attributes.
<h1 #Html.EditAttributes(x => x.Heading)>
/* Render the Heading property, unless it it empty, then use PageName as fallback */
#Model.Heading ?? #Model.PageName
</h1>
You can find more information about editing attributes here: https://docs.developers.optimizely.com/content-cloud/v11.0.0-content-cloud/docs/adding-editing-attributes-using-property-web-control

Laravel Blade Template - Put php built-function or maybe laravel helper inside yield

do you ever use PHP built-in function inside blade yield ?
For example can we do something like this :
// master layouts
#yield(ucwords('title'))
// view
#section('title', $title)
Note: $title is from controller
I've already try the first example, but it doesn't work. It doesn't output the $title on my view. Right now I am using this in all of my views
// master layouts
#yield('title')
// view 1
#section('title', ucwords($title))
// view 2
#section('title', ucwords($title))
// view 3
#section('title', ucwords($title))
But I think on second example, I'm not DRY my code because I always repeating the ucwords() on each my view. Can we using it on master layout right on yield declaration?
Thank you guys, have a nice work!
You can make your own blade directive for example if you want to make a #ucfirst() then do something like this in AppServiceProvider .
Blade::directive('ucfirst', function ($expression) {
return ucfirst($expression);
});
Past this into boot()
or on each section() you can extend the main layout #section('title', ucwords($title)) or make the helpers like i mentioned above
as you mentioned above you can use yield()
#yield('title',ucwords(strtolower('Your title')))

how to add common variable for laravel and pass this data to all view all views

I want to create a global variable and pass this into all view, so I can get this variable into all blade template
basically, my need is to pass my general setting controller value into my common blade view like header.blade.php
Thanks in advance
For that you will need to add some code in the App->Providers->AppServiceProvider.php like this:
public function boot()
{
view()->composer('*',function($view){
$settings = Settings::firstOrNew(['id' => '1']);
$view->with('settings', $settings );
});
}
According to Laravel documentation you can use view composer:
https://laravel.com/docs/5.8/views#view-composers
for using this feature in app > AppServiceProvider and in boot method you can use this approach for passing parameter to specific view If you have data that you want to be bound to a view each time that view is rendered. this approach meet your need perfectly. for example you want to pass a parameter named userName to header.
View()->composer('header', function ($view){
$userName= "username"
$view->with(['userName'=>$userName]);
});

Laravel 5 Bind data to var for menu in header.blade.php

What is the best solution for creating a menu.
I have $menudata (dynamic from dbase) and want to pass it to a heade.blade.php to generate top menu.
$menudata = Menu::all();
#foreach($menudata as $value).......
How can i achieve that? What's the best way to do it?
Add below code within the boot method of the AppServiceProvider
View::composer('*', function($view)
{
$view->with('menudata', Menu::all());
});
Here, * means all views will receive $menudata.
Now you can use this like
#foreach ($menudata as $menu).....
If the menu is visible in every page then add the following in any service provider (e.g. in the AppServiceProvider):
public function boot() {
// ...
View::share("menudata",Menu::all());
//...
}
Then you can use the menu data in any view e.g.
#foreach($menudata as $value).......
Laravel-menu provides three rendering methods out of the box. However you can create your own rendering method using the right methods and attributes.
As noted earlier, laravel-menu provides three rendering formats out of the box, asUl(), asOl() and asDiv(). You can read about the details here.
{!! $MyNavBar->asUl() !!}
You can also access the menu object via the menu collection:
{!! Menu::get('MyNavBar')->asUl() !!}

Laravel disable controller action layout

Is there a way to disable layout for certain controller method?
Im using something like $this->layout = null ,yet it still render the layout
The view im rendering obviously have a layout associate with it, i just wonder is it possbile to disable the layout from within controller method, without need to modify the blade file itself
Here is the controller:
class PurchaserController extends \BaseController
{
public function index()
{
$this->layout = null;
return View::make('purchasers.index');
}
}
The view:
#extends('layouts.master')
#section('content')
Content
#stop
Im using Laravel 4
Just remove
#extends('layouts.master')
from your view. That will prevent the view from loading.
Also - if you are using the #extends - then you dont actually need $this->layout() in your controller at all
Edit:
" i just wonder is it possbile to disable the layout from within controller method, without need to modify the blade file itself"
The idea is you do it either entirely from the controller, or entirely from the blade file. Not both together.

Resources