Page 1 of 1

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

Posted: Fri Dec 26, 2008 8:08 am
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 :)

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

Posted: Fri Dec 26, 2008 8:42 am
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...

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

Posted: Fri Dec 26, 2008 9:08 am
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 } ?>

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

Posted: Fri Dec 26, 2008 9:45 am
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.

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

Posted: Fri Dec 26, 2008 10:21 am
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?

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

Posted: Sat Dec 27, 2008 11:10 am
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.

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

Posted: Sat Dec 27, 2008 12:22 pm
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 :)

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

Posted: Sat Dec 27, 2008 12:32 pm
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.

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

Posted: Sat Dec 27, 2008 12:41 pm
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... :(

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

Posted: Sat Dec 27, 2008 1:40 pm
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.

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

Posted: Sat Dec 27, 2008 2:13 pm
by sergio-pro
Hi

If you think that the way you output html is awful, then its definitely time to start learning smarty ;)

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

Posted: Sat Dec 27, 2008 8:12 pm
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.

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

Posted: Sat Dec 27, 2008 8:35 pm
by cptnwinky
Well, you always have us to ask questions too. If you ever need too, feel free to PM me directly.

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

Posted: Sun Dec 28, 2008 12:55 am
by aditya2071990
Thank you :)