Page 1 of 1

best method to make a template?

Posted: Sun Apr 10, 2011 2:18 am
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...

Re: best method to make a template?

Posted: Sun Apr 10, 2011 9:31 am
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.

Re: best method to make a template?

Posted: Sun Apr 10, 2011 10:25 am
by k0d3r101
thanks, i think i will use the author's method for now.