 |
 |
PHP Problems
|
 |
|
 |
|
Forum Regular
Join Date: May 2001
Location: New York
Status:
Offline
|
|
I made a PHP script which lets me use a template file, but I'm having some problems. I am unable to use other php code within the $content variable. (I can't echo the time or include something) Here is the code that I use for my index page:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
<?php
$title = 'chrisellsworth.net | home';
$validate = 'yes';
$content = '
<!--Begin Content-->
<div class=<font color = red>"center"</font>>
<div style=<font color = red>"font-size : 32px"</font>>
chrisellsworth.net<br />
</div>
</div>
<!--End Content-->
';
include 'template.php';
?>
</font>[/code]
and here is the template code:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
<!DOCTYPE html PUBLIC <font color = red>"-<font color = brown>//W3C//DTD XHTML <font color = blue>1.1</font>//EN"</font></font>
<font color = red>"http:<font color = brown>//www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"</font>></font>
<html xmlns=<font color = red>"http:<font color = brown>//www.w3.org/<font color = blue>1999</font>/xhtml"</font> xml:lang=<font color = red>"en"</font> ></font>
<head>
<title><?php echo $title;?></title>
<meta http-equiv=<font color = red>"content-type"</font> content=<font color = red>"text/html;charset=iso-<font color = blue>8859</font>-<font color = blue>1</font>"</font> />
<link rel=<font color = red>"stylesheet"</font> href=<font color = red>"/stylesheet.css"</font> type=<font color = red>"text/css"</font> />
</head>
<body>
<div id=<font color = red>"navbar"</font>>
<a href=<font color = red>"/"</font>>home</a>
| <a href=<font color = red>"/computers"</font>>computers</a>
| <a href=<font color = red>"/contact"</font>>contact</a>
| <a href=<font color = red>"/stuff"</font>>stuff</a>
</div>
<div id=<font color = red>"content"</font>><?php echo $content;?>
</div>
<div class=<font color = red>"center"</font>>
<br />
<?php if ( $validate == <font color = red>"yes"</font> ) {
echo( '<a href=<font color = red>"http:<font color = brown>//validator.w3.org/check/referer"</font>><img</font>
src=<font color = red>"http:<font color = brown>//www.w3.org/Icons/valid-xhtml11"</font></font>
alt=<font color = red>"Valid XHTML <font color = blue>1.1</font>!"</font> /></a>
<a href=<font color = red>"http:<font color = brown>//jigsaw.w3.org/css-validator/validator?uri=http://www.chrisellsworth.net/stylesheet.css"</font>><img</font>
src=<font color = red>"http:<font color = brown>//jigsaw.w3.org/css-validator/images/vcss"</font> </font>
alt=<font color = red>"Valid CSS!"</font> /></a>' );
} else {
echo( '<a href=<font color = red>"http:<font color = brown>//jigsaw.w3.org/css-validator/validator?uri=http://www.chrisellsworth.net/stylesheet.css"</font>><img</font>
src=<font color = red>"http:<font color = brown>//jigsaw.w3.org/css-validator/images/vcss"</font> </font>
alt=<font color = red>"Valid CSS!"</font> /></a>' );
}
?>
</div>
</body>
</html>
</font>[/code]
-Chris
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Jan 2001
Status:
Offline
|
|
Nevermind my previous post. I misunderstood. I think that your problem is the '. It's a literal and will not process any php variables contained within it. Use a double quote instead. I would just echo those lines and make sure you escape the " with a \. If you need to reuse the code repeatedly, put it in a function and call it when you need to. Or maybe I'm misunderstanding your problem
[ 02-03-2002: Message edited by: torifile ]
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: May 2001
Location: New York
Status:
Offline
|
|
Yup... you understood correctly. The way I wrote the script is really weird, I think I'm gonna go back to class.FastTemplate. I'll wait until I know some more code before I make my own template engine.
-Chris
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Nov 2001
Location: Oregon
Status:
Offline
|
|
Originally posted by cElls:
<STRONG>include 'template.php';
</STRONG>
In this context, you should use "require 'template.php';" rather than "include 'template.php';". It'll execute faster. Use include only inside of conditional tests, where the code may or may not be included, and require everywhere else.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: May 2001
Location: New York
Status:
Offline
|
|
Thanks for the tip.
-Chris
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jan 2002
Status:
Offline
|
|
PHP4 supports Perl style here documents, which may help you out when doing basic templating stuff (although the syntax is a little different than Perl's)
<?php
$title = 'chrisellsworth.net | home';
$validate = 'yes';
$content = <<< HTML
My title is $title<br />
This is some more content.
Notice I can include variables like $validate<br />
Right with my HTML, and I don't have to escape anything either.
<a href="home">$title</a><br />
HTML;
Now, when you print $content in your template, PHP will parse in the values of those other variables.
The 'HTML' which starts and ends the here document can be anything you want, it just has to be the same at the start and end. Also note that you must make sure there are no characters (that includes whitespace) after the initial HTML or the ending HTML; -- if there are, PHP will throw an error.
Hope that helps!
--
dayfax
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: May 2001
Location: New York
Status:
Offline
|
|
Cool... I'll keep that in mind.
-Chris
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|