|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
Simple Template
Alright, following on from this thread and the discussion that's happening here, here's Simple Template (hopefully lastcraft will excuse my riding on the back of the Simple Test name but this really is pretty simple).
The ZIP attached has all the code - 90% is examples plus there's an extensive README. Here's the simplest example of Simple Template is use; The controller; PHP Code:
PHP Code:
Here's one more example of a template that has a loop in it; PHP Code:
|
|
|
|
|
|
#2 |
|
FreeBSD The Power to Serve
![]() Join Date: Jul 2001
Location: Italy
Posts: 4,568
|
Harry,
what do you think to use an object $obj so you will have something like: PHP Code:
Andrea |
|
|
|
|
|
#3 | |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
Quote:
|
|
|
|
|
|
|
#4 |
|
SitePoint Zealot
![]() ![]() Join Date: Aug 2002
Posts: 191
|
IMHO: that's the way to go with templating and PHP - KISS .
I've been doing the same as this for my last project ( not OO, but the same concept ) and as Harry says: given a brief intro to PHP anyone doing design should be able to do this fairly quickly! |
|
|
|
|
|
#5 | |
|
SitePoint Victim
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Apr 2003
Location: London
Posts: 2,399
|
Hi.
Quote:
. In fact I am kicking myself for choosing such a dumb name! Yours is much more appropriate.yours, Marcus.
__________________
Marcus Baker Testing: SimpleTest, Cgreen, Fakemail Other: Phemto dependency injector Books: PHP in Action, 97 things |
|
|
|
|
|
|
#6 |
|
Non-Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2003
Posts: 5,788
|
HarryF - Thanks for sharing your scripts;
As to using objects in the Template this is exactly the idea I have myself, so you use getter's to get the data from an object passed from the MODEL. I suppose there is no real way of avoiding the issue of having PHP in a Template other than using something like Smarty style tags which is an unnessacary overhead. Myself, I suppose I just need to get used to this idea; either that or use my previous methods of an XML document and XSL stylesheet or use the DOM to dynamically create the page; The first option is said to be slower and the second option is limited to the need to change the page structure for example; something a designer is unable to do ![]() Umm... If only there was another option huh ? |
|
|
|
|
|
#7 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Oct 2001
Posts: 666
|
You know what would make a template system like this perfect? If there were some sort of function in PHP that would enable safe mode for only one file. So the template engine could tell PHP that the template file can only use 'safe' PHP functions, such as foreach(), str_replace, etc and it would be banned from using functions such as exec().
|
|
|
|
|
|
#8 | |
|
SitePoint Zealot
![]() ![]() Join Date: Jun 2002
Location: Brazil
Posts: 146
|
Quote:
![]() |
|
|
|
|
|
|
#9 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Oct 2002
Location: Edinburgh, UK
Posts: 1,750
|
Franly speaking, I don't like it. I dislike such loops mixed up with HTML. I like phpBB template system with
<!-- BEGIN BLOCK> and <!-- END BLOCK --> tags. |
|
|
|
|
|
#10 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Oct 2001
Posts: 666
|
oh oh, there we go again.. :O
|
|
|
|
|
|
#11 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Oct 2002
Location: Edinburgh, UK
Posts: 1,750
|
I put repeatting blocks to sepearetes files, the I open them befre going thorugh a loop, in the loop, I parse that file and show it or assign to some other variable.
Anyway, Happy Birthday To You, Harry!!! |
|
|
|
|
|
#12 |
|
SitePoint Zealot
![]() ![]() Join Date: Jul 2003
Location: Mainz, Germany
Posts: 122
|
Prettier Looping
We're using a modified AwesomeTemplateEngine on our project that has remarkable resemblance to your SimpleTemplate, so I thought I'd just share this little trick to keep the webdesigners happy:
To make the templates more readable, use PHP's "alternative" syntax for control structures. Your "Loop" example would then look like this: PHP Code:
On a side-note: I prefer the "<?=" syntax for readability. Most servers have the short_open_tags set to on anyway, and I don't find that setting it to off gives me any advantage, because I still can't use "<?xml" inline or my script will break when put on a server with short_open_tags on. |
|
|
|
|
|
#13 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Aug 2002
Location: Ottawa, Ontario, Canada
Posts: 215
|
I prefer "<?=;?>" as well. As a matter of fact, every single embeded php "echo" type statement in all HTML code I have written at work uses the short syntax. It is simple, fast, and to the point ;p
I do however, disagree on using the alternative syntax instead of braces. But, that is what makes the world so great; we can disagree ![]() Cheers, Keith. |
|
|
|
|
|
#14 | |
|
SitePoint Member
Join Date: Jul 2003
Location: CN
Posts: 8
|
Quote:
<?php echo ... ;?>' is safer and more reliable than'<?=...?>'(needn't ";") especially in app with xml also, depend on the php.ini config of host... ![]() KnightE |
|
|
|
|
|
|
#15 | |
|
SitePoint Zealot
![]() ![]() Join Date: Jun 2002
Location: Brazil
Posts: 146
|
Quote:
![]() |
|
|
|
|
|
|
#16 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Oct 2001
Posts: 666
|
Separation of logic from presentation is not the same as separating PHP code from HTML, we've discussed that I-dunno-how-many times on these forums..
|
|
|
|
|
|
#17 |
|
SitePoint Zealot
![]() ![]() Join Date: Jul 2003
Location: Mainz, Germany
Posts: 122
|
I think the distinction someone made somewhere else is very useful here: We don't want to separate logic from presentation (impossible IMHO), but rather separate business logic from presentation logic.
|
|
|
|
|
|
#18 | ||
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
Quote:
Quote:
The point is, whether your presentation logic looks like; PHP Code:
PHP Code:
PHP Code:
PHP Code:
|
||
|
|
|
|
|
#19 | |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Aug 2002
Location: Ottawa, Ontario, Canada
Posts: 215
|
Quote:
![]() I have the luxury of coding PHP in a corporate environment, and since I am the senior php programmer, I get have quite a bit of input into how things are set up (for PHP at least) ![]() Cheers, Keith. |
|
|
|
|
|
|
#20 |
|
SitePoint Zealot
![]() ![]() Join Date: Jan 2003
Location: Belgium
Posts: 133
|
I'd be interested in some opinions on my latest templating approach, inspired by http://simon.incutio.com/archive/200...pAndColdFusion , which in turn is inspired by the way ColdFusion CFML works.
It's based on pure XML templates (which is good, since it will force me to code XHTML compliant pages) and SAX parsing. I particularly focused on PEAR:DB_DataObject integration, as it offers a lot of interesting options to abstract the template tags. Next up is PEAR:DB raw SQL queries... Also, apart from what is shown here, I won't mess with other logic constructs like if()/else() etc. If anyone is interesting in developing this concept further don't hesitate to contact me... - prefab HTML Code:
<template id="test"> <doctype type="xhtml1strict" /> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>#title#</title> </head> <body> <h1>#title#</h1> include:<br/> <include src="include.xml" parse="false" type="txt"/> <hr/> include(parsed):<br/> <include src="include.xml" parse="true" type="txt" somevar="#title#" othervar="this is another variable"/> <hr/> ignore:<br/> <ignore>ignore this text</ignore> <hr/> loop:<br/> <loop sid="one" var="set"/> <ul> <output sid="one"><li>#_key_# :: #_val_#</li></output> <else><li>no looping!</li></else> </ul> <hr/> loop: (nested resultset)<br/> <!-- #%# is a row counter, note #alternate# --> <loop sid="one" var="nested"/> <ul> <output sid="one" alternate="red|black"> <li style="color: #alternate#">#%# :: #_key_# :: #name# :: #country#</li> </output> <else><li>no looping!</li></else> </ul> <hr/> isset:<br/> <isset var="title">this is the title : <i>#title#</i></isset> <else><b>title not set</b></else> <hr/> PEAR:DB_DataObject:<br/> <dataobject sid="obj" type="Rooms"/> <output sid="obj">#nr# :: #description# :: #price#<br/></output> <else>no room found..<br/></else> <hr/> PEAR:DB_DataObject:<br/> <dataobject sid="obj" type="Rooms" price="60"/> <output sid="obj">#nr# :: #pers# :: #description# :: #price#<br/></output> <else>no room found..<br/></else> <hr/> PEAR:DB_DataObject:<br/> <dataobject sid="obj" type="Rooms" where="price <= 40" order="price ASC"/> <output sid="obj">#nr# :: #pers# :: #description# :: #price#<br/></output> <else>no room found..<br/></else> <hr/> PEAR:DB_DataObject:<br/> <!-- note how the ID is assigned dynamicly from variable --> <output sid="obj">#nr# :: #pers# :: #description# :: #price#<br/></output> <else>no room found..<br/></else> <hr/> PEAR:DB_DataObject:<br/> <!-- uses $_GET to supply filtering parameters (see PEAR:DB_DataObject:setFrom()) ex: script.php?price=40&pers=3 --> <dataobject sid="obj" type="Rooms" scope="get"/> <output sid="obj"> #nr# :: #pers# :: #description# :: #price#<br/> </output> <else>no room found..<br/></else> <hr/> PEAR:DB_DataObject:<br/> <dataobject sid="obj" type="Rooms" scope="get"/> <output sid="obj" alternate="red|black|white"> <span style="color: #alternate#">#nr# :: #pers# :: #description# :: #price#</span><br/> </output> <else>no room found..<br/></else> <hr/> </body> </html> </template> Last edited by prefab; Aug 1, 2003 at 01:32.. |
|
|
|
|
|
#21 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Mar 2002
Location: Osnabrück
Posts: 1,003
|
Harry, I preferred your approach to parse .Net files using phpHtmlLib.
I think, the discussion of wether to use PHP in the templates depends on the target audience of the script. Some People using a guestbook on their page are not so comfortable with php syntax and would prefer to use something like <%entry%> or some extended html syntax as prefab shows in his example. (Arguably, someone using the class should be able to handle this, but I would say that is not for the "masses".) PHP Code:
@prefab: how do you parse your template file? |
|
|
|
|
|
#22 | |
|
SitePoint Zealot
![]() ![]() Join Date: Jan 2003
Location: Belgium
Posts: 133
|
Quote:
This is really a work in progress, as I just implemented alternating, and added the option to use $_GET or $_POST scope, while setting default parameters as atributes. - prefab like: HTML Code:
<dataobject sid="obj" type="Rooms" where="price > #min# AND price <= #max#" order="#sorder# #sdir#"
scope="get" min="0" max="100" sorder="price" sdir="asc"/>
PHP Code:
|
|
|
|
|
|
|
#23 | |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Aug 2002
Location: Ottawa, Ontario, Canada
Posts: 215
|
Quote:
![]() Not running down what you are doing. If you like it, then good on ya, but it certainly isn't for me ![]() Cheers, Keith. |
|
|
|
|
|
|
#24 | |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Aug 2002
Location: Ottawa, Ontario, Canada
Posts: 215
|
Quote:
C'est la vie, the great template debate continues.... Stay tuned for another episode of, "How the Template Parses!" Cheers, Keith. |
|
|
|
|
|
|
#25 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Posts: 846
|
|
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 09:23.






Andrea






Linear Mode
