The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

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.

Template System

Hello,
Someone a while back suggested I used a template engine for my site. Now I havn't got a clue what one is, what it does, how i can use it, and why i need to use it. Please can you explain about them and answer those queries I have. Also what one should i use and how should I start to use it. Someone suggested XiTemplate or Smarty but i really do not have a clue. I am also not that experienced in PHP.

Maybe that will give you some good ideas... actually, I highly recommend signing-up for the PHP.net general mailing list, it is very informative and the folks on the list are very helpful. Your question would probably get a lot of great responses.

I personally have not used smarty. I have read good/bad things about it. Typically, my sites are so small that I prefer not to use a bloated templating system... a simple setup typically meets my needs.

I usually build my own PHP templates from scratch, or use something like Textpattern (CMS/BLOG software).

Hmmm, let me see if I can explain how I typically set things up:

For content and/or code that will be the same on all pages, I set those up as php includes on my main php template... for example:

Header
Footer
Navigation
Sidebar

Then I setup a variable, say "$page" and all my content is then pulled-in to the page depending on the query string. Example:

index.html?page=home

Lets say that is the href for my main navigation "home" link. When clicked, index.html (the template) loads and the home page content will display because I told my PHP script to grab content from a DB or a flat html file, or even a text file.

Thats slightly how my site is done at the moment but i have tonnes of php files with the same template and then the content on those. the header, footer and navigation are brought in via includes. So are you saying i should start again with 1 page as a template and use that variable system? Also the site is www.atmania.com

Could i make page as a template and then make another page with basically all the coding from the others brought in with if $page is this or whatever?

Thats slightly how my site is done at the moment but i have tonnes of php files with the same template and then the content on those. the header, footer and navigation are brought in via includes.

Sounds good to me.

Originally Posted by rctneil

So are you saying i should start again with 1 page as a template and use that variable system? Also the site is www.atmania.coml

Cool site.

I like to minimize the amount of pages I have to update, so for me a one page PHP template is the way to go. It can be harder to setup (from scratch), but in the end much easier to maintain.

Originally Posted by rctneil

Could i make page as a template and then make another page with basically all the coding from the others brought in with if $page is this or whatever?

It sounds like you are thinking in the right direction. Basically, for me, I try to put all the PHP in the main template (includes, conditionals, security...) and then only pull-in raw html files or php files (depending on content.)

As far as your site goes...

Looks like you got a lot of content to deal with. Also, looks like you are already using page vars to load content... If you are thinking about doing things how I suggested you would have to make sure all of you links were updated... for example:

TemplateLite is a fork of Smarty that uses a lot less overhead and parses faster than Smarty. It uses the exact same syntax (with the exception of reserved 'smarty.' type notations and is super easy to implement. I'd highly recommend TemplateLite if you are looking for a template parser that works like Smarty but is not as heavy.

Me | My Family | My Design FirmMuch of what I say can be refuted or disputed.
I am just a lowly professional PHP developer.
I could be just as wrong as the next guy.

So can i use one of these cms systems in stead of a system like smarty or something? I did wonder yesterday whether to just make a template php page which has the layout, and all the normal "includes" like header, footer, navigation and then a white space where content will go and then when i link to another page use a variable like said above such as $page. The page variable will "include" the content requested which will be stored in small php files. So basically mini websites which don't look good . and when they are shown in the template you get the full pages. I don't know if this is the best way to do it. I am getting too confused with a template engine and i cannot get to grips with the coding and i'm not sure what words like parse or other things mean. Would this be a better way. I do have a mini CMS system on the site which i made myself but it will not update the whole of the site just certain areas like adding extra rides into the rides database or things like that.

you need to check the value against a list of filename you want to allow before you include it. you could start another thread once you get going.

How do you mean by that?

Also is there any way that i can have a list of include files and then if the $page value is the same as the name as one of the include files then it will include that specific file? Also is there any benefit to using this method of "include"ing the content pages into the main template page over using the system i currently have where i have all pages and everypage has the layout, template, the layout includes - header, footer, navigation and the content on them as well?

So can i use one of these cms systems in stead of a system like smarty or something?

Yes, but it depends on how much work you want to do. It kinda sounds like you want to take the easiest route possible. A cms would be a good thing, but it would probably require a major re-structuring of your site if you want it done right.

Personally, I find it more work to try an retrofit a website with new "stuff"... much easier to build from scratch... allows for a more clean website in the end. But that is just me... and sometimes one does not have that choice, which may be your case.

Originally Posted by rctneil

I did wonder yesterday whether to just make a template php page which has the layout, and all the normal "includes" like header, footer, navigation and then a white space where content will go and then when i link to another page use a variable like said above such as $page. The page variable will "include" the content requested which will be stored in small php files. So basically mini websites which don't look good . and when they are shown in the template you get the full pages.

The above article is very basic (found it via a quick google search)... security is very important, and I do not see ALA covering much of that... clamcrusher brought-up one of the important things to consider, but you will probably have to start a new thread cause that would get a bit OT here.

Originally Posted by rctneil

I don't know if this is the best way to do it. I am getting too confused with a template engine and i cannot get to grips with the coding and i'm not sure what words like parse or other things mean.

Then it probably is best for you to use a CMS and/or blog software. Unless you are ready to crack the books and RTFM, you will have best luck with something more WYSYWYG... Most CMS/blogs are setup so one can choose to not touch much (if any) of the coding.

I am talking about apps like Joomla, WordPress, Expression Engine, Django, Textpattern... so many to choose from.

Originally Posted by rctneil

Would this be a better way. I do have a mini CMS system on the site which i made myself but it will not update the whole of the site just certain areas like adding extra rides into the rides database or things like that.

I'm personally opposed to including logic in your templating engine. I think logic should be performed by your functions/methods. In my system I use html style syntax which I think makes things quite simple. My methods are generally quite flexible with their output, so there is really no need. I also include the ability to use php in templates. Also I always have the output generated by methods passed through a template too.

So I could have like <mgx:get_news template="(template id/name)" />

Ah yeah temapltes can set vairables too, so the template in the above example may set the number of items to display and such.

Hmmm, I am getting way to confused. I am currently using dreamweaver to create my site. Below is what i am going to do:

I will close my site for a week while i do the work. I will have one page called template.php which will have the look of everypage and use includes for the header, navigation and footer. I will then have hundreds of mini pages called things such as ride.php, scoreboard.php, links.php, downloads.php and these will be embedded into the template in the area left for page content using includes depending on what the current $page value is. There are to things i need to know:

how will i get template to know what include to include? Do i have to put hundreds of if statements in the template page and when i make a new conten page i have to add a new if statement or is there an easier way?

2. How will i get the page to load template.php$page=home when a user types in my domain such as www.example.com?

Np about the questions above. They were all answered in the link that mhulse gave above. I shall now go ahead and close the site and start making the changes
Too be honest they shouldn't take that long. I just have to check all links carefully.

Hey,
Completed changing all the content to be in samller pages whcih do not include the template so these pages can be "include"d on the index page. but i have a small problem. When people go to a page called www.example.com?page=parkguide they have to choose an area to view more details on using drop down boxes. These boxes are populated using a mysql database table. Below is the code i have for the box:

<?php

require "db.php";

// do query to get id to pass to address bar
$result = mysql_query("SELECT id, area, showit FROM atm_areas WHERE showit=1 ORDER BY area asc") or die('Error, query failed');
?>
</div>
<form method="get" action="index.php?page=area&areaid=">
<div align="center">
<select name="areaid">
<optgroup label="::Select an Area::">
<!--option selected="selected" value="">--Select an Area--</option-->
<?php
// go through the database and while there are different areas add them to the list dynamically
while ($row = mysql_fetch_array($result)) {
// $areaid is the variable that the area.php page will look for and then use that id to display the right data
$areaid=$row['id'];
echo '<option value="' . $areaid . '">' . $row['area'] . '</option>';