
04-23-2008, 03:53 PM
|
|
WHT Addict
|
|
Join Date: Oct 2003
Posts: 132
|
|
Help with using variables in a PHP template system?
I'm using PHP to call header and footers for my site design:
Code:
<?php
$page_title = 'PAGE TITLE HERE';
include $_SERVER['DOCUMENT_ROOT'] . '/header.html';
?>
...and that header has this to set the title:
Code:
<title><?php echo $page_title; ?></title>
However, I also would like to incorporate meta tag info for each page rather than in the template...I have no idea how I would set that in the PHP file or call it in the HTML header. Suggestions?
Also, how can I do something similar with an image that changes with each page but it's placement is consistant in the layout? I would assume the method for setting the title I'm using can be applied but I just don't know how.
Any help is appreciated.
|

04-23-2008, 07:29 PM
|
|
Radiofreak for life
|
|
Join Date: Aug 2005
Location: The Netherlands
Posts: 3,198
|
|
I think this all would be much easier with a ready-made system like WordPress or CMS Made Simple. WebsiteBaker does the job as well.
__________________
██ Co-owner EclecticRadio | Electronic Music Radio
██ WE MAKE YOU DANCE!
██ Visit us @ www.eclecticradio.nl
|

04-24-2008, 12:05 AM
|
|
Junior Guru Wannabe
|
|
Join Date: Dec 2006
Posts: 76
|
|
You can set the meta tags in the same way you set the Title, as they also have to be in the <head> section. You can also do the same with the image. I am assuming that you have your header looks like:
PHP Code:
<head>
<?php
<title>$title</title>
?>
</head>
and your page looks like
PHP Code:
<?php
$title = "Page 2";
include ("head.php");
?>
|

04-24-2008, 09:26 AM
|
|
WHT Addict
|
|
Join Date: Oct 2003
Posts: 132
|
|
Quote:
Originally Posted by amygdela
I think this all would be much easier with a ready-made system like WordPress
|
Not in the least.
Quote:
Originally Posted by computerwiz3491
You can set the meta tags in the same way you set the Title, as they also have to be in the <head> section. You can also do the same with the image. I am assuming that you have your header looks like:
PHP Code:
<head>
<?php
<title>$title</title>
?>
</head>
and your page looks like
PHP Code:
<?php
$title = "Page 2";
include ("head.php");
?>
|
That's pretty close. And yes I do realize that I can do it I'm just wondering what the code would look like...?
|

04-24-2008, 09:39 AM
|
|
WHT Addict
|
|
Join Date: Oct 2003
Location: 2nd star to the right
Posts: 135
|
|
You would so it the exact same way as the title.
Here is how I'd do it. I am big fan of functions to keep my mainline code looking clean
PHP Code:
<?php
// call your two functions to do the work
build_title();
build_metatags();
// do content work here
// put out the footer
build_footer();
exit;
function build_title()
{
$title = "My awesome page title goes here";
echo "<title>$title</title>";
}
function build_metatags()
{
$metakw = "computer, case, keywords, here";
$metadesc = "Computer Case Home Page";
echo '<meta name="description" content="' . $metadesc . '">'
echo '<meta name="keywords" content="' . $metakw . '">'
}
function build_footer()
{
echo "Here is where your footer goes";
}
?>
...and PS something like this from the other poster will not quite work:
PHP Code:
<?php
<title>$title</title>
?>
PHP will try to interpret the <title> tag and will fail. That code should have probably been:
PHP Code:
<?php
echo "<title>$title</title>";
?>
Try it all out and let us know how it goes.
|

04-24-2008, 12:55 PM
|
|
WHT Addict
|
|
Join Date: Oct 2003
Posts: 132
|
|
Thanks Patrick. Is there a way to simplify that? Here's how my pages are set up:
Code:
<?php
$page_title = 'PAGE TITLE';
include $_SERVER['DOCUMENT_ROOT'] . '/header.html';
?>
<!-- CONTENT -->
<?php
include $_SERVER['DOCUMENT_ROOT'] . '/footer.html';
?>
Could I just set that up so the meta info is part of that opening PHP tag? Something like:
Code:
<?php
$page_title = 'PAGE TITLE';
$page_metadesc = 'STUFF';
$page_metakey = 'STUFF';
include $_SERVER['DOCUMENT_ROOT'] . '/header.html';
?>
And if so how would I call those in the header?
Only reason I ask is because I have a few sites with thousands of pages that I've updated for this new layout. Your example definitely has more flexibility but the thought of rewriting all of those pages again gives me chills.
|

04-24-2008, 01:54 PM
|
|
Junior Guru Wannabe
|
|
Join Date: Dec 2006
Posts: 76
|
|
You would do it the same way you did the title.
Your header file would look like
PHP Code:
<?php print "<title>$page_title<title> <meta name=\"description\" content=\"$page_metadesc\"> <meta name=\"keywords\" content=\"$page_metakey\">"; ?>
And your main page would indeed look like:
Quote:
PHP Code:
<?php $page_title = 'PAGE TITLE'; $page_metadesc = 'STUFF'; $page_metakey = 'STUFF'; include $_SERVER['DOCUMENT_ROOT'] . '/header.html'; ?>
|
|

04-24-2008, 02:06 PM
|
|
WHT Addict
|
|
Join Date: Oct 2003
Posts: 132
|
|
Quote:
Originally Posted by computerwiz3491
You would do it the same way you did the title.
Your header file would look like
PHP Code:
<?php print "<title>$page_title<title> <meta name=\"description\" content=\"$page_metadesc\"> <meta name=\"keywords\" content=\"$page_metakey\">"; ?>
And your main page would indeed look like:
|
Ah - many thanks. I tried to adapt what he showed me initially but forgot the backslashes. I will try that.
|

04-24-2008, 02:19 PM
|
|
WHT Addict
|
|
Join Date: Oct 2003
Posts: 132
|
|
Well, actually the head in the header.html looks like this:
Code:
<head>
<meta name="robots" content="index,follow" />
<title><?php echo $page_title; ?></title>
<link href="/maincss.css" rel="stylesheet" type="text/css" />
</head>
So would I still need to use print or can I just do something like...
Code:
<head>
<meta name="robots" content="index,follow" />
<title><?php echo $page_title; ?></title>
<meta name=\"description\" content=\"$page_metadesc\">
<meta name=\"keywords\" content=\"$page_metakey\">
<link href="/maincss.css" rel="stylesheet" type="text/css" />
</head>
...?
|

04-24-2008, 03:43 PM
|
|
WHT Addict
|
|
Join Date: Oct 2003
Location: 2nd star to the right
Posts: 135
|
|
Also with PHP there is shortcut so you do not have to use a full echo statement
PHP Code:
<?=$page_title ?>
is the same as
PHP Code:
<? echo $page_title; ?>
The shorter version is easier to drop in the middle of some HTML.
|

04-24-2008, 04:33 PM
|
|
Junior Guru
|
|
Join Date: Feb 2006
Location: top: 50px; left: 200px;
Posts: 213
|
|
Quote:
<head>
<meta name="robots" content="index,follow" />
<title><?php echo $page_title; ?></title>
<meta name=\"description\" content=\"$page_metadesc\">
<meta name=\"keywords\" content=\"$page_metakey\">
<link href="/maincss.css" rel="stylesheet" type="text/css" />
</head>
|
no need to escape the quotes in the html page.?
You could use arrays for the meta info so in the top of your index.php file you would define the array:
Quote:
|
$page_metakey = array(1 => 'word1', 'word2', 'word3');
|
and then echo the array in the html:
Quote:
|
<meta name="keywords <?=$page_metakey() ?>" />
|
Desription can be another normal variable.
and your image can also be a noraml variable:
Quote:
|
$page_image = "imageURL.jpg";
|
Quote:
|
<img src="<?=page_image ?>" />
|
|

09-29-2008, 03:53 PM
|
|
WHT Addict
|
|
Join Date: Oct 2003
Posts: 132
|
|
Quote:
Originally Posted by computerwiz3491
You would do it the same way you did the title.
Your header file would look like
PHP Code:
<?php
print "<title>$page_title<title>
<meta name=\"description\" content=\"$page_metadesc\">
<meta name=\"keywords\" content=\"$page_metakey\">";
?>
And your main page would indeed look like:
|
I revisted this problem and tried the above solution, but unfortunately I got a blank page. I checked the page source and everything looks right, and the page renders fine under the "old" system. Not sure if there's something wrong with this suggestion that's maybe a little outside of my grasp? 
|

09-30-2008, 12:05 PM
|
|
Newbie
|
|
Join Date: Jul 2008
Posts: 15
|
|
I got a quick question. Are you putting the variables on the page you want it to be displayed on like this?
Index.php
<?php
$page_title = 'PAGE TITLE';
$page_metadesc = 'STUFF';
$page_metakey = 'STUFF';
include $_SERVER['DOCUMENT_ROOT'] . '/header.html';
?>
Header.php
<?php
print "<title>$page_title<title>
<meta name=\"description\" content=\"$page_metadesc\">
<meta name=\"keywords\" content=\"$page_metakey\">";
?>
If so it seems kind of redundant to me. You are already writing your code on the page and then asking it to be added into the header.php. I might be reading this wrong, but thus far it seems like that is what you are doing. I can help you out, just trying to figure out exactly what your goal is.
|

09-30-2008, 03:24 PM
|
|
WHT Addict
|
|
Join Date: Oct 2003
Posts: 132
|
|
Quote:
Originally Posted by Marc_W3Solutions
I got a quick question. Are you putting the variables on the page you want it to be displayed on like this?
Index.php
<?php
$page_title = 'PAGE TITLE';
$page_metadesc = 'STUFF';
$page_metakey = 'STUFF';
include $_SERVER['DOCUMENT_ROOT'] . '/header.html';
?>
Header.php
<?php
print "<title>$page_title<title>
<meta name=\"description\" content=\"$page_metadesc\">
<meta name=\"keywords\" content=\"$page_metakey\">";
?>
If so it seems kind of redundant to me. You are already writing your code on the page and then asking it to be added into the header.php. I might be reading this wrong, but thus far it seems like that is what you are doing. I can help you out, just trying to figure out exactly what your goal is.
|
That looks right. It's not redundant though (at least it's not supposed to be) because the header (along with the footer, both of which are HTML files, not PHP) wraps around the PHP file. There are dozens or hundreds of PHP files throughout the site (index.php, contact.php, about.php, etc.). If I leave place in the header template file for information specific to each page, specified in the page itself, it allows me to only have to use one template file for the whole site, and all of the meta and title info specific to a page is kept in that page. Hopefully that makes sense.
Right now I'm not using meta tags, but I can give an example of what the title looks like:
header.html
Code:
<!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" />
<meta name="robots" content="index,follow" />
<title><?php echo $page_title; ?></title>
<link href="maincss.css" rel="stylesheet" type="text/css" />
</head>
<body>
index.php
Code:
<?php
$page_title = 'The title of the page goes here';
include ('header.html');
?>
<p>Any of the page content goes here, including text, images, etc.</p>
<?php
include ('footer.html');
?>
It works amazingly well, I was just hoping to be able to decide for each page if I want the page indexed, followed, set descriptions, etc.
|

09-30-2008, 09:35 PM
|
|
Newbie
|
|
Join Date: Sep 2005
Posts: 11
|
|
Have you considered Smarty template engine?
http://www.smarty.net/
Once you get the hang of it it's very simple to use. It also increases load times and makes it easier for designers and programmers to work on the same project.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
| Postbit Selector |
|
|
Posting 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
|
|
|
|
|
|
| Login: |
|
|
| Advertisement: |
|
|
| Web Hosting News: |
|
|
|