Adding pages with custom templates

When you start polishing your pages, you might have a need to either add some static pages with just a text or change the layout of your dynamic pages. Both tasks can be easily done by using page templates

Because you do not add "Page" object yourself, you can't specify 4th argument. You will need to know how Application is loading those templates. Behaviour may vary depending on the API used.

Page Template handling in ApiFrontend

ApiFrontend is most popular API and is used for public applications as well as administrative back-ends. ApiFrontend determines page by the URL used. For example if the URL which was used is http://yoursite.com/disclaimer/moreinfo.html the page will be "disclaimer/moreinfo".

Static Pages

Static pages does not have their own class defined. They will be initialized using prototype of default "Page" class. You can globally change this by setting $api->page_class property.

If page/disclaimer/moreinfo.php was not found, then ApiFrontend will attempt to load template from templates/default/page/disclaimer/moreinfo.html. Actually it will look into atk4 folder as well as "shared" skin there.

Create such a file and put all the text you want in there. Do not forget to use a place-holder for ID in the same way how we did it for views in previous chapter.

        

<div id="<?$_name?>">
  <p>
    disclaimer text here........
  </p>
</div>

Always use <div> with id of _name on pages. You may at some point to dynamically load pages and if elements in different pages do not match, there might be some confusion.

You can redefine $api->loadStaticPage if you wish that static pages are loaded differently.

Catch-all pages and 404

If page class can't be loaded and template is not found method $api->pageNotFound() will be called as a last resort. By default this method throws exception, but you can use it to either load and display or redirect user to a 404 page. Below are examples for both.

function pageNotFound($e){
    
$this->api->redirect('notfound',array('orig'=>$this->page));
}
function pageNotFound($e){
    
$this->page_object=$this->add('page_NotFound');
}

Pages with Template

Page class is derived from view, therefore it has many thing in common. One thing is that it also uses defaultTemplate() method. You can redefine this method to explicitly define which template to use for the page.

function defaultTemplate(){
    return array(
'page/disclaimer/moreinfo');
}

This will allow you to combine dynamic content with your own template. Inside this template you most probably would need to have <?Content?< region, where objects are outputted by default. You can however use other regions as long as you remember to specify 3rd argument to any $page->add() calls on your page.