Laravel blade section - php

I just get to know that we can write section with this:
#section('sectionname','data')
What is the difference between the above method with the below method?
#section('sectionname')
'data'
#stop
Is it a proper way to write with the first method? Although, during my testing, it does not need #stop for the first method. But is it the best practice that we need to put it?
Thank you.

For writing small string in the sections you can use
#section('sectionname','data')
and to write multiple lines of html you will use
#section('sectionname')
'data'
#stop

Related

is there a way to nest {{}} expressions in Laravel template?

I have a resource route generated by Artisan command as
Route::resource('posts','PostsController');
the URI of this route is posts/{post}/edit which require a dynamic value in the middle. But because I'm using the url() method to link all of my routs I am forced to nest the template expression as:
Edit
This is giving me an error: NotFoundHttpException because it couldn't get the {{$show->id}} part. How can I fix this?
In PostsController, create a new variable that you want to show up in the template. You should be able to use the url() function within a controller. (See here) Pass this value to the template like you normally would.
Just in case: documentation on how to pass values to a template
Use Edit with out the use of {{}} notation.
You need double quote and remove one bracket
Do you looking for something like this?
#foreach($posts as $post)
Edit
#endforeach

Laravel Blade variables in view

I am working on the front end of a Laravel project and I can change all the values in the view templates. I can probably modify other files as well, but, as I don't yet grok Laravel Blade fully, and I have a time constraint, I'd prefer not to make life harder for myself.
What I want to do is output some data related to the current route, and retrieve and parse some data from the Resources/lang/values.php file. Can I do this within the view without inserting a bunch of messy php? Is this a stupid thing to do? Is their a best practice for this?
Thanks in advance.
Use __() or trans() helpers or #lang Blade directive to work with language files:
{{ __('values.some_string_from_values_language_files') }}
Or:
#lang('values.some_string_from_values_language_files')
These helpers will work only if values.php is in:
resources/lang/en/values.php
resources/lang/fr/values.php
....
An answer to your question about best practices is no, you shouldn't reinvent the wheel and keep language files in a standard directory.
To get current route data, use Route facade and these methods:
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
https://laravel.com/docs/5.5/routing#accessing-the-current-route

Laravel Blade pass array data to multiple extends

I've searched for this on internet alot, but wasnt able to find answers. I'm using Laravel 5 and I've little issue with blade templating as in my project I need sometime to do multiple extends and I need to pass all data from one layout to all master layouts "extends"
Nested page example :
#extends('layouts.full', ['var' => 'key'])
#section('page')
page content here
#stop
layouts/full.blade.php example
#extends('app', ['need to pass same data here too'])
#section('content')
#yield('page')
#stop
and app.blade.php is just main html stuff
And I wanted to ask is there possibility to pass same vars without setting global variable like?
#extends('layouts.full', $data = [])
I think this might help you :
https://laravel.com/docs/5.2/blade#service-injection
to pass multiple variables, use an array:
#extends('layouts.full', [ 'data' => ['var' => 'key'] ])
as for multiple extensions, maybe just use include statements in layouts.full :
#include('header')
#section('content')
#yield('page')
#stop
#include('footer')

Laravel Blade: Something similar to Jinja2 block

Is there an existing way in Blade to mimic Jinja2's block construct?
I understand it is possible to use #yield('content', '<h1>Default content</h1>'), but this is clunky and ugly if you want the default content to be much bigger than a small string.
Yes, you can use #section/#show instead of #yield:
#section('content')
default content
#show
and override as usual:
#section('content')
override
#stop

Laravel 4 Blade #include variable

I was trying to do include with Laravel blade, but the problem is it can't pass the variable. Here's my example code:
file_include.blade.php
<?php
$myvar = "some text";
main.blade.php
#include('file_include')
{{$myvar}}
When I run the file, it return the error "Undefined variable: myvar". So, how can I pass the variable from the include file to the main file?
Thank you.
Why would you pass it from the include to the calling template? If you need it in the calling template, create it there, then pass it into the included template like this:
#include('view.name', array('some'=>'data'))
Above code snippet from http://laravel.com/docs/templates
Unfortunately Laravel Blade engine doesn't support what you expected!.But a little traditional way you can achieve this!
SOLUTION 1 - without Laravel Blade Engine
Step a:
from
file_include.blade.php
to
file_include.php
Step b:
main.blade.php
<?php
include('app/views/file_include.php')
?>
{{$myvar}}
SOLUTION 2 with Laravel Blade Engine
routes.php
$data = array(
'data1' => "one",
'data2' => "two",
);
View::share('data', $data);
Access $data array from Any View like this
{{ $data['data1'] }}
Output
one
Blade is a Template Engine for Laravel. So try passing the value from the controller or you may define it in the routes.php for testing purposes.
#include is used to include sub-views.
I think you must understand the variable scope in Laravel Blade template. Including a template using #include will inherit all variables from its parent view(the view where it was defined). But I guess you can't use your defined variables in your child view at the parent scope. If you want your variable be available to the parent try use View::share($variableName, $variableValue) it will be available to all views as expected.
In this scenarion $myvar would be available only on the local scope of the include call.
Why don't you send the variable directly from the controller?
I suggest you do a classic PHP require if you really want to change your variable (then it's automatically by reference)

Categories