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:

<!-- View stored in resources/views/greeting.html -->
 
<html>
    <body>
        <h1>Hello, {{ $name }}</h1>
    </body>
</html>

Since this view is stored at resources/views/greeting.html, we may return it using the global view helper like so:

Route::get('/', function () {
    return page('greeting', ['name' => 'Hassan']);
});

Rendering Views

You may create a view by placing a file with the .html extension in your application's resources/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 pagehelper:

Route::get('/', function () {
    return page('greeting', ['name' => 'Hassan']);
});

Views may also be returned using the View facade:

use Kiaan\View;
 
return View::page('greeting', ['name' => 'Hassan']);

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:

return page('admin.profile', $data);

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:

use Kiaan\View;
 
if (View::exists('emails.customer')) {
    //
}

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:

return page('greetings', ['name' => 'Mohammed']);

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 pagehelper 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:

View::data(["name"=>"Hassan"])->page("welcome");
View::data("lang", "ar")->page("welcome");

// Or

View::data(["age"=>"24"])->page("welcome", ["name"=>"Hassan"]);

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.

View::global("name", "Hassan");
// Or
View::global(["name"=>"Hassan"]);
// Or
View::global("name", "Hassan", "layouts/*");
// Or
View::global(["name"=>"Hassan"], "layouts/*");

Html string to render

You can display the HTML code as a page.

View::render("<h1>Hello World!</h1>");
// Or
View::render('<h1> {{ "Welcome : $name" }} <h1>', ["name"=>"Hassan"]);

HTML code

HTML code can be extracted from the page after it has been processed.

View::html('welcome');
// Or
View::html('ptofile', ["id" => 1]);ph

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.

View::helper("limit",function($arg, $limit, $symbol='...'){
    return substr_replace($arg, $symbol, $limit);
});

used as follows :

@limit("Hello World!", 12)

{{ "Hello World!", 12 | limit }}

Last updated