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 are going toArticleController
Ofindex
The view of the method outputs a$title
variables, in Laravel, there are several common methods:
Using the WITH () method
publicfunction index() { ‘文章标题1‘; return view(‘articles.lists‘)->with(‘title‘,$title); }
Such awith(‘title‘,$title)
, the first‘title‘
That's key, the second one.$title
is the value, so that we can be in ourarticles/lists.blade.php
In the output of this variable:
<body > <h1 ; <?php echo $title; ?> </h1 ; </body ;
Refresh our blog.dev
, you can see a page like this:
and in the blade engine, we can output variables like this:
<body><h1>{{ $title }}</h1></body>
In fact, in the blade engine,{{ $title }}
will be parsed into output like this.<?php echo $title; ?>
, but here the{{ }}
The symbol will output the data as you would$title
Written like this:
public < Span class= "Hljs-keyword" style= "color: #333333" >function Index {$title = ' <span style=" color:red "> Articles </span> title 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:
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 this:
public < Span class= "Hljs-keyword" style= "color: #333333" >function Index {$title = ' <span style=" color:red "> Articles </span> title 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 < Span class= "Hljs-keyword" style= "color: #333333" >function Index () {$title = "<span style=" color:red "> Articles </span> title 1 ' ; $ Intro = "Introduction to article One ' ; return view ( ' articles.lists ' , [ = = $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 < Span class= "Hljs-keyword" style= "color: #333333" >function Index {$title = ' <span style=" color:red "> Articles </span> title 1 ' ; $intro = "Introduction to article One ' ; 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.
Laravel Framework page data rendering the use of the HTML compact