best method to make a template?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
k0d3r101
Forum Newbie
Posts: 2
Joined: Thu Apr 07, 2011 2:52 pm

best method to make a template?

Post by k0d3r101 »

hello i am new to php.
i started learning PHP 2 days a go ( i came from c++ ).
using the book "build your own database driven website using php & mysql 4th edition"

in this book the author showed his method to a template.

he showed this through an example of a connection to mysql and outputting a string if the connection fails or succeeds


1. index.php
had the connection form
and he assigned the string to an output variable.

2.output.htm.php
had the template (html page)
and had an

Code: Select all

<?php
echo $output
?>
my question is whats the difference and wich method is better his method
or just 2 pages one is index.php which have an include statement to connection.php and a connection.php page which have the connection script?
thanks in advance...
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: best method to make a template?

Post by cpetercarter »

It is easy with php to write code where logic is mixed in with echo statements which output bits of html. But the resulting spaghetti can be difficult to understand or maintain. It is much better to keep logic and presentation separate. Then, if you want to, you can completely change the presentation (or even hand it to a web designer to do it for you) while leaving the logic untouched. or change the logic without having to revise the presentation. I guess that the author of the book you are using is introducing the basic idea of "templating" as a way of keeping logic and presentation separate. Large php projects may use a special templating library such as Smarty, or php frameworks such as Codeigniter which effectively enforce a logic/presentation separation.
k0d3r101
Forum Newbie
Posts: 2
Joined: Thu Apr 07, 2011 2:52 pm

Re: best method to make a template?

Post by k0d3r101 »

thanks, i think i will use the author's method for now.
Post Reply