Quick Start
Getting started with the Render Layer is quite simple. The necessary modules and classes are all
present in the \Approach\Render
namespace.
The easiest way to understand the Render Layer is to create a simple HTML page with the Render Layer, however, the render layer is much more powerful, being able to render not just HTML, but also XML, JSON and basically any format you can think of.
So, think of a simple HTML page, with a header, a body and a footer. The header and footer are the same on every page, but the body is different. The body is the content of the page, and it is different for every page.
So, the way you can think of the Render Layer is that it is a way to render the body of the page, and then include the header and footer around it. Sort of like this:
Set Up
Since we will be rendering some good o’l HTML, we will need to set up a few thing first. First, we need to include the
\Approach\Render\HTML
namespace, which contains all the classes we need to render HTML.
Next, we need to create a root element, a root element is the root of the HTML document, and it is the element that
contains all the other elements. In HTML, the root element is the <html>
tag. We can create a root element like this:
Tip: Include
$root->before = <!DOCTYPE html>' . PHP_EOL;
to add the<!DOCTYPE html>
tag to the beginning of the document.
While we’re at it, let’s create the <head>
and <body>
tags, and add them to the root element.
Every HTML element can be extended by adding elements to it, called children. You can do this by extending
the element with the []
operator.
Defining some content
Now that we have some basic structure, let’s add some content to the <head>
and <body>
tags.
First, let’s add in a title, and a stylesheet.
Now, you can echo $head
to see what it looks like.
Since the
<link>
tag is self contained, it doesn’t need a closing tag, so we setselfContained
totrue
.
Now, let’s add some content to the <body>
tag.
Now, you can echo $body
to see what it looks like.
Putting it all together
When you put all this good stuff together, you get something like this:
Now, if you visit index.php
in your browser, you should be able to see your page.