官术网_书友最值得收藏!

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:

  1. Go back to the code editor and create another folder inside resources/views, called bands.
  2. 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.
  1. 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.

  1. 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
  1. Go back to your browser and visit http://localhost:8081/bands/, where you will see a result similar to the following:
View template engine
主站蜘蛛池模板: 繁昌县| 建瓯市| 华安县| 贵德县| 凤凰县| 玛曲县| 垣曲县| 威海市| 贞丰县| 新疆| 云龙县| 金塔县| 富顺县| 绵阳市| 东明县| 涪陵区| 奉新县| 广德县| 松阳县| 额尔古纳市| 东乌珠穆沁旗| 集安市| 玉溪市| 顺平县| 保亭| 昭苏县| 保亭| 昆明市| 苍梧县| 隆子县| 敖汉旗| 叶城县| 惠水县| 修武县| 文成县| 三江| 天全县| 安图县| 溆浦县| 彭阳县| 东辽县|