上QQ阅读APP看书,第一时间看更新
Creating the Blade template engine
Now, it's time to create another view component. This time, we will use the Blade template engine to show some records from our database. Let's look at what the official documentation says about Blade:
Blade is the simple, yet powerful, templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. All Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application.
Now, it's time to see this behavior in action:
- Go back to the code editor and create another folder inside resources/views, called bands.
- Create a file, show.blade.php, inside resources/views/bands, and place the following code in it:
<h1>Band {{ $band->id }}</h1>
<ul>
<li>band: {{ $band->name }}</li>
<li>description: {{ $band->description }}</li>
</ul>
You can find out more about Blade at https://laravel.com/docs/5.2/blade.
- Open your browser to http://localhost:8081/bands/1. You will see the template in action, with results similar to the following:
View of the template engine
Note that here, we are using the Blade template engine to show a record from our database. Now, let's create another view to render all of the records.
- Create another file, called index.blade.php, inside resources/views/bands, and place the following code in it:
@foreach ($bands as $band)
<h1>Band id: {{ $band->id }}</h1>
<h2>Band name: {{ $band->name }}</h2>
<p>Band Description: {{ $band->description }}</p>
@endforeach
- Go back to your browser and visit http://localhost:8081/bands/, where you will see a result similar to the following:
View template engine