7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
FREELessons: 21Length: 2.7 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.7 Blade Essentials

In this lesson we'll investigate the blade templating engine. It's exclusive to Laravel and quite similar to Twig. With Blade you can create beautiful view code while still maintaining the flexibility of a vanilla PHP file. It has tags to echo variables, escape variables, and run control statements.

2.7 Blade Essentials

Hello! and welcome back to another episode of Getting Started with Laravel 4. In the previous video, we had a look at resource controllers and the rest pattern. Now, in this lesson, we'll have a look at the Blade Templating Engine. Which is exclusive to Laravel and quite similar to Twig. With Blade, you can create beautiful view code while still maintaining the flexibility of a Vanilla PHP file. Okay, now, we'll take the user controller that we created in the previous video and create a Blade view for that. Well, first we need some data to work with. Inside of the index method we'll just create a bunch of users. Now the first thing I'll do is set a variable of users and make that equal to an array. And then we'll just create a loop to fill that array. So something like for ($i = 1; $i $1++). Now, we'll just use this for statement to create three users with. now, our new user will be a class. Will do new standard class. And this will create an empty class that we can store into user. Now, of course, we want to set a user email, so we'll set a property email and make that equal to a string. Maybe "user{$i}@tutsplus.com". Now we'll also need a $user->password, so we'll set a property for that as well. And that will be = "MySecretPassword{$i}", like so. Now as you know, it's bad practice to ever work with plain text passwords, so I'll just cut this and run it through Laravel's. Hash make method, which will turn it into an encrypted password. Now, all that's left for us to do is take this user and add it to the users array. So now that we have our three fake users set up, let's see if we can create a view and display the users there. Well like always we'll just do return view make. And then it would probably be a good idea to have some kind of a user subfolder. And then just create a view with the method name. Now we could pass the users to the view like we've done before, but we'll use different method this time. As a second parameter to the view make method, I'll just pass compact and then add in a string of users. And that will have the same effect. Now, notice that this is way too much business logic to put in any controller. And normally, you would delegate this to a model, but as we haven't touched the subject of models yet, we'll just do it like this. Okay, time to close this out. And create a view. We'll start by creating the sub folder called User. And then inside of that folder we'll create a new file called index.blade.php. And watch this extension, this is what tells Laravel. To use the blade templating syntax, okay. Now the first thing I'll do is paste in an html5 snippets like so, and we'll give that a title users. And now let's display the email addresses for our users, now we'll start with Vanilla PHP and then gradually work our ways. Towards the blade syntax. Okay, now one way these templates are typically built up is by typing a php start and end tag and then running a foreach statement. Okay, now if all goes well then we should have a users variable present so we'll do for each users as user. And then of course we need to end that statement as well, and for each and then within that for each loop we will write a paragraph tag and do something like echo user email, [INAUDIBLE] will give us the email property for the current user. Okay so we have a controller in place, we have a view in place, lets see if our route is in place, yes there is a resource route right here. So if I go to the browser and load public user, then sure enough, I am getting these three email addresses. Okay? So now let's see how we can clean this up. Well for instance, I can replace php echo with curly braces. So replace the opening tag, and the closing tag. And now I just need to remove that semicolon as well. And there you go, that gives us the same results in the browser. Now that's much cleaner, isn't it? You see, behind the scenes, Laravel actually translated the double braces into a php echo statement. But what about escaping your output? Does Laravel help there? Why indeed if you wrap this into triple curly braces then your output is automatically, properly escaped. Well of course you can't see that on simple email addresses but let's try a bit of malicious JavaScript. I'll just splice in a little snippet here. Here we are running a string containing a script tag and an alert method through double braces, so these should not be escaped. And sure enough, that JavaScript is actually executed, so now what if we wrap this into triple braces? Well that runs it through an escape method. Rendering the JavaScript harmless. Okay, let's go back to our code. Let's just see if we can clean this up a little better. Well we can because Blade also enables you to run common code flow statements. Here we can replace the php tags from the first line by proceeding with an @. And then just stripping out the rest. And we can do the same for the last line. Strip out php, replace it with @, and strip out the closing tag as well. And now that really cleans it up. You know, I'm having so much fun, let's do another one. Let's say that if and only if our users array actually holds any users then we want this code to be run. Just close that out like you would expect with @endif, and check that out. Still running. And Blade can even do @unless. Which does exactly the opposite. So now this code is only run if this expression returns false, see? Well in our case that's not very helpful, so let's just change that back like so. And do one more. Blade can also help you with common is set statements. Yeah, well you're probably all too familiar with this type of code where you want to echo a variable. First you have to check if that variable is actually set, and then if it is. You can safely echo that. But if it's not, then you need to echo a default value. Well with Blade, you just do this. You've got your braces, you just echo user email, or you'll echo a default value instead. And that takes care of that entire segment. Okay. That's it for the basics of what you can do inside of a Blade template. In the next video we'll use Blade to create reusable templates, containing content, and sections, and partials. Very cool. I'll see you there.

Back to the top