Laravel Tutorial Three: View variable passing and blade
This article is original article, without consent, prohibit reprint.
In the previous article we simply said Router,views and controllers workflow, this time I will follow the previous plan, to say the following content:
- Passing variables to the view
- Usage of blade templates
Passing variables to the view
When we're developing Web applications, it's not usually about writing static pages, we need to deal with data, and then the question is, how do you pass data to a view in an MVC framework? For example, we want to ArticleController
index
output a variable in the view of the method, $title
in Laravel, there are several common methods:
Using the WITH () method
public function index() { $title = ‘文章标题1‘; return view(‘articles.lists‘)->with(‘title‘,$title); }
In this case with(‘title‘,$title)
, the first one ‘title‘
is key, the second $title
is the value, so we can articles/lists.blade.php
output this variable in our:
<body><h1><?php echo $title; ?></h1></body>
Refresh our blog.dev
, you can see a page like this:
In the blade engine, we can output variables like this:
<body><h1>{{ $title }}</h1></body>
In fact, in the blade engine, it {{ $title }}
will be parsed like this output <?php echo $title; ?>
, but the symbol here will be the {{ }}
data output, such as you will $title
write:
public function index() { $title = ‘<span style="color: red">文章</span>标题1‘; return view(‘articles.lists‘)->with(‘title‘,$title); }
This time you use the {{ $title }}
output, you will see something like this:
If you want to $title
render the output as a page element, you need to write this:
<h1>{!! $title !!}</h1>
Here {{ }}
and {!! !!}
is blade the most basic usage, these two we will use very much, later I will explain in detail the usage of blade.
Directly to the view () pass parameter
When you use this method, you can write:
public function index() { $title = ‘<span style="color: red">文章</span>标题1‘; return view(‘articles.lists‘,[‘title‘=>$title]); }
Refresh the page and you'll still see the same output. Here's what you need to say if you pass multiple variables, such as:
public function index() { $title = ‘<span style="color: red">文章</span>标题1‘; $intro = ‘文章一的简介‘; return view(‘articles.lists‘,[ ‘title‘=>$title, ‘introduction‘=>$intro ]); }
In the passed array:
[‘title‘=>$title,‘introduction‘=>$intro]
Each key is used as a variable in the view, and value
as the value of the variable. So in the view we need this output:
<body><h1>{!! $title !!}</h1><p>{{ $introduction }}</p></body>
It should be written here {{ $introduction }}
, not {{ $intro }}
.
Using the Compact
This is written using the compact:
public function index() { $title = ‘<span style="color: red">文章</span>标题1‘; $intro = ‘文章一的简介‘; return view(‘articles.lists‘,compact(‘title‘,‘intro‘)); }
compact()
The string can be the name of the variable, and multiple variable names are separated by commas. At this point, pay attention to changing the view's variable output.
These are the methods commonly used in laravel to transfer variables to the view, choose a way you like and stick to this kind of writing, I am using the third kind.
Basic usage of Blade
The above content introduces a little bit of blade syntax, here we introduce blade, say the following a few more commonly used:
@yield()@extends()@if() and @unless()@foreach()
@yield()
And @extends()
often used by the Union, to achieve what we usually call layouts layout: In the process of web development, we put some common parts such as header
, and footer
so on directly in a view file, and then use the use of direct inheritance (使用@extends)
, such as we have in the Under the c6/> folder, create a app.blade.php
:
<! DOCTYPE html><Htmlclass="No-js"lang="ZH-CN" ><Head><Metacharset="UTF-8" ><Metahttp-equiv="X-ua-compatible"Content="Ie=edge" ><MetaName="Viewport"Content="Width=device-width, Initial-scale=1, maximum-scale=1, User-scalable=no" ><Title>laravel 5 Tutorials</Title><LinkRel=' Stylesheet 'href="/css/all.css"Type=' Text/css 'Media=' All '/><ScriptType=' Text/javascript 'Src="/js/all.js" ></Script></Head><Body><DivId="Wrapper" > @yield (' content ')<Navclass="Nav-container Group"Id="Nav-footer" ><Divclass="Nav-wrap" ><Ulclass="Nav container Group" ><Liclass= "Menu-item" > <a href="/"rel=" nofollow "target=" _blank ">laravel 5 Blog </a> </ li> </ul> </div> </ nav></div> </body></ HTML>
Write so some content, which CSS this href="/css/all.css"
, we need to manually create a folder public/
under the folder css/
, and create all.css
this file, for JS is src="/js/all.js"
also the same, these two files are for the back of the page to beautify and prepared in advance.
Note that @yield(‘content‘)
this syntax, here is to say, there is a content
content area, if a page inherits this app.blade.php
, then the page can be dynamically changed @yield(‘content‘)
content. For example we are in articles/lists.blade.php
, we inherit app.blade.php
:
@extends(‘app‘)@section(‘content‘)$title !!}</h1><p>{{ $intro }}</p>@endsection
The first line here @extends(‘app‘)
is to declare that this page is inherited, that is, what app.blade.php
we articles/lists.blade.php
can use all.css
and the all.js
file, and then that is @section(‘content‘)
app.blade.php
the case @yield(‘content‘)
, that is: in the rendering load articles/lists.blade.php
, @yield(‘content‘)
This section will be replaced by the following content:
<h1>{!! $title !!}</h1><p>{{ $intro }}</p>
@if()
This is usually used to determine whether something is displayed in the view based on certain conditions, such as we can "be bored" to try this:
public function index() { $first = ‘jelly‘; $last = ‘bool‘; return view(‘articles.lists‘,compact(‘first‘,‘last‘)); }
In the views file, we use @if()
:
@extends(‘app‘)@section(‘content‘) @if($first == ‘jellybool‘) $first }}</h1> @else @endif@endsection
Refresh to see the output of the page as $last
the value.
Above @if()
, there is a label that can be used @unless()
, @unless()
it can be understood as if( ! )
, that is, the if not
understanding is OK.
@foreach()
Used for cyclic output variables, such as:
public function index() { $first = [‘jelly‘,‘bool‘]; return view(‘articles.lists‘,compact(‘first‘)); }
We pass an array to the view, and then we can use @foreach()
the loop output:
@extends(‘app‘)@section(‘content‘)@foreach( $first as $name) $name }}endforeach@endsection
Refresh the page and you'll see the results of the loop:
For more information on blade, you can refer to the documentation:
Http://laravel.com/docs/5.1/blade
Next section
It seems that the above configuration database link has not been used, do not worry, my next article will be specific to the use of eloquent, this is a very representative part of the Laravel, I hope you can learn a little something.
Happy Hacking
Laravel Tutorial Three: View variable passing and blade