Development - Templates
In ApPHP Framework there is only one standard place where design template (theme) files are stored:
application/templates/{your template name}/
For each application you may define a default template name in configuration file. If no template is
defined as default the "default"
name will be used.
<?php
return array(
// application settings
'defaultTemplate' => 'default',
);
Sometime we need to use more than one template in our application and we need some way to change
the template "on fly". For example we need different templates for Backend and Frontend views.
The symplest way to do it is to re-define it directly in constructor of appropriate controller.
<?php
class AdminController extends CController
{
public function __construct()
{
parent::__construct();
A::app()->view->setTemplate('backend');
// - or - set backend mode
// Website::setBackend();
}
}
The common structure of the template directory looks like following:
-
templates/ directory where placed all templates
- backend/ all public files for backend template
- default/ all public files for a specific template
- css css files for template
- images image files for template
- js javascript files for template
- default.php default (master) template file
- .htaccess templates directory .haccess file
Example of default.php file:
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="keywords" content="<?php echo CHtml::encode($this->_pageKeywords); ?>" />
<meta name="description" content="<?php echo CHtml::encode($this->_pageDescription); ?>" />
<title><?php echo CHtml::encode($this->_pageTitle); ?></title>
<base href="<?php echo A::app()->getRequest()->getBaseUrl(); ?>" />
<?php echo CHtml::cssFile("templates/default/css/main.css"); ?>
<?php echo CHtml::scriptFile('http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'); ?>
<?php echo CHtml::scriptFile('templates/default/js/main.js'); ?>
</head>
<body>
<header>
<nav>
<a href="index">Home</a>
</nav>
</header>
<section>
// your content here
<?php echo A::app()->view->getContent(); ?>
</section>
<footer>
<p class="copyright">Copyright © <?php echo date('Y'); ?> Your Site</p>
<p class="powered"><?php echo A::powered(); ?></p>
</footer>
</body>
</html>
You may split default.php file into some files if you need. For example:
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="keywords" content="<?php echo CHtml::encode($this->_pageKeywords); ?>" />
<meta name="description" content="<?php echo CHtml::encode($this->_pageDescription); ?>" />
<title><?php echo CHtml::encode($this->_pageTitle); ?></title>
<base href="<?php echo A::app()->getRequest()->getBaseUrl(); ?>" />
</head>
<body>
<?php include('header.php'); ?>
<section>
// your content here
<?php echo A::app()->view->getContent(); ?>
</section>
<?php include('footer.php'); ?>
</body>
</html>
If you need dynamically change template, for example when you're going to backend area, you may whether
to define it in controller's constructor (if whole controller uses another template) or just define it
for appropriate action. Below you may check both examples.
<?php
class AdminsController extends CController
{
public function __construct()
{
parent::__construct();
// use template of protected area for whole controller
}
public function dashboardAction()
{
// use template of protected area for specific action
A::app()->view->setTemplate('backend');
// - or - set backend mode
// Website::setBackend();
}
public function view()
{
// use default template for this specific action
A::app()->view->setTemplate('default');
// - or - set frontend mode
// Website::setFrontend();
}
}