Forum Moderators: coopster

Message Too Old, No Replies

Calling a file from another file?

         

ripeog

6:51 pm on Mar 9, 2005 (gmt 0)

10+ Year Member



Ok, I'm not sure how to explain this, but I'll give it a shot.

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!

ogletree

7:03 pm on Mar 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you are asking if you can have an include in an include you can't.

jatar_k

7:13 pm on Mar 9, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> If you are asking if you can have an include in an include you can't.

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?

winglian

7:30 pm on Mar 9, 2005 (gmt 0)

10+ Year Member



here's another spin on calling another php script from within a script. I've been calling a another php script via fopen and pointing to the localhost... but is there a way to call a php script via an exec() or something and pass the URI query strings and such?

Thanks
Wing

ogletree

7:46 pm on Mar 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sorry jatar_k I don't know where I came up with that. I could have sworn I ran into that at some time. Maybe it was ASP I don't know. I tried it an sure enough you can do it.

ripeog

8:18 pm on Mar 9, 2005 (gmt 0)

10+ Year Member



What I have for my main page is news/index.php. Within that file, the header.tpl file is called where the columns are defined. But also within that php file an index.tpl file is called where the middle column is defined.

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!

ironik

9:28 pm on Mar 9, 2005 (gmt 0)

10+ Year Member



I might be a little off here, but if you are using PHPlib's template system and you want to call an entire template file and place it's content in another file it would look like this:

<!-- 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!

ripeog

9:42 pm on Mar 9, 2005 (gmt 0)

10+ Year Member



I'm almost understanding this. Thanks for the reply.

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!

ironik

10:49 pm on Mar 9, 2005 (gmt 0)

10+ Year Member



I don't think it will work, although I'm not familiar with all the nuances of PHP lib.

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).

ripeog

12:54 am on Mar 10, 2005 (gmt 0)

10+ Year Member



Thanks alot for the info. I will look into the smarty templates, but for now I will try what you suggested and see if I can get it to work.

I may be back if I have more questions!

Thanks again!