Forum Moderators: coopster

Message Too Old, No Replies

Creating PHP Template & assigning text to variable

         

tommychops

2:05 pm on Mar 7, 2011 (gmt 0)

10+ Year Member



Hi,

I've designed a website for a friend which has been built around a Dreamweaver template. The templates no good as they want to update the site themselves and have NO coding experince (HTML included) so I'd like to start again, this time using PHP to construct my template.

I'm relatively new to PHP and have had a good look around this forum but my main issue seems to be getting the individual content of each page into my template file.

I'd like to assign each page's content to a vaiable named $content which is called by the template.

Here's my template.php file:

<!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" />
<link rel="stylesheet" type="text/css" href="../css/mainstyle.css" />
<meta name="description" content="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
<title><?php echo $title; ?></title>
</head>
<body>
<div id="border">
<div id="container">
<?php @include('header.php');?>
<div id="pageheader"><h2><?php echo $page_header; ?></h2></div><!-- end pageheader -->
<div id="content">
<div id="textarea"><?php include $content; ?></div><!-- end textarea -->
<div id="newsarea">
<?php @include('showcase.php');?>
</div><!-- end newsarea -->
<div class="clear"></div>
</div><!-- end content -->
<?php @include('footer.php'); ?>
</div><!-- end container -->
</div><!-- end border -->
</body>
</html>


Here's an example of one of the pages (eg: faq.php)

<?php

// Variables - change these to suit each individual page
$title = "page title here";
$description = 'description here';
$keywords = 'keyword1,keyword2';
$page_header = 'Frequently Asked Questions (FAQ)';
$content = content for site to go here. (will include html tags)

include('includes/template.php');
?>

I'm also having a problem with the site not applying the stylesheet that's attached to it.

Any help would be much appreciated.

g1smd

2:12 pm on Mar 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



href="../css/mainstyle.css"

This style of linking will mean the stylesheet will fail to load if the page calling the stylesheet is not enough, or is too many, levels down the folder structure. The leading ".." is the problem.

Use a URL reference that begins with a leading slash (and therefore counts from the site root) and the code will always work, no matter what the URL of the calling page. It is the browser that works out what URL to request.


href="/css/mainstyle.css"

jspeed

4:00 pm on Mar 7, 2011 (gmt 0)

10+ Year Member



im not totally sure i know what youre going for. are you attempting to load the content/html into a text area and allow the client to edit it? if so you could utilize something like ckeditor [ckeditor.com ] or a similar wysiwyg html editor.

rocknbil

5:33 pm on Mar 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are many ways to do this, but compare this

<meta name="description" content="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
<h1><?php echo $page_header; ?></h1> <!-- note this should be an H1 -->

with this

<div id="textarea"><?php include $content; ?></div><!-- end textarea -->

You **almost** have it. At the top of your script you can do

include('get_article.php');

and in get_article is where you would populate $keywords, $description, $page_header, and $content - then change $content to

<div id="textarea"><?php echo $content; ?></div>

This is still a "high maintenance" approach; another is to create a template file with placeholders, like

<meta name="description" content="[META_DESC]" />
<meta name="keywords" content="[META_KWDS]" />
<h1>[PAGETITLE]</h1> <!-- note this should be an H1 -->

and

<div id="textarea">[PAGE_CONTENT]</div>

Then store the template in a plain text or HTML file. Your script would then open up the template and replace [META_DESC], [META_KWDS], [PAGETITLE], and [PAGE_CONTENT] with $description, $keywords, $page_header, and $content, respectively. The advantage of this method is it would allow your client to design their templates at will, with very little training, they just have to know the placeholders.

I'd start off with the first method, then when it's working, attempt the second.

When doing this, don't forget this one:

<title>[PAGE_TITLE]</title>

Page titles should be unique and have the ability to be different than the level 1 heads. It's an SEO thing, but if they are designing in DW it's likely they won't care a lot . . . at least, not yet.

sundaridevi

9:01 pm on Mar 7, 2011 (gmt 0)

10+ Year Member



If I understand correctly, the problem is that you aren't getting any values to load into your page template.

In that case maybe it is because the varibles are out of scope? In php5 registerGlobals is off by default. If they were on, your code would work. As it is, you must register the variables as session variables and then start your page with this as the very first line in your page.

<?php
session_start();
?>

tommychops

9:49 pm on Mar 7, 2011 (gmt 0)

10+ Year Member



Thanks everyone for their help so far.

Just so you know, the div "textarea" is just the name i gave the div to store the pages main content.

Also, the variables $page_header, $title, $description & $keyword are all working fine.

To give people a better idea. I came across this comment in a post on this forum by jatar_k

------------------------------------------------------
by the same token you can also put the header and footer code into a single page called template.php with

include $content;

in the middle. Then you just do this on every page.

$content = "pathtocontentpage.html";
include "template.php";

and voila, the template echo the page set to content
---------------------------------------------------------

The part that gets me confused is where do you put the

$content = 'page.html';
include('template.php');

statements?

Anywho I'll keep playing around and see what I come up with.

tommychops

5:27 am on Mar 8, 2011 (gmt 0)

10+ Year Member



I've opted for a much more simplistic approach.

I've settled for a template.php file made up of several includes and from that file I simply add the content for each page manually, saving each file as a separate php file (eg: index.php, about.php, contact.php, etc...)

Probably not the most effecient or correct way to do it but it works.