Laravel Views

Laravel provides two different syntaxes to handle your view. 1) you can use View class and 2) you can use view().

As we all know Laravel is MVC framework. Model, view, and controller it is the best technique to separate your web application into different layers such as database logic, business logic, and view.

In Laravel that is blade template engine will help you to manage your view file. The blade is template engine that converts php code to HTML code runtime.

You have to save your each and every view file into resources/views folder.

I am using Laravel 5.3 fresh installation setup. when you open views directory you can notice welcome.balde.php file. You notice that file extension is .blade.php. You have to save you all views file with .blade extension.

To show your view from the controller you can use three different syntaxes:

Three different syntax but that will returns you same output. Of course, you can pass variables to your view file using with() method or using share().

For example, I want to share my $post variable into my welcome.blade.php file.

You can share multiple variables with specific key and value can be anything.

If into your view or blade template file to print $post variable you can use special syntax something like this:

To print any variable you can use above syntaxes.

of course, you can use if condition and for loop and everything that you write into normal PHP file just make sure you stick with blade template syntax:

Sometimes it’s very important to perform some operation into view file such as to calculate variables or print calculated value after some specific operation at you can use php blocks into blade file to process on variable and then you can print it.

For example, I want to plus counter in the loop so I can write code like this:

It’s very important to end every block into view either you will get an exception for syntax. Some of best advantages of Laravel blade is you can extend Layout. You can create one single header and footer and every time you can extend it into your view file.

Laravel cache your view into storage folder. You can clear it by using a command that is:

You can get check best blade syntaxes here. Or you can know more about Laravel views at here.

Leave a Reply

Your email address will not be published. Required fields are marked *