I am trying a HTML/CSS file to convert to a template. I asked for help but I didn't receive any suggestions in that post yet I tried to figure out a way to create a template. I am having few glitches in doing that. My controller has main.php file which is the main page of the website, and I have register where the header, footer, and rest remain same except that main content changes when the page loads.
I have the following template in the views folder:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title><?= $title ?></title>
<style type="text/css" >
@import url('<?=base_url()?>/includes/front.css');
@import url('<?=base_url()?>/images/');
</style>
</head>
<body>
<div id="container">
<div id="header">
<?= $logo ?> <!--This is an image tag to display the image on the template -->
<?= $topnav ?>
</div>
<div id="menubar">
<?= $menulist ?>
</div>
<div id="maincontent">
<?= $flashcontent ?>
<?= $resources ?>
<?= $registerForm ?>
</div>
<div id="footer">
<div>
<?= $footertable1 ?>
</div>
</div>
</div>
</body>
</html>
In the
div id开发者_开发技巧 = maincontent
This is the div tag which would be changing when a site loads. In my template I have included <
<?= $flashcontent ?> <?= $resource ?> and <?= registerForm ?>
The flashcontent and resources are supposed to be in the main page and registerForm is supposed to be shown when I click on register in the main page. I have been reading the template user guide in the code igniter but I am unable to understand how to make this dynamic. Please help me in getting over this issue.
This is the template.php in config folder.
$template['active_group'] = 'default';
$template['default']['template'] = 'template.php';
$template['default']['regions'] = array(
'title',
'logo' => array(
'content' => array(''),
'name' => 'Logo',
'wrapper' => '<img>',
'attributes' => array('id' => 'logo')
),
'topnav' => array(
'content' => array(),
'name' => 'Topnav',
'wrapper' => '<div>',
'attributes' => array('id' => 'topnav', 'class' => 'topnav')
),
'menulist' => array(
'content' => array('<li>
<a href="#"> Home </a>
<a href="#"> Generator </a>
<a href="#"> News </a>
<a href="#"> Forum </a>
<a href="#"> About </a>
<a href="#"> Feedback </a>
</li>'),
'name' => 'Menulist',
'wrapper' => '<ul>',
'attributes' => array('class' => 'menulist')
),
'flashcontent' => array(
'content' => array(''),
'name' => 'Flashcontent',
'wrapper' => '<div>',
'attributes' => array('id' => 'flashcontent')
),
'resources' => array(
'content' => array(''),
'name' => 'Resources',
'wrapper' => '<div>',
'attributes' => array('id' => 'resources')
),
'registerForm' => array(
'content' => array(),
'name' => 'Registerform',
'wrapper' => '<div>',
'attributes' => array('id' => 'registerForm')
),
'footertable1' => array(
'content' => array('<ul>
<li> <a href="../main">Home</a>
<a href="#">News & Events</a>
<a href="#">Forum</a>
<a href="#">About us</a>
<a href="#">Contact us</a>
<a href="#">Terms of use</a>
<a href="#">Privacy policy</a>
<a href="#">Site map</a>
</li>
</ul>'),
'name' => '',
'wrapper' => '<div>',
'attributes' => array('id' => 'footertable1')
)
);
$template['default']['parser'] = 'parser';
$template['default']['parser_method'] = 'parse';
$template['default']['parse_template'] = FALSE;
Thanks in advance.
Are you using a CMS or a custom database? Where are the dynamic elements coming from?
The basics for making a dynamic element are as follows:
Above your html:
<?php
$logosrc="mylogo.gif";
?>
Then in your html:
<img src='<? echo $logosrc;?>' />
Looks like you're working with Colin Williams Template library.
I haven't looked at it too much but take a read @ http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html#manipulation
$this->template->add_region($name, $settings = array())
The add_region() method allows one to dynamically add regions for writing. At a minimum, a $name must be supplied to identify the region. Optionally, a $settings array can be passed in to provide default content, wrappers and attributes.
$this->template->add_region('flash');
$this->template->write_view('flash', 'some_flash');
$this->template->add_region('login');
$this->template->write_view('login', 'some_login');
Edit
(Again, assuming you are using the template library I think you are),
You have a region content
in your template. You have a view called registerForm
which contains just the form.
In your controller method
for register, you simply have to do:
$this->template->write_view('content', 'registerForm');
The library will load the content of the view registerForm
in to the content
region.
You can have as many regions as you like, obviously, but the template library allows you to define an area (a region) that you want any content to appear, and then outputs your HTML or views directly in to the region.
Either I misunderstand but I don't think I can explain it much simpler than this. The docs are rather good as well.
Edit the second
Here's a sample method from a project I did a while back using this library.
class Welcome extends Controller {
function __construct()
{
parent::__construct();
$this->template->set_template('my_template');
// as defined in config/template.php
}
function index()
{
$this->template->write('title', 'This is my $title region');
// here I am writing some views to my regions $content and $sidebar
// content/home-page-view is just a view containing text.
$this->template->write_view('content', 'content/home-page-view');
$this->template->write_view('sidebar', 'sidebar/home-page-sidebar');
// lets go..
$this->template->render();
}
now when I access mysite.com/welcome/index
my HTML template will be displayed with the content I wrote in the method.
My HTML template looks a little like:
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<?php echo $content; ?>
<?php echo $sidebar; ?>
</body>
</html>
My config/template.php file looks like this:
$template['default']['template'] = 'template/master';
$template['default']['regions'] = array(
'title',
'content',
'sidebar'
);
$template['default']['parser'] = 'parser';
$template['default']['parser_method'] = 'parse';
$template['default']['parse_template'] = FALSE;
template/master
refers to a file in my views folder (template/master.php) which contains my layout (as above).
Use
uri_segment
to control your dynamic content.
In yourmain.php
:<?php //... $template['registerForm'] = ''; if($this->uri->segment(1) === false) { $template['registerForm'] = $this->load->view('form/registration'); } //... ?>
Use JavaScript to load new page when clicked. Make a simple function in
main.php
just to load that form template and show that form via ajax.
精彩评论