TooBasic: Layouts
What is a layout?
Well, you know, the part of your page that surrounds your main content an usually stays always the same.
Create a site with layout
Main content
Let's follow an old example an create an action called myaction this way:
- A controller in ROOTDIR/site/controllers/myaction.php:
<?php class MyactionController extends \TooBasic\Controller { protected function basicRun() { $this->assign("helloaciton", "Hello World!"); return true; } } - A template in ROOTDIR/site/templates/action/myaction.html:
<h4>{$helloaciton}</h4> - And visit it, for example, at:
After all of this you'll find a page saying Hello World!.
Nav bar
Let's create another action to emulate a navigation bar and call it mynav:
- A controller in ROOTDIR/site/controllers/nav.php:
<?php class MynavController extends \TooBasic\Controller { protected function basicRun() { $this->assign("hellonav", "I'm a nav"); return true; } } - A template in ROOTDIR/site/templates/action/mynav.html:
<h4 style="color:red;">{$hellonav}</h4> - Why? You'll see.
Layout
Now that you have a main content to show, let's create another controller called mylayout for your layout:
- A controller in ROOTDIR/site/templates/action/mylayout.html:
<?php class MylayoutController extends \TooBasic\Layout { protected function basicRun() { return true; } } - A template in ROOTDIR/site/templates/action/mylayout.html:
<!DOCTYPE html> <html> <head> <title>HELLO</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> {$ctrl->insert("mynav")} %TOO_BASIC_ACTION_CONTENT% </body> </html> - And visit it, for example, at:
Config
As you may see, using a parameter called layout on each URL may not be pretty, therefore you can configure your site by creating/modifying a file in ROOTDIR/site/config.php with this content:
<?php
$Defaults["layout"] = "mylayout";
In this way, you may enter your page just specifying your action.
Also, you may do this to have a default action:
<?php
$Defaults["layout"] = "mylayout";
$Defaults["action"] = "myaction";
And then access this way:
Doubts
What the heck is that?
You've probably seen an strange word/constant/keyword/thing called %TOO_BASIC_ACTION_CONTENT%, this is a keyword you must use inside your template in the place where you want to put your main content.
Insert?
If you look closely to our example you'll find something like
{$ctrl->insert("mynav")}. This sentence "inserts" the results of an action
called mynav. Of course you can import that part with AJAX later on, but in
this way, that part will be add to your layout cache when it's activated.
Wrong layout?
If for any reason you create an action that requires a different layout, you can change it writing something like this:
<?php
class MyactionController extends \TooBasic\Controller {
protected $_layout = "otherlayout";
protected function basicRun() {
. . .
}
}
And if you don't want a layout at all, you may write this:
<?php
class MyactionController extends \TooBasic\Controller {
protected $_layout = false;
protected function basicRun() {
. . .
}
}
Suggestions
If you want, you may visit these documentation pages: