Forum Moderators: coopster
I am using PHP, MYSQL, html, css, etc. and have got a header file that contains the div classes: top, right, and left. These columns are all static and show up no matter where you go to in the site.
Now the middle column is called within multiple files throughout my site because it is the only one that the contents change when browsing my site.
My header file is set up basically like so:
<div class="topColumn">BANNER CODE</div>
<div class="leftColumn">LEFT CODE</div>
<div class="rightColumn">RIGHT CODE</div>
Now within the middle column files it is set up like so:
<div class="middleColumn">MIDDLE CODE</div>
Due to layout issues, I need to "call" this middle column code within the one header file so I'd like to have it as so:
<div class="topColumn">BANNER CODE</div>
<div class="leftColumn">LEFT CODE</div>
<CALL MIDDLE COLUMN CODE HERE>
<div class="rightColumn">RIGHT CODE</div>
Is there a way to do this, or is there an easier way?
Thanks in advance for your time!
no offence ogletree but you can
ripeog, I don't see why you can't do that though I can't suggest code as I don't quite see what you are asking.
My guess is that it may be similar to some things I have done. I have a file in every top level directory (main section) of my site that has the exact same name. The header knows to include that file, it always calls it using the present path so it is a single non changing include that grabs a different file depending on where you are within the site.
I also do selective includes based on the path, my header figures out where it is being included from and does some different things accordingly.
Is that along the lines of what you are talking about?
Now if you go to say games/index.php, it works the same way, but it's a different index.tpl file that is called there but the same header.tpl file is called.
Here's an example of where the tpl file is called within one of my index.php files:
$template->set_file('index', 'news/index.tpl');
SOME CODE HERE
$template->set_var(array(
'CATEGORY_ID' => $table_news['category_id'],
'CATEGORY_IMAGE' => $table_news['category_image'],
'CATEGORY_NAME' => $table_news['category_name'],
$template->parse('news', 'NEWS_BLOCK', true);
Now in the corresponding index.tpl file I have this:
<!-- BEGIN NEWS_BLOCK -->
<div class="middleColumn">
SOME CODE that contains {CATEGORY_ID}, {CATEGORY_IMAGE}, etc...
</div>
<!-- END NEWS_BLOCK -->
I hope this explains it better.
Thank you!
<!-- BEGIN NEWS_BLOCK -->
<div class="middleColumn">
<!-- BEGIN MIDDLE_COLUMN --><!-- END MIDDLE_COLUMN -->
</div>
<!-- END NEWS_BLOCK -->
Then the PHP code:
$template->set_block('<parent template var name>', 'MIDDLE_COLUMN', 'middle');
$template->set_file('middle', 'header.tpl');
$template->loadfile('middle');
That will replace the block MIDDLE_COLUMN with the contents of the header.tpl file and then you go ahead with your NEWS_BLOCK code as all the template variable containers will be available to you.
I hope that is what you are asking, otherwise I'm way off!
So, basically I would have to "loadfile->header.tpl" into every one of my index.php files like so?
$template->set_file('index', 'news/index.tpl');
$template->set_block('index', 'NEWS_BLOCK', 'news');
$template->loadfile('middle', 'MIDDLE_BLOCK', 'news');
$template->parse('news', 'NEWS_BLOCK', true);
$template->parse('middle', 'MIDDLE_BLOCK', true);
And then all I'd have to do to display any page is change the header.tpl file to:
<div class="topColumn"></div>
<div class="leftColumn"></div>
<!-- BEGIN MIDDLE_BLOCK -->
<div class="middleColumn"></div>
<!-- END MIDDLE_BLOCK -->
<div class="rightColumn"></div>
Would this work?
Thanks!
Check out the example I gave before, It's the only method that I know for sure works.
If you load a file into a block, then any of the contents in the block will be replaced with the contents of the file (my example has nothing between the block delimiters).
Here's a full example:
<?php
include('templateclass.php');//replace with your template class file name
$template = new Template('template/dir/', "keep"); // set the template directory
// Set the main file as index.tpl
$template->set_file('main', 'index.tpl');
// Replace the block 'MIDDLE COLUMN' with the contents of header.tpl
$template->set_block('main', 'MIDDLE_COLUMN', 'middle');
$template->set_file('middle', 'header.tpl');
$template->loadfile('middle');
// Do other template stuff here
// Output template
$t->pparse('OUT', 'main');
?>
That is a very simple example that will replace:
<!-- BEGIN MIDDLE_COLUMN --><!-- END MIDDLE_COLUMN -->
with the contents of the header.tpl. If the header.tpl file has variables in it e.g. {MY_VARIABLE} then you can use $template->set_var('MY_VARIABLE', 'My value'); after you've replaced the block with the contents of header.tpl
(BTW, I don't use PHPlib anymore unless I have to. If you have a large volume of traffic to your website, this template system can start to put a higher load on the server. Check out 'smarty' templates for something with template caching).