|
[eluser]Daniel Dornhardt[/eluser]
Hello everybody, I'm new to Code Igniter, but I already like it...
I am a PHP Programmer for only a little bit more than a year now, but I already tried different frameworks and approaches to structuring code...
I started porting a personal app to Code Igniter to check it out, and I really like it so far, but one thing I really liked when I tried the Django web framework was still missing:
Template inheritance.
So I created a helper to take care of that.
It also works for "Ungnited" PHP Code by the way.
I am actually not sure if everything is working as it should, especially concerning the loading of views from within views... I also didn't test it on PHP 4 yet, but I think it should work.
Comments are very welcome
If someone can tell me how to produce proper linebreaks / paragraphs in the wiki, I'll prettify it later on.
Without further ado:
Template Inheritance Helper in Wiki
SHORT SYNAPSIS:
* Create a Master Template
* Mark some blocks in it
* Create some child templates
* extend Master Template
* do $this->load->view('child_template.php')
* multiple levels of inheritance. E.g. create a master template for the general page outline, extend that for different sections, and extend those section templates to include the final content
CONFIGURATION:
If you are using Code Igniter, you shouldn't have to do anything.
If not, you'll have to define a 'TI_VIEWS_DIR' constant, like
define ('TI_VIEWS_DIR', 'views/');
which should be a path where PHP can find your view files. You can make it relative
to the current PHP working dir or absolute, it shouldn't matter.
If you don't do this, you'll have add the path to your files to your extend() calls.
Then just include this file somehow. Code Igniter users can drop it into their 'helpers' - dir
and make sure that it gets loaded (via autoload or manually).
QUICK TUTORIAL:
Imagine you have a website with the same basic structure in every page. Something like this:
Code: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
Dan's Music Store
</title>
<link rel="stylesheet" href="css/base.css" type="text/css" media="all" />
</head>
<body id="body">
<div id="container">
<!-- Content goes here -->
</div> <!-- container -->
</body>
</html>
Then you might have different subpages which actually use the same basic structure.
Now you can use PHP to include separate files for every area of that template, but
I always felt like this was a suboptimal solution.
So the idea is to put some kind of markers into the base template which can be extended from child
templates. We'll call it "main_template.php". An example could look like this:
main_template.php:
Code: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
<? start_block_marker('title') ?>
Dan's Music Store
<? end_block_marker() ?>
</title>
<? start_block_marker('extra_head') ?>
<link rel="stylesheet" href="css/base.css" type="text/css" media="all" />
<? end_block_marker() ?>
</head>
<body id="body">
<div id="container">
<? start_block_marker('content') ?>
<h1>Welcome to the Dan's Music Store</h1>
<p>Instruments from all over the world</p>
<? end_block_marker() ?>
</div> <!-- container -->
</body>
</html>
If you include just this, essentially you would get the same output as in the first example, because
no special block content has been assigned yet.
To use this file as a base template for different content, you have to use the extend() function
and start overriding the blocks of the base template. Let's call this one "guitars.php". It will be a
section template which includes some extra content for the guitar section of our music store.
guitars.php:
Code: <? extend('main_template.php') ?>
<? startblock('title') ?>
<?= get_extended_block() ?>
- Guitars
<? endblock() ?>
<? startblock('extra_head') ?>
<?= get_extended_block() ?>
<link rel="stylesheet" href="css/guitars.css" type="text/css" media="all" />
<? endblock() ?>
<? startblock('content') ?>
<h2>Look around!</h2>
<p>Such a fine selection of Guitars!</p>
<? endblock() ?>
<? end_extend() ?>
With extend('filename'), you tell this template which base template it should extend. You need to wrap
this file up with a call to <? end_extend() ?> to make the magic work.
You can call get_extended_block() to inherit / receive the content of the parent block,
which is useful for adding additional data to the base template.
In this example it's used to add a second part to the <title> - tag and to add an additional stylesheet.
|