hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Web Design and Content : Help with using variables in a PHP template system?
Reply

Web Design and Content Subjects include, HTML, graphics, editors, CSS, Flash, graphics creation, placing of ads, ad serv companies, copyright, content and nearly anything else design related. Also talk about businesses that provide design services. If you link to your site, you must post in Web Site Reviews.
Forum Jump

Help with using variables in a PHP template system?

Reply Post New Thread In Web Design and Content Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 04-23-2008, 03:53 PM
emills01 emills01 is offline
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.

Reply With Quote


Sponsored Links
  #2  
Old 04-23-2008, 07:29 PM
JayNL JayNL is offline
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

Reply With Quote
  #3  
Old 04-24-2008, 12:05 AM
computerwiz3491 computerwiz3491 is offline
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");
?>

Reply With Quote
Sponsored Links
  #4  
Old 04-24-2008, 09:26 AM
emills01 emills01 is offline
WHT Addict
 
Join Date: Oct 2003
Posts: 132
Quote:
Originally Posted by amygdela View Post
I think this all would be much easier with a ready-made system like WordPress
Not in the least.

Quote:
Originally Posted by computerwiz3491 View Post
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...?

Reply With Quote
  #5  
Old 04-24-2008, 09:39 AM
patrick24601 patrick24601 is offline
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.

__________________
allaboutfocus.com

Reply With Quote
  #6  
Old 04-24-2008, 12:55 PM
emills01 emills01 is offline
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.

Reply With Quote
  #7  
Old 04-24-2008, 01:54 PM
computerwiz3491 computerwiz3491 is offline
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';
?>

Reply With Quote
  #8  
Old 04-24-2008, 02:06 PM
emills01 emills01 is offline
WHT Addict
 
Join Date: Oct 2003
Posts: 132
Quote:
Originally Posted by computerwiz3491 View Post
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.

Reply With Quote
  #9  
Old 04-24-2008, 02:19 PM
emills01 emills01 is offline
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>
...?

Reply With Quote
  #10  
Old 04-24-2008, 03:43 PM
patrick24601 patrick24601 is offline
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.

__________________
allaboutfocus.com

Reply With Quote
  #11  
Old 04-24-2008, 04:33 PM
killapix killapix is offline
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 ?>" />

__________________
Filefeed.net - Image Hosting -

Reply With Quote
  #12  
Old 09-29-2008, 03:53 PM
emills01 emills01 is offline
WHT Addict
 
Join Date: Oct 2003
Posts: 132
Quote:
Originally Posted by computerwiz3491 View Post
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?

Reply With Quote
  #13  
Old 09-30-2008, 12:05 PM
Marc_W3Solutions Marc_W3Solutions is offline
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.

Reply With Quote
  #14  
Old 09-30-2008, 03:24 PM
emills01 emills01 is offline
WHT Addict
 
Join Date: Oct 2003
Posts: 132
Quote:
Originally Posted by Marc_W3Solutions View Post
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.

Reply With Quote
  #15  
Old 09-30-2008, 09:35 PM
netlinkie netlinkie is offline
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.

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
ITX Design Launches Service to Help Simplify PCI DSS Compliance Web Hosting News 2013-01-02 10:59:36
Web Host eApps Offers Control Panel ISPmanager for Free Web Hosting News 2011-12-07 21:48:20
Web Host GlowHost Offers Tutorials, Template for Resellers Web Hosting News 2011-11-29 20:44:14
SingleOS Launches Fuscan Linux Cloud 2.0 Web Hosting News 2011-08-12 19:09:05
Web Host SoftLayer Launches Public Image Repository for Cloud Templates Web Hosting News 2011-06-08 14:56:19


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?