What is the best way to include HTML in php scripts?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

What is the best way to include HTML in php scripts?

Post by aditya2071990 »

I am currently using 'echo' to output the code, and let me tell you that it looks awful! Maintanence has become a headache... and no I don't want to use a templating system like smarty or whatever else that will require me to learn new syntax, new code and other things...

So is there a built in way to handle this stuff? I just want the stupid html out of my way in the main script.

All help appreciated :)
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: What is the best way to include HTML in php scripts?

Post by aditya2071990 »

Okay I read somewhere that I can use includes, which turn out to be a pretty cool idea. But variable scoping is a major problem for me to understand. As far as I understood, one can use all the variables declared in both the main script and the include file in both the main script and include file, but something tells me that this is not a good thing to do...
thetooth
Forum Newbie
Posts: 3
Joined: Fri Dec 26, 2008 7:31 am

Re: What is the best way to include HTML in php scripts?

Post by thetooth »

the way i do it is insted of putting the echo command i end the php block and enter my html and then open it up again

Code: Select all

<?php
if($cookies){?>
<b>heloo</b>
<?php } ?>
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: What is the best way to include HTML in php scripts?

Post by mmj »

thetooth wrote:the way i do it is insted of putting the echo command i end the php block and enter my html and then open it up again

Code: Select all

<?php
if($cookies){?>
<b>heloo</b>
<?php } ?>
Yeah, thats a good method.

Also the alternative control structures syntax is used frequently with this method.
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: What is the best way to include HTML in php scripts?

Post by aditya2071990 »

I like this method, but I need to output the html based on a condition, like if(logged in){ then show the html } so if I simply end the php tags, the html shows up anyway... any other methods?
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: What is the best way to include HTML in php scripts?

Post by watson516 »

aditya2071990 wrote:I like this method, but I need to output the html based on a condition, like if(logged in){ then show the html } so if I simply end the php tags, the html shows up anyway... any other methods?
How about...

Code: Select all

<?php
...
if($_SESSION['logged_in']) include("inc/logged_in.php"); else include("inc/logged_out.php");
...
?>
As for the ending of the php tags to output html, if the html is within a block of php, the html will only get outputted if that php block is executed.

Code: Select all

<?php
...
if($someVariable)
{
      ?>
       <p>Some variable equals true!</p>
      <?php
}else{
      ?>
       <p>Some variable equals false!</p>
      <?php
...
?>
If $someVariable is true, only the first p tag will be outputted. If it is false, only the second will be outputted.
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: What is the best way to include HTML in php scripts?

Post by aditya2071990 »

I am making use of a hybrid of what you said, watson.

I am making an include with variables consisting of only html code, like when logged in $something = 'html blah blah' and when logged out $somethingElse = 'more html blah blah', and depending on the session, if(logged in){show $something} else {show $somethingElse}

This is working fine for now, but I always welcome fresh ways of doing things :)
cptnwinky
Forum Commoner
Posts: 84
Joined: Sat Dec 27, 2008 10:58 am
Location: Williamstown, MA

Re: What is the best way to include HTML in php scripts?

Post by cptnwinky »

I know that you said you don't want to use a template engine like Smarty but you should understand that without some level of abstraction, no matter how you echo out html within your php script it will always be a headache to maintain. If you truly want to know what is the "best" way to include html in your php script abstraction is the best way. Besides that, Smarty is extremely simple to use.

Code: Select all

 
require('Smarty.class.php');
$smarty = new Smarty();
 
$var1 = 'Hello World';
$smarty->assign('var1', $var1);
$smarty->display('index.html');
 

Code: Select all

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
<p>{$var1}</p>
</body>
</html>
 
If your goal is to ease maintenance then Smarty or some other abstraction layer is the way to go.

Just my 2 cents worth.
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: What is the best way to include HTML in php scripts?

Post by aditya2071990 »

Well though I hate to admit it, I still don't program like the big boys... meaning I'm still a procedural guy... smarty uses OOP, and that spooks me off... :(
cptnwinky
Forum Commoner
Posts: 84
Joined: Sat Dec 27, 2008 10:58 am
Location: Williamstown, MA

Re: What is the best way to include HTML in php scripts?

Post by cptnwinky »

The only difference as far as end use goes is say calling a function called assign like this...

assign('var1', $var1);

Or like this...

$smarty->assign('var1', $var);

It took me a while to wrap my head around OOP until I realized that it was just a different way of doing the same thing.
User avatar
sergio-pro
Forum Commoner
Posts: 88
Joined: Sat Dec 27, 2008 12:26 pm

Re: What is the best way to include HTML in php scripts?

Post by sergio-pro »

Hi

If you think that the way you output html is awful, then its definitely time to start learning smarty ;)
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: What is the best way to include HTML in php scripts?

Post by aditya2071990 »

Okay, the current project I am working on is running out of time, so for this one, I'll go ahead and do it my old way, after this is done, I'll make sure the first thing I do is learn OOP. Thanks for the helpful replies.
cptnwinky
Forum Commoner
Posts: 84
Joined: Sat Dec 27, 2008 10:58 am
Location: Williamstown, MA

Re: What is the best way to include HTML in php scripts?

Post by cptnwinky »

Well, you always have us to ask questions too. If you ever need too, feel free to PM me directly.
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: What is the best way to include HTML in php scripts?

Post by aditya2071990 »

Thank you :)
Post Reply