...

View Full Version : PHP Server side template inclusion



ssskaya
03-29-2004, 04:30 PM
<?php
$title = "My Title";
$text = "I need to have 5 paragraphs of text here.";
?>

<?php include ("template.php"); ?>

----------

I want to use the script above as a file which will work with the template.php file.

For "$text", I have 5 paragraphs of text, but I don't want to insert it there as a php inclusion from a third file.

Is there another way to put the text there?

I just want to get it done with two files.

Thanks!

sad69
03-29-2004, 07:52 PM
I don't think I understand what you mean. Where are you getting these 5 paragraphs from? If it's in a separate file then....?

Sadiq.

ssskaya
03-29-2004, 08:57 PM
FIRST:


I have a template file called template.php.

I also have a set of files. For the sake of the conversation, we can call them:

textfile1.php
textfile2.php
textfile3.php
textfile4.php
textfile5.php

Each of these text files contain paragraphs of text.

The template file has $title and $text that needs to be filled in by these textfiles.

-------

SECOND

Since these textfiles should embed the necessary text in the template.php file, a typical textfile.php would be like:

<?php
$title = "My Title";
$text = "I need to have 5 paragraphs of text here.";
?>
<?php include ("template.php"); ?>

---------

QUESTION:

As you see, I have no problem with the title. I just write it there, and that's it.

But as for the 5 paragraphs, I don't know what to do.

I know a solution though. For $text, I can make php inclusions from .html files which would have just:

<p>a paragraph of text</p>
<p>a paragraph of text</p>
<p>a paragraph of text</p>
<p>a paragraph of text</p>
<p>a paragraph of text</p>

But I don't want to do that. Because I want to get this done with only two files: 1.template.php 2.textfile1.php

Therefore I need to know how I can put 5 paragraphs of text for $text without the obligation of a third file.

sad69
03-29-2004, 10:09 PM
ok so textfile.php will hold the title, a variable $text, and include template.php.

i'm still not sure where the 5 paragraphs are coming from. are they coming from eacho of textfileX.php? so textfile1.php is paragraph 1, ...?

because if that's the case, i'm confused as to how you're planning on doing that, as each textfileX.php includes template.php....

so i need more specifications.
sadiq.

ssskaya
03-29-2004, 11:00 PM
dear sadiq, (loyal, that is, right?)

you are talking about the exact same problem.

these 5 paragraphs are not in any of the files. actually, this is what the problem is.

we are trying to find a way to somehow equalize them to $text.

sad69
03-29-2004, 11:29 PM
In arabic, as far as I know, Sadiq == Honest. But loyal could be right too..

In regard to your problem, I see that you're not sure where the paragraphs are coming from either.

In order to figure out where you want to put these 5 paragraphs, you should ask/answer the following questions:
-why do you need the different textfile.php?
-will the 5 paragraphs differ in each of the textfile.php?
-are there other php files that will include template.php which will have a different 5 paragraphs?

The point I'm trying to make here is will these 5 paragraphs be the same for all pages? Such that if the 5 paragraphs were to change in on file, should they change in all files?

Possible places could be to place them in template.php (the 5 paragraphs should be the same on all pages), in a separate file (which you seem to be against... why, if I may ask? This would be the easiest place to keep them for updating, etc.. that is if the 5 paragraphs should be the same on all pages), in a database record (same idea as a file really..), or retype them in each textfile.php (if each textfile.php should have a different 5 paragraphs).

There may be some other places, but I can't think of them right now...

Hope that helps,
Sadiq.

ssskaya
03-29-2004, 11:36 PM
I understand.

The textfiles are all different.

The template.php will be a template file for about 60 files. 60 textfile.php files in other words. Some will have 3 paragraphs some 10.

I don't want to have them separately because otherwise I will have three files to execute each single page, and it will be difficult to update because of the nature of the file.

I want to execute each page with two files: template.php (which has all the style, tables etc.) and the textfile (which has the text, and instructions as to where to include them)

sad69
03-30-2004, 12:08 AM
Seeing as the "paragraph" portion will not be static, and will be changing for each textfile.php, then you should actually include the "paragraphs" in each textfile.php.

If you have certain "paragraphs" that will be repeated in some/many textfile.php, then I would suggest putting these paragraphs as records in a database, then you could select the appropriate paragraph message to place in the textfileX.php file as necessary...

Unless I'm missing something, that's what I'd do. Let me know what you decide, or if you still can't make up your mind and why.

Sadiq.

ssskaya
03-30-2004, 11:21 PM
Sadiq,

Please visit:

http://www.fiscalstudy.com/template.txt (the script)

http://www.fiscalstudy.com/template.php (the page)


I put all the details there for you.

Thanks!

sad69
03-31-2004, 08:00 PM
Whew, this took a little thought, but the solution is quite simple -- I think so at least.

I hope this works for you:

So you have the template.php. If you want to open a page, lets say contacts.php, what you should do is this:
Have your source code for the contacts page (just the table, text, and images...) in contacts.php. contacts.php should not hold any includes for header, footer, etc...

How you would open contacts.php is as follows:
template.php?page=contacts.php

Now in your template.php where you want the contacts.php page to show up, you include $page, where $page == "contacts.php".

Does that make sense?

You'd have to change your menu so that instead of opening contacts.php, it will open template.php?page=contacts.php, and all the links in your website accordingly, but that's the cleanest way to go about this I think. And this way you only have 1 template file, and then 1 file for each of your webpages.

I've attached a couple files, hopefully it helps you understand better. I think my explanation was sort of crappy... let me know if you're still stuck.

Good luck,
Sadiq.

ssskaya
04-01-2004, 12:38 AM
Sadiq:

Yes I understood what you said. Although I never dod that before, no worries, it was quite clear.

I will let you know what happens.

And I would be damned if I don't say this: This was the greatest help I ever got in a forum - since this thing was very important to me. You must have understood that when you saw my site.

Thanks for spending time on it.

I say they should make you a MODERATOR or something.

you are not just a "CF Regular" like me!

sad69
04-01-2004, 12:55 AM
Hey no problem! Lol, I already have a fulltime job, but I'm flattered anyway!

But lookie, I'm a CF Addict now! Sweeet! lol

Good luck with it, and let me know if you have any troubles (I doubt you will..).
Sadiq.

ssskaya
04-01-2004, 01:09 AM
I tried the attachment on the server but it shows blank.

For $page I uploaded the page1.php that you attached. On the browser's address bar I wrote: ....com/template2.php?page=page1.php

did I do something wrong or is there something wrong with the inserted php script?


PS: That's because I renamed it to template2.php (it's still there)
PSS: The page looks fine when I exclude the php script that you inserted. A copy of it is below.



<?php
if(in_array("page", $_GET)) {
$page = $_GET["page"];
if ( file_exists($page) ) AND !preg_match("#^\.\./#",$page) ) {
include $page;
}
else {
echo "Invalid page requested.";
}
}
else { //no page included
echo "Please request a page.";
}
?>

sad69
04-01-2004, 01:15 AM
Whoops! I think this may be the problem at least...

Change:
include $page;

to:
echo include $page;

I think that should fix it... let me know.

Sadiq.

ssskaya
04-01-2004, 01:19 AM
sorry.

still blank..

sad69
04-01-2004, 01:41 AM
My apologies, I never tested it out.

This will work though:


<?php
if(isset($_GET["page"])) {
$page = $_GET["page"];
if ( file_exists($page) AND !preg_match("#^\.\./#",$page) ) {
include $page;
}
else {
echo "Invalid page requested.";
}
}
else { //no page included
echo "Please request a page.";
}
?>


Confirm that works,
Sadiq.

ssskaya
04-01-2004, 01:58 AM
Yes!

Works great! -which means I have some pages to work on!

Thanks again.

sad69
04-01-2004, 02:03 AM
No problem, good to hear.

Have fun :),
Sadiq.

ssskaya
04-01-2004, 04:59 PM
the template file has <?php print $title; ?> in its title tag.

How do I specify it in the textfiles so that the template file can print the title?

(or do you know by any chance as to whether it's possible to have the title and meta tags in each file instead of the template file. actually that would offer more flexibility.)

sad69
04-01-2004, 06:45 PM
Not the way it's currently setup.

Basically the contents your page1.php type of file would need to be stored in a PHP variable (say $include_page). Then you can also store the title in the $title variable.

You should do the include if/else block at the very top of the template.php file. Something like this:



<?php
if(isset($_GET["page"])) {
$page = $_GET["page"];
if ( file_exists($page) AND !preg_match("#^\.\./#",$page) ) {
include $page;
}
else {
$title = "Invalid page requested.";
$include_page = "Invalid page requested.";
}
}
else { //no page included
$title = "Please request a page.";
$include_page = "Please request a page.";
}
?>


Then you can echo $title and echo $include_page as necessary.



<td valign="top" width="82%"><div align="center">
<br>
<?php
echo $include_page;
?>
</div>
</td>


Hope that works,
Sadiq.

ssskaya
04-01-2004, 10:00 PM
It didn't work.

I suspect it's the template script because when it's opened on its own, it doesn't say "request a page".

--

Question: I understand that where I should execute the title I just echo it on the template. As for the html files, in order to define the title, is it enough to just write: <?php $title="My page title"; ?> at the beginning of the page?

sad69
04-01-2004, 10:33 PM
To answer your "Question", yes that's what you should do for the title. Is the title coming up properly?

Are you putting the if/else stuff right at the top of template.php? do some echo's in that if/else block to make sure it's working.

I'm not sure what's going wrong, that should work.

Just debug it man, that's all I can say. Show me your page1.php, to make sure your html is in that variable properly.

Sadiq.

ssskaya
04-01-2004, 11:10 PM
I now put the else/if stuff at the top the page.

The title comes out all ok.

but the page shows at the top of everything since I put the script at the top of everything although I have put <?php echo $include_page; ?> at where I wanted the page to show up.

I guess it's because the script says 'include $page' at the top.

ssskaya
04-01-2004, 11:54 PM
Attached is my latest template.php & the page.

You can see how it turns out at:

http://www.fiscalstudy.com/tpl2.php?page=pa/argentina/argp01.php

Thanks.

sad69
04-01-2004, 11:59 PM
Ya, see you're not putting the html code in the PHP variable $include_page in your argp01.php

That argp01.php should be all PHP code, with no echo's. That's because you don't want to echo the page yet, you want to save the page in the $include_page variable, and echo it down below in the table (in template).

Make sense?

Sadiq.

ssskaya
04-02-2004, 12:16 AM
I totally understand.

But I don't think I know how to do it!

And I know that's truly lame of me.

sad69
04-02-2004, 03:58 AM
Ok here's a small example of a page1.php:


<?php
$title = "Title of page1.php";
$include_page ="";
$include_page .= "<table>";
$include_page .= "<tbody>";
$include_page .= "<tr>";
$include_page .= "<td>";
$include_page .= "This is a sample page1.php content.";
$include_page .= "</td>";
$include_page .= "</tr>";
$include_page .= "</tbody>";
$include_page .= "</table>";
?>


Does that make sense? So then when you echo $include_page, it'll display all that html code nicely.

I know it's more work, and it's not as nice as the other way, but if you really want the title, this is the price you're going to have to pay. I don't see any other way to do it.

Good luck,
Sadiq.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum