Learning Laravel: Eyeing Up Views

Originally posted on nathanheffley.com

Now that we’ve started learned how to set up some basic routes (go check out Really Radical Routing
if you haven’t created any routes yet), lets give our site visitors something to look at that’s a bit nicer than some plain text

Views will allow you to use a templating engine called Blade to create HTML pages with ease. You can check out the default page that gets created when you first make a Laravel project inside resources/views/welcome.blade.php

Within that file, you’ll see a basic HTML page with some Blade-specific content. Here’s the file with some of the extraneous content taken out:

getLocale() }}">
@if (Route::has('login')) @endif

The first example of some blade templating is encountered within the first two lines. The most basic way to use Blade is to simply use it as a way to output the value of some PHP code to the page. The way that you let Blade know that you want to evaluate a snippet of PHP code and put the result directly into the HTML code is to use double curly braces.

getLocale() }}">

Blade will encounter this line, evaluate the code within the curly braces (in this example it’s a call to a function) and then output the result of that function into the lang
attribute of the HTML tag. In this example, app()->getLocale()
is a special Laravel function to get, of all things, the current locale.

Further down in the file, we encounter something a little more interesting.

@if (Route::has('login'))

If you’re an astute reader, you may have already guessed that is an if
statement just like you would use in regular PHP code. This is simply checking to see if a login
route exists. Anything that is within one of these @if/@endif
sections will only be output/evaluated if the conditional is true. If you want to get technical, in Blade templating speak, this is called an “if directive”. Any time you see an @
symbol with a keyword in a Blade file, you’re looking at a directive.

These directives are what make Blade really powerful. In a future post we may look into creating our own directives, but for right now we’ll look at a special one that Laravel offers out of the box:


This directive checks to see if the current user is authenticated (logged in) to the site. Normally you would have to use an if statement to check if the user is authenticated, but this handy-dandy directive hides all that messy code behind and nice simple @auth
keyword. Because this is basically an if
statement in disguise, you can use else
just like you normally would. These directives are what gives Blade some of the magic that makes it much nicer for templating than straight PHP code.

Now that you understand what curly braces and directives mean in templates, let’s create our own view to use in place of that boring “Hello, World!” from the last article.

Create a new file in the resources/views
directory called layout.blade.php
and inside of it we’ll setup the outline of any HTML document.

    Learning Laravel

This @yield
directive is one of the more important directives that you’ll be using when creating Blade templates. It allows you to extend
this template file and place more code right where the @yield
is. The parameter that yield
takes allows you to create multiple sections and place them in different places. You can have as many yield
directives as you want as long as they have different names. Pretty nifty! Now let’s create the actual view for our route. Make one more file in resources/views
and name it hello.blade.php



Hello, World!


Here we use the @extends
directive to tell Blade which file we want to extend
and because Blade is so clever, it knows that all template files will end in .blade.php
so we can leave that bit off and just give it the first part of the filename. Then we use the @section
directive and pass it the same value that we gave to the @yield
directive in the layout file. This is how Blade knows where this section belongs in the layout template.

Now that we have our very own view created, let’s go back to the routes/web.php
file and open it up again. Go to where we defined our /hello
route in the previous lesson and change it so that we return a view instead.


Route::get('/hello', function () {
    return view('hello');

Just like Blade, Laravel knows that our file will end in .blade.php
so we can leave that off. Now if you save that and check out your site’s route (most likely at localhost:8000/hello
) you should see an actual HTML page with an h1
instead of just plain text.

You now how everything you need to create a site with Laravel! You can create routes, display actual HTML pages, and even do a little bit of programming logic if you want. But putting code in the route file isn’t a great way to keep things organized, and just returning basic pages isn’t very interesting. In the next lesson we’ll look at a brief overview of the project we’ll build together to learn how you can use Laravel to build real-world projects. Make sure to check back so you don’t miss it!

The Practical Developer责编内容来自:The Practical Developer (源链) | 更多关于

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合技术 » Learning Laravel: Eyeing Up Views

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录