PHP include (replacement for masterpage)
Posted: Thu Jan 14, 2010 6:09 pm
Hey all! Thanks in advance for any replies.
I am somewhat new to serverside programming. I started with ASP.net, and recently decided that i would try out PHP to see which i liked better. So far, PHP has been better for me.
In asp the solution i found for templating a website so all pages look similar and to centralize the coding of navigation and such was/is called "master pages".
I did some google search "research" and found most people saying that php's solution to this is to create a template page w/ some php varriables in it, then set the values of those varriables in a new page and include() the template page...
This works great for me!... BUT i have run into an issue now.
I would like to run a php script on a single page that uses this template... If i wanted to run the script on all pages, i could easily just throw it into the template, but that's not the desired goal.
My issue comes in the fact that i cannot set the varriables values to have the php script in it and have the server actually process the script... Time for examples:
template.php :
<html>
<?php echo $content ?>
</html>
workingpage.php :
<?php $content="My content for page goes here."
include('template.php')?>
problempage.php :
<?php $content="my content goes here.. and my script i want to run <?php include('script.php') ?> "
include('template.php')?>
So i can kind of understand WHY it doesn't work, i mean, i would think that the php "engine" if you will is not really parsing the varriable for php code.. instead it's just handling it as a string... but what i don't understand is how to put a script like that on a single page with out doing something crazy like putting it in the template w/ and if statement to go w/ the title of the page. Also, i think that i could easily include the script where i'm including the template, but what if i need to put something in a <div> ? i guess the question would be how to control the location of the output of the script.. i need to put it in my "content' <div>
Hopefully i made myself clear enough to achieve an answer.. If not please feel free to question something and i'll answer best i can..
Again, thanks in advance for any replies..
Tom
I am somewhat new to serverside programming. I started with ASP.net, and recently decided that i would try out PHP to see which i liked better. So far, PHP has been better for me.
In asp the solution i found for templating a website so all pages look similar and to centralize the coding of navigation and such was/is called "master pages".
I did some google search "research" and found most people saying that php's solution to this is to create a template page w/ some php varriables in it, then set the values of those varriables in a new page and include() the template page...
This works great for me!... BUT i have run into an issue now.
I would like to run a php script on a single page that uses this template... If i wanted to run the script on all pages, i could easily just throw it into the template, but that's not the desired goal.
My issue comes in the fact that i cannot set the varriables values to have the php script in it and have the server actually process the script... Time for examples:
template.php :
<html>
<?php echo $content ?>
</html>
workingpage.php :
<?php $content="My content for page goes here."
include('template.php')?>
problempage.php :
<?php $content="my content goes here.. and my script i want to run <?php include('script.php') ?> "
include('template.php')?>
So i can kind of understand WHY it doesn't work, i mean, i would think that the php "engine" if you will is not really parsing the varriable for php code.. instead it's just handling it as a string... but what i don't understand is how to put a script like that on a single page with out doing something crazy like putting it in the template w/ and if statement to go w/ the title of the page. Also, i think that i could easily include the script where i'm including the template, but what if i need to put something in a <div> ? i guess the question would be how to control the location of the output of the script.. i need to put it in my "content' <div>
Hopefully i made myself clear enough to achieve an answer.. If not please feel free to question something and i'll answer best i can..
Again, thanks in advance for any replies..
Tom