Adding custom page Templates

Page templates in xScape Framework Themes are working the same way as with any other theme. But, creating template is a bit different. As with all other templates, template has two files: one is loaded by WP, and other one is actual content of the page template.

xScape Custom Page Template Basics

Main template file goes to main theme folder. This file must follow WP requirements for custom page templates. All themes have several such templates built in: archives, sitemap, taxonomy and twitter. Each file for custom page templates is prefixed with ‘tpl-‘. But, as with all theme templates in xScape, these files are only loaders for the actual template. Here is the full content of tpl-sitemap.php file:

[sourcecode language=”php”]<?php
/*
Template Name: Sitemap
*/

global $xscape_filename;
$xscape_filename = basename(__FILE__);

get_header();
xss_content();
get_footer();

?>[/sourcecode]

So, basically, there is no actual content here. Lines 2-4 are REQUIRED for file to be identified as custom page template. Template name must be set, because it will be displayed in template selection boxes. This file, once processed by xScape and WP will use layout manager settings, and for the content it will pull actual template content file from ‘templates/content’ folder. For default custom templates, content files are pre-set, and they have ‘page_tpl_’ prefix. This second file is actual content you want in content area of the page, no sidebars, footer, header or menu.

What you get with this model introduced with xScape is that you can focus on the actual content for custom page template, and not to worry about sidebars, headers and other required elements. Also, layout manager settings will be applied automatically. To see how this works, check out default custom page templates.

Create custom page template

Let’s say we want to create simple page template that will simply display Hello World. We need to make new basic template file. In our case we will call it ‘tpl-helloworld.php’. This file goes in theme folder, and will have exactly same content as in example above, but with two changes. Here is the code:

[sourcecode language=”php”]<?php
/*
Template Name: Hello World
*/

global $xscape_filename;
$xscape_filename = basename(__FILE__);

get_header();
xss_content(‘page_tpl_helloworld’);
get_footer();

?>[/sourcecode]

As you can see, line 3 now have new name for the template, and line 10 is changed to direct xScape to load actual content template file. I recommend that you use the same naming convention as I did here, and with other templates.

Now, you need file with content, and this file goes into¬†‘templates/content’ folder. This file will be called page_tpl_helloworld.php’. Here is the file:

[sourcecode language=”php”]<?php
echo ‘Hello World’;
?>[/sourcecode]

Once you create both files, if you open Layout Manager for the theme, you will see new file in the Templates List: ‘tpl-helloworld.php’. You can set this template any way you want. But, most important, you can use it in the pages. Create new page and assign Hello World template. If you open the newely created page, you will see Hello Word printed as the content.

Upgrade

Minor problem now is that if the theme is upgraded using Dev4Press Updater, you will loose custom templates. So, before upgrade, save both template files, and add them back after upgrade. Future versions of xScape and upgrade plugin will sort this problem and allow keeping of the custom files.

Conclusion

As you see, adding custom page is simple. You can concentrate on the content of the page, and use all xScape features for setting layout.

Comments

Leave a Reply