View
Introduction
Of course, it's not practical to return entire HTML documents strings directly from your routes and controllers.
Thankfully, views provide a convenient way to place all of our HTML in separate files.
Views separate your controller / application logic from your presentation logic and are stored in the resources/views
directory.
A simple view might look something like this:
Since this view is stored at resources/views/greeting.html
, we may return it using the global view
helper like so:
Rendering Views
You may create a view by placing a file with the .html
extension in your application's r
esources/views
directory.
The .html
extension informs the framework that the file contains templates contain HTML as well as template directives that allow you to easily echo values, create "if" statements, iterate over data, and more.
Once you have created a view, you may return it from one of your application's routes or controllers using the global page
helper:
Views may also be returned using the View
facade:
As you can see, the first argument passed to the view
helper corresponds to the name of the view file in the resources/views
directory.
The second argument is an array of data that should be made available to the view. In this case, we are passing the name
variable, which is displayed in the view using template syntax.
Nested View Directories
Views may also be nested within subdirectories of the resources/views
directory. "Dot" notation may be used to reference nested views.
For example, if your view is stored at resources/views/admin/profile.html
, you may return it from one of your application's routes / controllers like so:
View directory names should not contain the . character.
Determining If A View Exists
If you need to determine if a view exists, you may use the View
facade.
The exists
method will return true
if the view exists:
Passing Data To Views
As you saw in the previous examples, you may pass an array of data to views to make that data available to the view:
When passing information in this manner, the data should be an array with key / value pairs.
After providing data to a view, you can then access each value within your view using the data's keys, such as <?php echo $name; ?>
.
As an alternative to passing a complete array of data to the page
helper function, you may use the data
method to add individual pieces of data to the view.
The data method returns an instance of the view object so that you can continue chaining methods before returning the view:
Sharing Data With All Views
Occasionally, you may need to share data with all views that are rendered by your application.
You may do so using the View
facade's global method.
Html string to render
You can display the HTML code as a page.
HTML code
HTML code can be extracted from the page after it has been processed.
Directive
Template allows you to define your own custom directives using the directive
method.
When the Template compiler encounters the custom directive, it will call the provided callback with the expression that the directive contains.
used as follows :
Last updated