Skip to content

Creating frontend templates with variants

Bianka Martinovic edited this page Aug 1, 2013 · 5 revisions

BlackCat can handle templates with variants. This means, you may create ONE template which has variants for home (welcome) page, news pages, gallery pages, WYSIWYG pages, and so on.

Note: Don't provide TOO much variants, this might irritate the user.

Folder structure

Every template should have the following directory structure, even if it does not provide variants by default:

./templates
           /<templatename>
                          /index.php
                          /css
                                    /default     default variant
                          /templates
                                    /default     default variant
                                            /index.tpl

For every variant, create a subfolder with the name of the variant, like this (example):

./templates
           /<templatename>
                          /index.php
                          /css
                                    /default     default variant
                                    /gallery
                          /templates
                                    /default     default variant
                                            /index.tpl
                                    /gallery
                                            /index.tpl

Loader file (index.php)

Now it's quite easy to create an index.php located in <templatename> folder which supports variants:

<?php

/**
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or (at
 *   your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful, but
 *   WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *   General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 *   @author          Black Cat Development
 *   @copyright       2013, Black Cat Development
 *   @link            http://blackcat-cms.org
 *   @license         http://www.gnu.org/licenses/gpl.html
 *   @category        CAT_Templates
 *   @package         <templatename>
 *
 */

if (defined('CAT_PATH')) {
    if (defined('CAT_VERSION')) include(CAT_PATH.'/framework/class.secure.php');
} elseif (file_exists($_SERVER['DOCUMENT_ROOT'].'/framework/class.secure.php')) {
    include($_SERVER['DOCUMENT_ROOT'].'/framework/class.secure.php');
} else {
    $subs = explode('/', dirname($_SERVER['SCRIPT_NAME']));    $dir = $_SERVER['DOCUMENT_ROOT'];
    $inc = false;
    foreach ($subs as $sub) {
        if (empty($sub)) continue; $dir .= '/'.$sub;
        if (file_exists($dir.'/framework/class.secure.php')) {
            include($dir.'/framework/class.secure.php'); $inc = true;    break;
        }
    }
    if (!$inc) trigger_error(sprintf("[ <b>%s</b> ] Can't include class.secure.php!", $_SERVER['SCRIPT_NAME']), E_USER_ERROR);
}

// Optional set some values for output....
$dwoodata	= array();

// get variant to use
$variant = CAT_Helper_Page::getPageSettings($page_id,'internal','template_variant');
if(!$variant)
    $variant = 'default';

$parser->setPath(CAT_TEMPLATE_DIR.'/templates/'.$variant);
$parser->setFallbackPath(CAT_TEMPLATE_DIR.'/templates/default');

// Print output
$parser->output('index.tpl', $dwoodata );

How to let the user choose from the variants

BlackCat CMS automatically creates a variants select box if it finds a list of variants configured in the info.php of the template. So add your variants like this:

$template_variants = array( 'default', 'gallery' );

How to load the variant's CSS

BlackCat CMS has an easy naming convention for autoloading. So the easiest way to autoload the variant's CSS - located in ./templates/<templatename>/css/<variantname> - is to name it template.css.

If you need more complex logic or different file names, use the headers.inc.php.

Note: If you don't need a different CSS for the variant, you don't have to create a subfolder under css.

Clone this wiki locally