Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > PHP Problems

PHP Problems
Thread Tools
Forum Regular
Join Date: May 2001
Location: New York
Status: Offline
Reply With Quote
Feb 3, 2002, 05:40 AM
 
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>
&lt;?php
$title = 'chrisellsworth.net | home';
$validate = 'yes';
$content = '
&lt;!--Begin Content--&gt;
&lt;div class=<font color = red>"center"</font>&gt;
&lt;div style=<font color = red>"font-size : 32px"</font>&gt;
chrisellsworth.net&lt;br /&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;!--End Content--&gt;
';
include 'template.php';
?&gt;
</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>
&lt;!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>&gt;</font>
&lt;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> &gt;</font>
&lt;head&gt;
&lt;title&gt;&lt;?php echo $title;?&gt;&lt;/title&gt;
&lt;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> /&gt;
&lt;link rel=<font color = red>"stylesheet"</font> href=<font color = red>"/stylesheet.css"</font> type=<font color = red>"text/css"</font> /&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=<font color = red>"navbar"</font>&gt;
&lt;a href=<font color = red>"/"</font>&gt;home&lt;/a&gt;
| &lt;a href=<font color = red>"/computers"</font>&gt;computers&lt;/a&gt;
| &lt;a href=<font color = red>"/contact"</font>&gt;contact&lt;/a&gt;
| &lt;a href=<font color = red>"/stuff"</font>&gt;stuff&lt;/a&gt;
&lt;/div&gt;
&lt;div id=<font color = red>"content"</font>&gt;&lt;?php echo $content;?&gt;
&lt;/div&gt;
&lt;div class=<font color = red>"center"</font>&gt;
&lt;br /&gt;
&lt;?php if ( $validate == <font color = red>"yes"</font> ) {
echo( '&lt;a href=<font color = red>"http:<font color = brown>//validator.w3.org/check/referer"</font>&gt;&lt;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> /&gt;&lt;/a&gt;
&lt;a href=<font color = red>"http:<font color = brown>//jigsaw.w3.org/css-validator/validator?uri=http://www.chrisellsworth.net/stylesheet.css"</font>&gt;&lt;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> /&gt;&lt;/a&gt;' );
} else {
echo( '&lt;a href=<font color = red>"http:<font color = brown>//jigsaw.w3.org/css-validator/validator?uri=http://www.chrisellsworth.net/stylesheet.css"</font>&gt;&lt;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> /&gt;&lt;/a&gt;' );
}
?&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</font>[/code]

-Chris
     
Mac Elite
Join Date: Jan 2001
Status: Offline
Reply With Quote
Feb 3, 2002, 11:50 AM
 
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 ]
     
cElls  (op)
Forum Regular
Join Date: May 2001
Location: New York
Status: Offline
Reply With Quote
Feb 4, 2002, 03:11 AM
 
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
Reply With Quote
Feb 8, 2002, 05:05 AM
 
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.
     
cElls  (op)
Forum Regular
Join Date: May 2001
Location: New York
Status: Offline
Reply With Quote
Feb 8, 2002, 05:25 AM
 
Thanks for the tip.

-Chris
     
Fresh-Faced Recruit
Join Date: Jan 2002
Status: Offline
Reply With Quote
Feb 9, 2002, 03:18 PM
 
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)

&lt;?php
$title = 'chrisellsworth.net | home';
$validate = 'yes';
$content = &lt;&lt;&lt; HTML
My title is $title&lt;br /&gt;
This is some more content.
Notice I can include variables like $validate&lt;br /&gt;
Right with my HTML, and I don't have to escape anything either.
&lt;a href="home"&gt;$title&lt;/a&gt;&lt;br /&gt;
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
     
cElls  (op)
Forum Regular
Join Date: May 2001
Location: New York
Status: Offline
Reply With Quote
Feb 9, 2002, 04:08 PM
 
Cool... I'll keep that in mind.

-Chris
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:03 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2