Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

is this the right place to post questions or what?

anyway i'm trying to make a simple template system using php,
here is:
a page using the template: http://www.yehiaeg.com/focusonvbs/test.php
the header: http://www.yehiaeg.com/focusonvbs/temp1.inc
the footer: http://www.yehiaeg.com/focusonvbs/temp2.inc

but don't know why the page using the template is messed up,

it should like this http://www.yehiaeg.com/focusonvbs/desc.htm ,
any ideas?


Re: Templates

Quoted text here. Click to load it

Your problem is with the HTML, not the PHP.  Delete the </table> tag at the
end of temp1.inc, and that should fix the biggest layout errors.

Chris Finke

Re: Templates

actually the html work well:
http://www.yehiaeg.com/focusonvbs/version.htm ,

and here is the contents of test.php :
<title>YehiaEg &gt; Products &gt; Focus On VBS &gt; Download</title>
 include 'temp1.inc';
<p class="NormalFont"><br>
        Latest version: 1.5<br>
        Size: 509 KB <br>
        License: Demo<br>
         Requirements:  None<br>
         Download Link: <a
href="http://www.yehiaeg.com/focusonvbs.exe ">Click Here</a>
       <strong>Note:</strong> "Save /As", "Cut", "Copy", are disabled
in this demo version.<br><br>
       Available also on <a
href="http://www.download.com/Focus-on-VBS/3000-2352-10223282.html?tag=lst-0-1 "><img
src="cnet.gif" width="127" height="27" border="0"></a>
 include 'temp2.inc';
All i did was just opening the original htm file and copy every before
the main text in temp1.inc and everything after in temp2.inc ,

Don't know what i'm doing wrong, PLZ help guys.

Quoted text here. Click to load it

Re: Templates

Quoted text here. Click to load it

well without seeing your code, it is hard to tell, exept that you have got
something wrong :) . Have you considered using some existing template system
for php?

http://www.templatetamer.com /

Re: Templates

Quoted text here. Click to load it
If it's any help I would recommend a different method, not necessarily
better for everyone, but for me.

Create a HTML template in your authoring packaging, preferably using CSS.

Then in place your own tags anywhere on the page, where you would like to
swap the tag for some variable.

e.g. } or }

Then save that as a file called template.htm

Then read your template.htm file in as array, get your pagetitle and
pagetext (+ other variables) from your database (I assume) and use
preg_replace to change over your tags to the appropriate string. (see
below).  Then you just have to output your page!


$template = "";
$temp = file($setuptemplate);
foreach($temp as $line) { $template .= $line; }

// update template with menus and content
$pat = array("/}/s",
$rep = array("$pagetext",
// replace all occurrences of search patterns with according replacements

$template = preg_replace($pat, $rep, $template);

echo $template;

Re: Templates

Forgot to say, main advantage of preg_replacing your own tags is that you
don't have to split your template in to a header and footer, so you can
change the template simply by loading any new html page containing your


Re: Templates

Quoted text here. Click to load it

      Templates are one of the most controversial topic, at least in
this group. For example,

* "Using a template system is a little like going back to using PHP
1--you get a far less powerful, less flexible system to work with." --
phpSt.Chung Leong
* "What about PHP ?-)" -- phpSt.Bruno Desthuilliers

  Also see:
   1. http://www.phpbuilder.com/annotate/message.php3?id=1013434
   2. http://www.phpbuilder.com/annotate/message.php3?id=1013711
   3. http://www.phpbuilder.com/annotate/message.php3?id=1013461
   4. http://www.phppatterns.com/index.php/article/articleview/4/1/1 /
   5. http://www.massassi.com/php/articles/template_engines /
   6. http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf

 |  Just another PHP saint  |
Email: rrjanbiah-at-Y!com

Re: Templates

Yehia wrote:
Quoted text here. Click to load it

Have you thought of using the Smarty template system?
http://smarty.php.net /

It's really easy to get going with it and does what Nel has recommended.

Re: Templates

don't want to use any third party code, i'm happy with the php include function,

anyway, still want to know why my code doesn't work!

Quoted text here. Click to load it

Re: Templates

Yehia wrote:

Quoted text here. Click to load it

Okay, how about doing the reverse of what you're trying to do?

The way I do simple "include style" templates is this:

You've got the HTML template you'd like to use, call that 'index.php'

Insert this code at the top:
   if (strlen($strPage)==0) //This is your default page
   if (!file_exists("pages/.php"))

Create a folder called 'pages' in the folder of 'index.php'
Place your pages in that folder, all with a '.php' extension.
Make sure you make one called "404.php" for when a user tries a URL
where a page is missing.
In 'index.php' replace the content area of your template with:
   <?php include ("pages/.php"); ?>

 From then on, you can access pages by using 'index.php?p=pagename'
This saves you from using two pages for your template.

Things to keep in mind:
   When submitting a FORM using METHOD="GET", make sure you have a INPUT
tag of TYPE="HIDDEN", NAME="p", and VALUE="pagename" (If you don't then
you'll not submit to the page you want to)
   You can't set headers/cookies in your pages, as output takes place
before the page is included, though there are many ways of working
around it..

Re: Templates

Quoted text here. Click to load it


Tim Van Wassenhove <http://home.mysth.be/~timvw

Re: Templates

Tim Van Wassenhove wrote:

Quoted text here. Click to load it
if (strlen($strPage)==0 || substr($strPage, "..")!==false)
//This is your default page
if (!file_exists("pages/.php"))

Re: Templates

neur0maniak wrote:

Quoted text here. Click to load it
urgh... replace substr with strpos

Re: Templates

ok, i did  copy them EXACTLY and it kinda worked but still have some
1.when using javascript in a php file should i write like this
<script language="javascript> <!-- some function --> </script>
or like this <script language="javascript"> some function </script>

2. somethings may vary from one page to another besides its content
like the page title, what is the simplest way to do this?
i read about sending arguments to the php file and then assign to the
page title, but this not really neat, what if i want to make it a long
test.php?pagetitle=This a really long title, really long
Anyother wahy to do this?

my questions might look stupid but i'm just really a beginner in php,

Quoted text here. Click to load it

Re: Templates

Yehia wrote:
Quoted text here. Click to load it

1. Either will work, the commented version is better for browsers though.

2. Create another folder (like pages, but call this one 'headers' or
such. Then add this code underneath where $strPage is assigned:

if (file_exists("headers/.php"))

The .php in the headers folder would need to be pure php (no out put, no
  empty spaces before/after the PHP tags).
Inside the PHP, you could have
<?php $strTitle = "Different Title"; ?>

Then, in your template page where you set your title, you could have
something like this:
$strTitle;}else{print "Normal Title"; ?></TITLE>

The file in the headers folder will need the same filename as the file
in the pages folder. You don't need to put a file in the headers folder,
for each one you have in your pages folder, just ones that change something.

This way you can put anything that changes headers or adds cookies on a
page using the file in the headers folder.

Site Timeline