Views

Get started with views in pine

Views are HTML files that use the Leaf template in Pine framework. Templates are just HTML code with added functionality; Think of it as HTML with super powers.

Creating A View

All views are stored in src/views directory. Some views are compulsory such as the views reserved for errors and each view must end with .leaf.html extension.

All HTML files are valid Leaf files as well. A simple view file will be as following

<!DOCTYPE html>
<html>
<head>
    <title>Hello, World</title>
</head>
<body>
    <h1>Hello, World!</h1>
    <p>My very first view in Pine</p>
</body>
</html>

Rendering A View

Once you've a view file at hand, you may want to show it to the user. Pine provides a View class which handles importing and rendering the view.

Let's assume we have a hello-world.pine.html file that we want to render

use Pine\App\View;

new View("hello-world");

Notice how we excluded .leaf.html from the file name. Pine is smart enough to find a template with that name and render it for us. In fact, Pine will throw an error if you supply it the full name.

Generally you would want to render the view through a function, for example

Route::get("/hello-world", function () {
    return new View("hello-world");
});

In this snippet, we are rendering hello-world when the user accesses /hello-world path.

Contribute:

Interested in helping with the docs? Check out our Documentation Repository to get started! From feature suggestions to bug fixes, all contributions are welcome.