WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-24-2006, 05:27 AM
WebProWorld Pro
 

Join Date: Sep 2005
Location: Manchester, UK
Posts: 257
mikesmith76 RepRank 0
Default PHP Smarty Usage

Hi all,

There are a lot of PHP programmers on this board and i'm sure some of you must use the smarty templating engine. I'm looking to use it more in the sites I develop, as I can see a definate benefit for keeping html seperate from php code. However just need a few points clarified.

At the moment I create all my site pages with a .php extension. This is mainly for future proofing, if i need to add php code i can without worrying about changing file extensions / links etc. I can see no reason to change this policy, and have had problems in the past when a static page with a html extension suddenly becomes not so static.

Now onto using smarty. Do all you developers out there that use smarty use it on every page of the site? With this model every seperate content page would need a template, with common elements in include files, right? While this seems sensible to me for database driven websites on a simple page I can't see the reason for it. However only using smarty on some pages would result in having two sets of content pages to edit when changes are needed.

Anyway just to wrap it all up, what is the general feeling from smarty users? Use smarty on all pages or only database driven / contact form type pages?

Thanks
Mike
Reply With Quote
  #2 (permalink)  
Old 02-24-2006, 03:23 PM
Easywebdev's Avatar
WebProWorld Veteran
 

Join Date: Apr 2004
Location: Donegal, Ireland.
Posts: 322
Easywebdev RepRank 1
Default

Hi Mike.
I dont use smarty rather I use a custom template engine based on phplib but the idea is the same, seperating content from code.

What I normally do is have a page_header.php and a page_footer.php file which are included in all files in my sites. The page_header.php file includes stuff like doctypes, stylesheets, all site links (relative to the document root) etc in smarty type variable forms such as {INDEX_PAGE}, anywhere in my site I want to link to the index page I just insert a href="{INDEX_PAGE}" etc.

I have a php page for all pages and then include the header and the footer php pages and a seperate file with the content for that particular page, such as index.html

My site directory structure usually looks like this - directories are encased in [ ].

[root]
index.php
contact.php
and so on.

[includes]
contains page_header.php, page_footer.php and any other common files (database info etc)

[html]
page_header.html (contains doctype, menu etc) included by page_header.php
page_footer.html - included by page_footer.php
index.html (content for the index page) - included by index.php (along with page_header.php and page_footer.php)
contact.html - included by contact.php
and so on.

Once you get used to this way of doing things then when you create a new site you will already have most of the donkey work done, just change your page_header.html file to include your doctype, layout, menu etc and go add content.

By keeping all links in smarty like variables if you do change a page name (or extension) then just update the page_header.php file and you are done.

Another reason for templating every page is that you can have the likes of an seo.php file that is included in every page and you can place meta tags in smarty like variables {META_KEYWORDS} then use a switch statement to see what page you are on and include different keywords, description meta tags for each individual page, keeping everything nice and neat in one included file.

You shouldnt end up with two files that need editing, if all your content is kept in html/page.html then that is all that needs be edited when adding/changing content as your php file just contains code to include the different elements (header, content, footer).

There are many benefits to templating all pages, I'm sure you will come up with a few that suit your own work methodology.
Reply With Quote
  #3 (permalink)  
Old 02-24-2006, 05:30 PM
DrTandem1's Avatar
WebProWorld 1,000+ Club
 

Join Date: Oct 2003
Location: Encinitas, CA
Posts: 1,908
DrTandem1 RepRank 2
Default

You can edit the .htaccess file to treat HTML pages as PHP pages. So, if you have existing HTML pages that you now find a need to add PHP scripts, you don't need to lose the traffic.
__________________
DrTandem's San Diego Web Page Design, drtandem.com
Reply With Quote
  #4 (permalink)  
Old 02-25-2006, 02:17 AM
southplatte's Avatar
WebProWorld Veteran
 

Join Date: Jul 2003
Location: Colorado
Posts: 380
southplatte RepRank 1
Default

Honestly I am just getting started with looking into templates myself, so hopefully I will get something out this too.

My question to Easywebdev is this:

If I pull my content from a DB, and I have some php db abstraction classes that do this separate from all other files, would I still need to do the variables as you put them {smarty style}?

Right now I structure my navigation for sites I create based upon one or more of several criteria - aka I have 40 links, some are marked for the home page, some for the products page, some for other pages. Then in the db class that pulls the navigation information, it only pulls for each section - so the header.inc.php would pull all header navigation simply by making a call to include/headerlinks.php.

I run all my content blocks this way to, so that I can use them from the database on any given page.

This allows me to have all content updated by myself or the client in an easy way, uses the same layouts on each page, new pages can be added easily as you just use the include() statement to include what content you want - which can have new tables for new content added at will as well.

Or is the other way a better system? I am still confused because what I have seen on many template systems is 10 files that have 30 defines in each for the content - the an {include} template tag to place that content in there. The problem I am seeing, is that to change the content you change the database, which chages the info that is returned by the db abtraction layer, which then places it in the define() statements in the .tpl or .inc files (or whichever they are setup as), which is then where the page gets the info from.

Why not just have one file that pulls the content for any given section using the db abstraction layer, and include that content in the pages at will? It removes a step from the process as far as I can tell, and still allows you to have a templated page in terms of layout/design and keeping php/html separated - I mean you still use the php include() statement, but that to me is no different than using {include} blocks that use another added step to get the content.

I may just be way off or very confused - so anyone who can effectively explain this is highly thanked in advance.
Reply With Quote
  #5 (permalink)  
Old 02-27-2006, 04:38 AM
WebProWorld Pro
 

Join Date: Sep 2005
Location: Manchester, UK
Posts: 257
mikesmith76 RepRank 0
Default

Quote:
You can edit the .htaccess file to treat HTML pages as PHP pages. So, if you have existing HTML pages that you now find a need to add PHP scripts, you don't need to lose the traffic.
DrTandem1 thank you for your reply, however this post is about using templating engines, not just using php in html files.

Easywebdev how long did it take for you to master your particular templating system? Do you find it speeds up development significantly?

I'd also be interested to hear the answers to southplatte's questions

Thanks again
Mike
Reply With Quote
  #6 (permalink)  
Old 02-27-2006, 09:42 PM
Easywebdev's Avatar
WebProWorld Veteran
 

Join Date: Apr 2004
Location: Donegal, Ireland.
Posts: 322
Easywebdev RepRank 1
Default

Billy, most template sytems I have looked at are terribly bloated, their failing is that they try to much. They may suit some people and not others. Smarty even lets you have php within your tpl file and that in my opinion defeats the purpose of a templating engine. Ideally you should have one php file that does ALL the php work before outputting anything to the browser, whether that is database interaction or just re-using common data with the use of {VARIABLE} tags.
The php include function should only include php files, never html content, that should be left to the templating engine to parse.

You are using one of the greatest templating engines out there at the moment. Phpbb, if you have never used it then please download it and look at how the template engine works in tandem with a database abstraction layer and all calculations/variable assignments are done before outputting the html.

Mike. I can't exactly remember when I converted to php (probably around 7 years ago) but I was using the same methodology of re-using code (ie, header, footer, common menu etc) via the use of SSI (server side includes).

I've put up a small five page site at www.easywebdev.com/basic_site/ username and password=webproworld where you can view source for each page and see how the page titles, meta tags etc change even though there is only one header file. You can download the package from the index page. You will then be able to take a look at the code to get a better understanding of what I said in my first reply. The template engine is only three functions (about 20 lines of code) but all it needs to do is parse {VARIABLES} within the tpl files.

Take a look at the site and code and feel free to pm me or post again here in case others use it and have questions.

As for speeding up development, yep, once you get used to re-using specific parts of your site such as the header/menu/footer then adding a new page is a 10 second cut/paste job, then add your page specific content file.
Reply With Quote
  #7 (permalink)  
Old 02-28-2006, 10:26 AM
AjiNIMC's Avatar
WebProWorld Pro
 

Join Date: Aug 2004
Location: India
Posts: 268
AjiNIMC RepRank 0
Default

I am using Smarty for last 2 years and it is cool. I can not survive with smarty now.

Look at this http://www.myicpf.com (a normal page)
http://www.myicpf.com/forums/ (a forum)
http://www.myicpf.com/partners/ (another open source application)
http://www.myicpf.com/events/ (webcalendar)
http://www.myicpf.com/wiki/ (mediawiki)

All is controlled using smarty. Without smarty it is impossible or difficult. I have a common topmenu for all these pages (rather applications).

Quote:
With this model every seperate content page would need a template, with common elements in include files, right?
You can do something like this

Commontemplate.tpl

{include file="header.tpl"}
{Variable}
{include file="footer.tpl"}

From different filed you can assign some values to the variable (basically page content), so you have common look with one template. This is very much like OOP.
Reply With Quote
  #8 (permalink)  
Old 02-28-2006, 01:32 PM
WebProWorld Pro
 

Join Date: Sep 2005
Location: Manchester, UK
Posts: 257
mikesmith76 RepRank 0
Default

Thanks to all who have replied so far, it has been a great learning experience, although I am a little interested in why so few people replied. Surely we must have more php programmers (and smarty users) on the site?

easywebdev thanks for the example site, that will be really handy when i knock together a site. And I agree with it defeating the point to put php in templates, the reasona i'm choosing smarty however is it's great plugin architecture. could save a lot of development hours in the future.
Reply With Quote
  #9 (permalink)  
Old 03-01-2006, 01:30 PM
chadhaajay's Avatar
WebProWorld Pro
 

Join Date: Dec 2003
Location: INDIA
Posts: 165
chadhaajay RepRank 1
Default Hello

Hi There,

I'd like to contribute my opinions as well as this is a kind of topic i'd love to reply back to. Many PHP developers use smart template engine and i agree with their choice but not always you need smarty. You can create your own template engine based on your application's requirements. We never used smarty for our widely used knowledge base software "phpkb professional" at http://www.knowledgebase-script.com but still it is very easy to customize.

So, the choice to use smarty depends on your type/size of app (my personal opinion).
Reply With Quote
  #10 (permalink)  
Old 03-01-2006, 08:08 PM
Easywebdev's Avatar
WebProWorld Veteran
 

Join Date: Apr 2004
Location: Donegal, Ireland.
Posts: 322
Easywebdev RepRank 1
Default Re: Hello

Quote:
Originally Posted by chadhaajay
So, the choice to use smarty depends on your type/size of app (my personal opinion).
Thats what its all about. Choosing the right tools for the job, but what is the right tool?

Personally I would not recommend smarty to anyone, I don't think it is the right tool for any job (overkill for most scenarios and confuses newbies by doing stuff they should be using php to do). In my opinion it is horribly bloated and a steep (and unneccesary) learning curve for users and you end up with a confusing multitude of files (ala Billy's post).

As I mentioned in an earlier post smarty allowing you to use php within your template files just defeats the whole purpose of templating in php. Smarty tags like AjiNIMC quoted such as {include file="header.tpl"} are absolutely nuts, why is the template engine doing the including instead of native php include('header.tpl');? when you start down that route you are beginning to write your own scripting language.

Theres a good article on php templating (using native php rather than a templating engine) here I don't entirely agree with the author but it does expose the lunacy of some template engines. Theres some good further reading at the bottom of the article.

A templating engine should only do two things, 1, parse variables (that you control the content of with php) and 2, (only for dynamic sites that have sessions/user authentication/output based on data from a database) have some form of block control with block nesting (ie, and ) that may or may not be parsed depending on the situation. Doing anything else in a template engine is crazy, php is well capable of doing everything else.

I mentioned the phpbb templating engine earlier, it is based on phplib with some additions and refinements by Nathan Codding of the phpbb group and is (imho) the best out there. It accomplishes 1 and 2 above with ease and leaves everything else to php, the only problem for new users is documentation on its use. There is none, you need to study phpbb to see it in use.

Php should be used to gather all relevant data for a page, do neccessary calculations to determine that data then (and only then) should a template engine be used to place that data within preexisting html. A maximum of three files, your php file that does the work, your html file and the template engine to bridge between the two, more than that and you are defeating the purpose of templating.

If a template engine introduces additional tags (ala, <tagname: $variable />) then it has lost the plot. Css managed the job of seperating style from content without adding new tags so should a template engine when seperating code from html.

The concept of templating in php (seperating html from code) is a good thing but smarty actually reverses that and adds extra "smarty" code within your html that adds another level of processing so you are worse off than when you began.
Reply With Quote
  #11 (permalink)  
Old 03-02-2006, 02:04 AM
southplatte's Avatar
WebProWorld Veteran
 

Join Date: Jul 2003
Location: Colorado
Posts: 380
southplatte RepRank 1
Default

Quote:
Originally Posted by AjiNIMC
You can do something like this

Commontemplate.tpl

{include file="header.tpl"}
{Variable}
{include file="footer.tpl"}

From different filed you can assign some values to the variable (basically page content), so you have common look with one template. This is very much like OOP.
I am still confused - The process, as I understand it is:

common.php = common functions, template parser code
mypage.tpl = html/formating code
mypage.php = php functions/classes/calls to db layer
mypage.htm = includes mypage.tpl (or any .tpl file, such as header, footer etc)

mypage.htm is parsed using the calls from the tpl files to files such as common.php, or functions.php or whatever (database abstraction layers, security layers etc).

So as the developer I make two pages for each page, the .tpl that makes the calls to the php functions in the common.php, db.php etc, and the actual html file that uses codes to include the information from the tpl or just use includes.

Then, the user is supposed to be able to change the look and layout fairly easily by modifying which files - the .htm, or the .tpl? I am still confused, because many systems I have tried I have had to change both.

If I make a layout using css, use OOP/OOAD to create all my backend processing - I can acheive the same thing by putting a few include() statements into a nicely developed xhtml layout. Then use one css file to change the entire look, layout and feel of the web site. Skip the need for 4 files per page type of idea, skip the need to include variables that were defined in one file that actually pulled the information from the database using the database abstraction layer.

Home.htm pulls in from home.tpl which pulls in from db.php - now the premise is to change home.tpl? or home.htm?

Why not have home.htm make a few php calls to the abstraction layer?

I am still missing the point of over bloated, 3 files to make a single page idea of template systems.

In what I do for clients, all the logic is still stored in classes, uses OOP and is merely instantiated on the home.htm page, or the products.php page for example.

I am confused on how this is very much like OOP - is the .tpl considered an object that gets instantiated to be manipulated? Or is the .htm file the object? I guess what I am trying to find out is - I make a class, create my methods in that class and variables that the class has access to. Then I make an instantiation of that class (or object) and can send/recieve necessary data and have it where I need it, when I need it. How does the templating system work like that?

==========================

Ahha! I just did a preview and read your post Easywebdev - that sums up what I was struggling with - I have left my original comments above as to how confused I was on this - until now.
Reply With Quote
  #12 (permalink)  
Old 03-02-2006, 02:32 AM
WebProWorld Pro
 

Join Date: Sep 2005
Location: Manchester, UK
Posts: 257
mikesmith76 RepRank 0
Default

southplatte there are only 2 files needed per page, for example index.php and index.tpl. Index.tpl contains all the layout stuff (xhtml / css / javascript etc) whereas the index.php file contains only php code. You link to index.php on your website. It then does any php processing, assigns the template variables and then displays the page.

The reason for doing this is the php code is completely seperated from the xhtml (if done correctly). You can completely rewrite how the php code gathers data and as long as the same variables are assigned you wouldn't even need to look at the template file. Likewise you could rip the templates apart without looking at the php coding.

Hope this clears it up a bit

Mike
Reply With Quote
  #13 (permalink)  
Old 03-02-2006, 03:38 AM
southplatte's Avatar
WebProWorld Veteran
 

Join Date: Jul 2003
Location: Colorado
Posts: 380
southplatte RepRank 1
Default

Makes sense now....Thank-you
Reply With Quote
  #14 (permalink)  
Old 03-04-2006, 06:45 AM
Faglork's Avatar
WebProWorld Veteran
 

Join Date: Feb 2005
Location: Forchheim, Germany
Posts: 947
Faglork RepRank 0
Default

FYI: There is a CMS out there based upon Smarty. I just used it for a website, and I am quite impressed at the ease of deployment. Have a look at
http://www.cmsmadesimple.org/

It is not perfect, but it may be all you need.

hth,
faglork
Reply With Quote
  #15 (permalink)  
Old 03-04-2006, 04:13 PM
WebProWorld New Member
 

Join Date: Mar 2006
Posts: 1
Feldon RepRank 0
Default Still a little lost

After a first pass through my reply I've realized I have two questions.

First, for most cases I can see how a templating engine (whether Smarty, or a simple home-brewed all-php solution) is desirable. I mean you can have a display page which contains entirely HTML interspersed with a few php variables, and another page which calculates the values of those variables. However, what are you supposed to do with forms? If the back-end coding page builds the form, or even just individual elements, how then does the templater add css to these elements? Alternatively, if the front-end template page builds the form, then the templater has effectively been given control over variable naming. Neither case seems quite right.

Second, I currently have an admin page for my site where I present the user with a series of forms that allow them to make changes to the site. Some of these are multi-page forms. For example, they can add news articles, update the member list, etc. At the moment all of these forms are contained/controlled by a single admin.php page. What would you suggest as an alternative to this format? How could I incorporate a templating engine (again either Smarty or more likely a home-brewed version)?

It seems to me that if I currently had 10 multi-page forms, for a total of say 20 pages of html forms, that I could wind up with a lot of files! i.e. a template page and the back-end code page for 20 pages = 20*2 = 40 files! Is that over-the-top?

Thanks for any suggestions.
Reply With Quote
  #16 (permalink)  
Old 03-04-2006, 09:28 PM
Easywebdev's Avatar
WebProWorld Veteran
 

Join Date: Apr 2004
Location: Donegal, Ireland.
Posts: 322
Easywebdev RepRank 1
Default

No, the scenario you have presented is one of the best usages of templating.

Lets say you have a complicated application that requires the user to submit data via a series of forms, ala your post.

You need 22 files, 1, the php file that does all your calculations, 2, the template engine, 3, the html for the 20 forms themselves.

As you process the variables from each page then you can set an error flag when you hit something you dont like/expect.

if you hit an error then resend the file again with the data already filled in via use of the $_POST global, if no error then proceed to step 2, etc, etc.

as you move from step to step you send the already verified data as hidden form data, when you reach the final step with no errors then email the data, submit it to a database, save it as a file etc.

so on page 6 you will have your form data but also a template variable such as {verified_data} which gets passed as
<input type='hidden' value="{FORM_1_NAME}" />
<input type='hidden' value="{FORM_1_EMAIL}" />
<input type='hidden' value="{FORM_2_SOCIAL_SECURITY_NUM}" />
<input type='hidden' value="{WHAT_EVER_YOU_ASKED_FOR}" />

as the form grows you just validate the data for the form you are on (pass a page variable as a hidden value) then you rewrtite what {verified_data} contains, each form will pass your verified data and let you validate the input from the form in question.

Once you have all your data then submit it to a database, save to a file, email, whatever.

In all your form files you just need 1 template variable {verified_data} that gets passed.

Its hard to explain unless you see the code and it actually working.

I did a multi page form application for a limousine company but I'm damned if I can remember the company or url, but check
http://www.northwestisp.com/contact_us.php
fill in any data you like and submit it (please dont fill in all the required data or it will be submitted) if you leave a field blank then you will get an error, try different fields left blank and you will see how using one php file and one tpl file can be re-used again and again with different output based on the submitted data.

As for css you would include that as an external file (or place it inline) in your page header.

Hope that helps.
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Tags: , ,



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Search Engine Optimization by vBSEO 3.2.0