Showing posts with label Basics of blade and views in laravel. Show all posts
Showing posts with label Basics of blade and views in laravel. Show all posts

Tuesday, 31 March 2015

Passing data to views in laravel

In this post I will show you how to pass data to views in laravel.This post is going to be a bridge between the route post and views.We will see how to pass data from routes.php file to views.
To get started lets open up the text editor and open routes.php and fruits.blade.php file( We created this file in the last post)
We will make some modifications in routes.php so that it is able to pass data to views/fruits.blade.php
There are two ways for passing data to views from routes
First one is like this




1
2
3
4
Route::get('fruits',function(){
 $fruitname = "Apple";
 return View::make('fruits')->with('var',$fruitname);
});


here we made use of with method to pass the variable.with() method takes 2 arguments,First one is the string and the second one is the variable.The string defines how you want your variable to be called in the view.In our example we have called it as var.Thus we can access the var value in views
Save the routes.php file.Open the fruits.blade.php and type in the following script
fruits.blade.php




1
2
3
4
5
6
7
8
9
<!DOCTYPE html>
<html>
<head>
 <title>Fruits page</title>
</head>
<body>
{{ $var }} 
</body>
</html>


Save the file and open you browser and browse to localhost/laravel/public/fruits you should get the following output.


Second way of passing the data

In this method we can pass the variable as an array without using the with() method.We can pass value as an array using key value pair.




1
2
3
4
Route::get('fruits',function(){
 $fruitname = "Apple";
 return View::make('fruits', array('var' => $fruitname));
});


The fruits.blade.php page remains the same and the out put too.

We can also pass multiple values using array method by adding key value pairs like



1
2
3
4
Route::get('fruits',function(){
 $fruitname = "Apple";
 return View::make('fruits', array('var' => $fruitname,'var2'=>$somevariable,'var3'=>$someothervariable);
});


And access the value of variable in the view using key.
with() method can also be used to send multiple values



1
2
3
4
Route::get('fruits',function(){
 $fruitname = "Apple";
 return View::make('fruits')->with('var',$fruitname)->with('var1',$somevariable)->with('var2',$someothervariable)
});


Accessed in the same way in views.
Next post will be about sending data using magic methods.

Saturday, 28 March 2015

Basics of blade and views in laravel (Part-1)

In the last post we learned how to create routes in laravel with route parameters but in that tutorial we did not create any view we only returned plain text.In this post I will show you how to create a view for a particular route using blade template engine.Since Laravel is a framework based on MVC architecture we have to follow certain standard format while developing our Laravel Applications
Before we jump on to code lets understand what is a view?
Views typically contain the HTML of your application and provide a convenient way of separating your controller and domain logic from your presentation logic. Views are stored in the resources/views folder



Now lets jump into the code
Open routes.php located in app/Http folder and create a new route.Read more about creating routes.





1
2
3
Route::get('fruits',function(){
 return View::make('fruits');
});


Save the file.Create a new file named fruits.blade.php in the views folder and dump in some basic php stuff and save the file.I just did this



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<!DOCTYPE html>
<html>
<head>
 <title>Fruits page</title>
</head>
<body>
 <?php $var = "Fruits view"; ?>
{{ $var }} 
</body>
</html>

Open the browser and navigate to localhost/laravel/public/fruits(According to my file structure yours might vary).You should get the following output.


What we just did is created a route to fruit view.The function in the route called the static make method of the view class and the argument that it takes is a view.Make sure that a view exists before you create a route otherwise you will be presented with a page full of errors.We named the view as fruit.blade.php we used the blade extension to tell the framework that we are using blade templating engine.You might have noticed that {{ $var }} did the job of echo $var.This is where blade template becomes handy,it makes code clean and tidy and removes the need of those untidy <? echo ?> when you want to print something.Blade template is not limited to only printing on page but it has many uses which we will see in the later posts.