Laravel5 series tutorial 3: View variable transfer and Blade

Source: Internet
Author: User
Laravel5 series tutorial 3: View variable transfer and Blade

From: https://jellybool.com/post/programming-with-laravel-5-blade-views-with-var

Articles: http://segmentfault.com/blog/jellybool

In the previous article, we briefly talked about the workflow of Router, Views, and Controllers. this time I followed the previous plan and described the following content:

  • Pass variables to the view

  • Blade template usage

  • Laravel database configuration and Migration usage

  • Pass variables to the view

    We usually do not develop web applications to write static pages. we need to deal with data. at this time, the problem arises. in a mvc framework, how can we pass data to the view? For example, to output a $ title variable in the view of the index method of ArticleController, there are several common methods in Laravel:

    Use the with () method
    Public function index () {$ title = 'Article title 1'; return view ('Articles. lists')-> with ('title', $ title );}

    In this case, in with ('title', $ title), the first 'title' is the key, and the second $ title is the value. in this way, we can go to our articles/lists. blade. this variable is output in php:

     

    Refresh our blog. dev and you will see a page like this:

    In the blade engine, we can output the variable as follows:

    {{ $title }}

    In fact, in the blade engine, {$ title} will be parsed as an output similar to this However, the {} symbol will output the data as is. for example, you can write $ title as follows:

    Public function index () {$ title = 'Article title 1'; return view ('Articles. lists')-> with ('title', $ title );}

    At this time, you use {$ title} to output the file, and you will see something similar to the following:

    If you want to render $ title as a page element, you need to write it like this:

    {!! $title !!}

    Here {} and {!! !!} It is the most basic usage of blade. we will use these two methods a lot. I will explain in detail the usage of blade later.

    Directly pass parameters to view ()

    When using this method, you can write as follows:

    Public function index () {$ title = 'Article title 1'; return view ('Articles. lists', ['title' => $ title]);}

    Refresh the page and you will still see the same output. It should be noted that if you pass multiple variables, for example:

    Public function index () {$ title = 'Article title 1'; $ intro = 'Article 1 Introduction'; return view ('Articles. lists ', ['title' => $ title, 'insert' => $ intro]);}

    In the passed array:

    ['title'=>$title,'introduction'=>$intro]

    Each key is used as a variable in the view, and value is used as the value of the variable. Therefore, we need to output the following output in the view:

    {!! $title !!}

    {{ $introduction }}

    Here, it should be written as {$ introduction} instead of {$ intro }}.

    Use compact

    Compact is written as follows:

    Public function index () {$ title = 'Article title 1'; $ intro = 'Article 1 Introduction'; return view ('Articles. lists ', compact ('title', 'Intro '));}

    The compact () string can be the variable name. Multiple variable names are separated by commas. In this case, change the variable output of the view.

    The above are several methods commonly used in Laravel to pass variables to the view. you can select a method you like and stick to this method. I am using the third method.

    Basic Blade usage

    The above content introduces a little bit of blade syntax. here we will introduce blade in a unified way and talk about the following commonly used:

    @yield()@extends()@if() and @unless()@foreach()

    @ Yield () and @ extends () are usually used in combination to implement the layouts layout: in the web development process, we put some public parts such as header, footer and so on are directly put in a view file, and then directly inherit (use @ extends) in use. for example, we can create an app under the resources/views/folder. blade. php:

        
         
         
         Laravel 5 tutorial    
         

    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.