Laravel Framework page data rendering the use of the HTML compact

Source: Internet
Author: User
Tags compact

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 toArticleControllerOfindexThe view of the method outputs a$titlevariables, 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.$titleis the value, so that we can be in ourarticles/lists.blade.phpIn 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$titleWritten 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.