Programming Tools
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingProgramming Tools

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 29th, 2003, 05:11 AM
Vantera Vantera is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Location: South Coast of NSW, Australia
Posts: 108 Vantera User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Send a message via ICQ to Vantera
Article Discussion: JVís Power Tips for PHP (Volume 1)

If you have any questions or comments about this article please post them here.

This forum post relates to this article
__________________
Kind Regards,
John Rebbeck
john@interspire.com
ICQ# 74637937

Reply With Quote
  #2  
Old January 29th, 2003, 10:16 AM
Captain Proton Captain Proton is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 18 Captain Proton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I find it odd.. You say that people should not break out of php tags for printing HTML. You give an argument why people do this, speed, but you do not give an argument anywhere on why you shouldn't break out of PHP tags.

It makes no sense to echo HTML using echo() or print() when you can break out of PHP tags. I won't mention the speed issue, but this is one reason. The other reason is that PHP was designedi] like this! PHP is after all a HTML-embedded scripting language. It means you can mix HTML code with <?php ?> tags, but what do you do? you stuff all your HTML inside echo ""; statements.

Quote:
There are plenty of pre-built template systems out there that can save you hours of coding (e.g. Smarty).

In fact, Smarty causes you hours of extra coding! You have to write code to control Smarty templates, when you can be off much easier by using PHP templates with simple <?=$variable?> tags in them. This is if you use PHP templates the right way, i.e. not mixing in all kinds of database access code and other code that has no business being in template files.

I don't want to have to explain this again, but if you want to argue on this, I suggest you read this thread over at the SitePoint forums.

Quote:
Power tips indeed.
...

Reply With Quote
  #3  
Old January 29th, 2003, 10:44 AM
picker999 picker999 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 19 picker999 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to picker999 Send a message via AIM to picker999
Well I see it as a fabulous way to maintain the "separation of church and state". I guess if you are small time you could fail to see the importance of coding like Justin's example. You should try to embrace the concept though if your budget demands. You can hire all fresh college grads (that think they know everything) to code your basic hack work html stuff at minimum wage. You don't confuse them with other stuff. As well security may require these minimum wage people don't get to see the keys to the candy store.

Last edited by picker999 : January 29th, 2003 at 10:47 AM.

Reply With Quote
  #4  
Old January 29th, 2003, 11:16 AM
jasonlotito jasonlotito is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Montreal, CA
Posts: 10 jasonlotito User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to jasonlotito
While the article was a bit to apologetic (the author kept saying "But this is just my opinion, you don't have to agree..."), and contradicts itself several times, it does try to present some good points.

Also, for those against Smarty for various reasons, read this: http://smarty.php.net/whyuse.php

Reply With Quote
  #5  
Old January 29th, 2003, 11:23 AM
jasonlotito jasonlotito is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Montreal, CA
Posts: 10 jasonlotito User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to jasonlotito
Oh, and as far as the rant on SitePoint is concerned, the one written by user voostind, while he makes a few points, most of his arguments fall flat in the face of elitism and his opinions. Frankly, I picked enough wholes in his argument that it just sank.

Reply With Quote
  #6  
Old January 29th, 2003, 11:49 AM
Captain Proton Captain Proton is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 18 Captain Proton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
His posts may be based on elitism, his points are valid. When you think logically, which I hope is something we are all still able to do in this age of computers doing so much, they make sense.

If you can really find so many wholes in his arguments as you say, I'm sure you would not mind posting them here so all of us can read them

Reply With Quote
  #7  
Old January 29th, 2003, 02:14 PM
jasonlotito jasonlotito is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Montreal, CA
Posts: 10 jasonlotito User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to jasonlotito
Okay, here are the holes that I have found.

First

Quote:
Because PHP already is a template engine. Examine all template engines you can think of, and compare their features with those of PHP. PHP has them all. And more.


He is partly right. PHP's initial design was essentially a templating engine. However, this isn't true anymore.

It's like saying JavaScript is just for the web, or Java is just for the embedded market. Just because a language was initially designed for something doesn't mean that's all it will ever be. In the end however, his argument here is trivial. Call it what you will, PHP is PHP.

Quote:
So why do people think we need a template engine? The answer to that question is very simple: PHP has become too complex.


That's completely untrue. There are still many places where PHP simply CAN'T be used as a template engine.

Let's say I want to create a service on my site, where other sites can use my service. These other sites want this service, when they use it, to use their site's style. So either I can either let them just use plain HTML with a small template system, or HTMl with PHP (because afterall, the argument is PHP is a template engine in itself). Now, anyone could see the potential for security risks for allowing them to use PHP, but we still need to offer the flexibility.

People want a template engine because they want something they want somethign that's sole purpose is to allow for the seperation of business logic and display.

Their is also great potential for failure for allowing design/html people access to PHP as their templating language. Seriously, a person can crash a site if they don't know what they are doing. Infinite loops are dangerous, especially when the person doesn't know what they are doing, and something a templating engine will prevent.

Quote:
Here, in these forums, we have learned people to not use those mysql_-functions directly, but use a database abstraction layer instead. This makes coding simpler (no need to know all those functions for the various DBMS's) and when they decide to use another DBMS instead of MySQL (and they undoubtedly will at some point), the conversion will be painless.


Obviously, I have to question whether he has every had to use an abstraction layer and had to switch from, let's say, MS SQL to Oracle, or MySQL to PostgreSQL, or even MySQL to Oracle. If it was that easy, we would all be using abstractions. However, anyone who knows anything about databases knows that SQL isn't all the standard. Sure, you can abstract everything, at the major cost of performance (and in this case, it's not a small performance loss). Really, if you switch from MySQL to Oracle, you should be changing a lot of SQL, as the design will possibly change to reflect the additional features Oracle has. Otherwise, it makes no sense to change.

His next argument about the CSS is an argument I agree with:

Quote:
I can change 99% of the layout of any of my sites by just changing the style sheet; there is no need to dive into the HTML. Only if the site needs a major overhaul the HTML must be changed. If your web designer tells you this isn't true, fire him.


This is very much true. In fact, Wired.com has almost proven CSS is now here. Now, truth be told, I still believe we have about 2 more years because Wired.com type sites become more and more abundant, but it's a good point.

However, his argument about the web designer is a bit far fetched. 90% of the web designers I know (and I know quite a few), still use the table design. Why? Because it works! It works in Dreamweaver, and makes working from Photoshop easier.

A business can't sit back and fire everyone because they don't create CSS perfect XHTML compliant websites (XHTML is another story) every time, or else they would never find web designers (and knowing how to use CSS/XHTML doesn't make a person a web designer).

What I hope for is in the long run, designers work less with the code, and programmers, or coders, start working with the CSS/HTML more. Designers will continue designing in Photoshop and DW, and coders will take those designs, and turn them into CSS sites.

Back on topic.

He then goes on to talk about the PHP programming community in general, and in some aspects he is right. However, he makes a blanket statement that the PHP community knows nothing about software architecture.

But who is the judge here? How do you define good software architecture? Software that looks nice on the inside with ti's design?

In the real world, where many of us work, good software isn't decided by it's internal design, but by the finished product.

It's like the argument over monolithic kernels and microkernels. (On a site note, this is where I am not a professional, just an observer, so please feel free to correct me here). Monolithic kernels are said to be old and out dated. I am sure many of us have heard of the Linux v.s. Minix arguments earlier in the days. The issue isn't which is better.

The issue is that right now, even though all the elite minds of the software community said microkernels were the way to go design wise, monolithic kernels are still dominant.

Many programmers get so caught up in the development process that they forget the main reason for programming: to solve problems. If you solve the problem, the program works. Nuff said.

(Note: Despite having said all of that, I still take pride in writing good software. I do know who Knuth is - as soon as I get my new job, I am picking up his books at the local book store - and I do know my sorting algo's, etc.)

He then goes on to point out 4 questions and his answers to them. Here are my replies:

Question 1: Not really important. It's not anything about template engines, and more about software design in general.

Question 2: Actually, no, the lines of code are the same. Again, this is a moot point on both sides.

Question 3: Ugly syntax is opinion based. I know designers (not necessarily web based) who are absolutely afraid of HTML. I can only imagine what PHP will do. Also, by allowing the designers to use PHP, you give in to greater security risks.

He also argues that the designers can start experimenting with PHP much faster. Not only is this dangerous (again, imagine infinite loops on the live server), but it's completely contradicts his previous arguments with this being what's wrong with the PHP community. He want's designers to use PHP, however, that's what is wrong with the community? Maybe I am just reading too much into it.

In the end, the security risk is too great.

Question 4: He is partly right here. However, if you design your own PHP based template system as he suggests, you still have a template engine you need to maintain at some level. I doubt he has any personal experience with managine a template engine. We used a rather simple one at work (just replaced vars, etc), and didnt' have to maintain a thing. We couldn't use PHP either. Again, the security risk was there.

As you can see, most of his arguments in this rant against template engines either had holes, or weren't an argument against template engines.

Don't mistake my argument as saying that template engines are always important. It depends on what you need. Frankly, I don't use a template system on PHPComplete. I just use CSS to handle the display. But at the same time, there are many places I do use some form of a template engine.

The biggest error any person makes when arguing against template engines is to say they are not good because of these reasons, and all those reasons are based on HTML/Website only reasons.

I use a template engine for email. This allows people to use placeholders in emails, something like:

Dear {username},

Using PHP here would create a security risk, and also un-needed cruft.

voostind doesn't sound like a stupid person. In fact, him and I would probably agree on many things. This just isn't one of them.

template engines aren't bad, they just aren't always needed, they aren't the holy grail some people preach them to be.

Reply With Quote
  #8  
Old January 29th, 2003, 02:19 PM
jasonlotito jasonlotito is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Montreal, CA
Posts: 10 jasonlotito User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to jasonlotito
Also, one more note.

Template engines are used everywhere in real world products, like word processors (MS Word, Open Office, KWord, Abiword), web design software (Macromedia Dreamweaver), presentation software (MS PowerPoint, etc). They all have a template engine. Sure, a person could hand code the template themselves, might even make for more efficient templates. Doesn't mean they have to.

Reply With Quote
  #9  
Old January 29th, 2003, 03:22 PM
jv22 jv22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 17 jv22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Well said!

jasonlotito,

You deserve a medal Well said!

Quote:
In fact, Smarty causes you hours of extra coding! You have to write code to control Smarty templates, when you can be off much easier by using PHP templates with simple <?=$variable?> tags in them.


As with many time saving implimentations, in the real world it takes effort to set up, but once it is set up it makes it easyer and faster to change your site when your site is a very big site with more than one person working on it.

PHP is no longer an HTML embedded scripting language as can be proved by the many people who are using it as a shell script tool and programming tools for writing deamons etc.

PHP has become a very serious programming language with some fantastic features and as such needs to be viewed that way.

There are many ways to skin an egg but it is my experience and opinion that sticking within php tags is best as a general rule.

This is the type of issue that only really makes sense after years of programming experience - especially when you get asked to re-work a site that you made 5 years ago - and you realise how much easier it would be if you had used the abstracted arcitecture you were using today!

The fact is the more you break stuff down the easier it is to understand and the more re-usable it is.

ie. Use small functions
Abstract static html from code

Cheers,
Justin.

Reply With Quote
  #10  
Old January 30th, 2003, 06:42 AM
Captain Proton Captain Proton is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 18 Captain Proton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
jv22, you are completely missing the point:

Quote:
He is partly right. PHP's initial design was essentially a templating engine. However, this isn't true anymore.

This does not mean that PHP's use is limited to templating. I never said that and I never will. The point is that php can be used for templating. You don't need a template engine like Smarty for that. So, argument debunked.

Quote:
Let's say I want to create a service on my site, where other sites can use my service. These other sites want this service, when they use it, to use their site's style. So either I can either let them just use plain HTML with a small template system, or HTMl with PHP (because afterall, the argument is PHP is a template engine in itself). Now, anyone could see the potential for security risks for allowing them to use PHP, but we still need to offer the flexibility.

I completely agree with you. This is a valid reason for using a template engine on top of PHP, for added security. It is in fact the reason I use one myself. But...:

Quote:
People want a template engine because they want something they want somethign that's sole purpose is to allow for the seperation of business logic and display.

Their is also great potential for failure for allowing design/html people access to PHP as their templating language. Seriously, a person can crash a site if they don't know what they are doing. Infinite loops are dangerous, especially when the person doesn't know what they are doing, and something a templating engine will prevent.

Firstly, separating "business logic from presentation" is mis used here, but I understand what you are saying. It means that you should not have code that has nothing to do with presentation in your templates. As long as you keep the 'business logic', which in your definition is probably code to access databases, in a separate file from your HTML template, you can achive this separation just as well as by using Smarty. In the file with the code to access the database, simply assign variables you load from the database to something like $newsItems. In the template file, simply use a <?php foreach .. ?> loop to loop through those news items. There is no difference in using <?php foreach .. ?> or {loop from=".."}, except for the syntax.

What I think is causing a lot of heated discussions, is that some people think that people who debunk template engines are in favour of 'spaghetti PHP code', all kinds of non-presentational PHP code mixed with HTML. This is how we all started, it became a mess to maintain, so we resorted to template engines because we thought that was the right thing (tm). At this stage, there is no advantage in using a template engine over putting your HTML in a separate file with simple, and I'd like to stress the simple, PHP to display <?=$variables?>.

All that you say about CSS is in my opinion not relevant to the discussion of using PHP as templating engine instead of a third party one such as Smarty. So let's please stick to the topic.

Reply With Quote
  #11  
Old January 30th, 2003, 07:04 AM
jv22 jv22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 17 jv22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
At this stage, there is no advantage in using a template engine over putting your HTML in a separate file with simple PHP to display <?=$variables?>.
If you read through my article you will see that that is EXACTLY what the article is saying.

So, even though you were the main protagonist and first poster to debunk the article... it seems that you have no argument with the article at all.

In fact you agree with it. Furthermore you are using the same concept that the article explains as your main argument - to argue against the article itself!

Justin.

Reply With Quote
  #12  
Old January 30th, 2003, 07:08 AM
Captain Proton Captain Proton is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 18 Captain Proton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
What article are you referring to? your post above here? If I read that correctly, you are on the 'use a 3rd party template engine' side, but maybe I misinterpreted that.

Quote:
At this stage, there is no advantage in using a template engine over putting your HTML in a separate file with simple PHP to display <?=$variables?>.

Maybe I wrote it down a bit awkwardly, so let me rephrase it: Using a 3rd party template engine has no advantages over using PHP as your template engine when it comes to separating presentation from other logic.

Reply With Quote
  #13  
Old January 30th, 2003, 07:23 AM
jv22 jv22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 17 jv22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Using a 3rd party template engine has no advantages over using PHP as your template engine
This is really what the guts of my article entitled 'JV's Power Tips For Php (V1)' (JVPTFP) is about. If you read through the article you can see that I am not advocating 3rd party template systems.

I am suggesting that you write a function that parses an HTML file and injects php variables into it.

If you look at the method I use it is the eval statment to 'evaluate the html' file using 'PHP' and swap $vars for the dynamic value stored in the main PHP script.

Therefore you agree with the main article JVPTFP .

Yet you were the first person to make a post that debunked JVPTFP...

and now you are using arguments that JVPTFP makes...

to argue against others on this thread...

that you started when you originally debunked JVPTFP....

I was just pointing out that I found a certain irony in that!

Justin.

Reply With Quote
  #14  
Old January 30th, 2003, 07:28 AM
Captain Proton Captain Proton is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 18 Captain Proton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I'm sorry, I didn't realize you were the author of the article you were referring to.

Quote:
I am suggesting that you write a function that parses an HTML file and injects php variables into it.
Correct me if I am wrong, but this is what 3rd party template engines do!

The whole point of that discussion over at the sitepoint forums was to let PHP do the job of parsing a HTML file and injecting php variables into it. If you use a function to do this, even if all it does is simple str_replace()'s, you have in fact a template engine. At first simple str_replace()'s may be enough, but sooner or later you will need functionality to do blocks / loops. Eventually, this will turn out to be just another Smarty.

I read your article thoroughly, and at first I was relieved to read that you weren't supporting 3rd party template engines, but then I saw your own solution was not much different.

Reply With Quote
  #15  
Old January 30th, 2003, 07:40 AM
jv22 jv22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 17 jv22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
1) It's not third party if you write it!
2) It's 'one part' of your overall solution.

For example if you look at the function use_tempate()in my article

You can write a the template like this..


// user_row.htm
<tr>
<td>
$user->name
</td>
<td>
$user->email
</td>
</tr>


Then you can do some php script like this..

$users = $db->get_results("select name, email from users");

foreach ( $users as $user)
{
echo get_template('user_row');
}

Not exactly like smarty huh? Still all done in PHP too.

Yet it makes it very easy to change the
formatting for the row output. And your
HTML is not confused with your PHP.

Because all your 'template' files are in one dir and
seperated from the code.

So you can think of it as a template system
that your php templates use.

Hope this makes sence.

Justin.

Reply With Quote
  #16  
Old January 30th, 2003, 07:43 AM
Captain Proton Captain Proton is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 18 Captain Proton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
1) It's not third party if you write it!
Third party, written by yourself, it doesn't matter. The point is that you use a template engine on top of PHP itself.

Quote:
Yet it makes it very easy to change the formatting for the row output. And your HTML is not confused with your PHP.

This does not separate your HTML from your PHP code at all. See how there is PHP code to 'drive' the HTML output? You still have to control your template, and the way you control it is coupled (which means it is not separated) to the structure of the template. With the approach I described above, you simply assign variables with content and you leave the template itself to handle placement of those variables.

Last edited by Captain Proton : January 30th, 2003 at 07:46 AM.

Reply With Quote
  #17  
Old January 30th, 2003, 07:54 AM
jv22 jv22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 17 jv22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
This does not separate your HTML from your PHP code at all.
I give up!

Reply With Quote
  #18  
Old January 30th, 2003, 10:20 AM
picker999 picker999 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 19 picker999 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to picker999 Send a message via AIM to picker999
Whew!

Justin I don't blame you. LOL !! I hope this hasn't exhauseted you for the next in the series. I am looking forwared to it.

Reply With Quote
  #19  
Old January 30th, 2003, 12:48 PM
Sork Sork is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 8 Sork User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Captain Proton, I think you miss understood JV I don't get it

In his article is doing the templating the way it should be done (my opinion and yours CProton, i guess ).

Theres is too ways, with include() and with eval().

I don't understand how this discussion started

Peace [] Sork

Reply With Quote
  #20  
Old January 30th, 2003, 02:45 PM
Captain Proton Captain Proton is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 18 Captain Proton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Well, this discussion started about whether or not you should break out of PHP tags to echo HTML code. That's the main thing we disagreed on.

But, allow me to illustrate with a complete example how I think PHP templating should be done, and how you achieve a better separation of templates and other sorts of code:

PHP Code:
// This is the file that is requested in the webbrowser: news.php
$query "SELECT * FROM news
ORDER BY date DESC
LIMIT 0,20"
;
$result mysql_query($query);

/* Prepare an array of news items for the template */
$newsItems = array();
while (
$row mysql_fetch_array())
{
  
$newsItems[] =& $row;
}

/* Now include the template, that's all */
include('newstemplate.php'); 


PHP Code:
// File newstemplate.php
<html>
<
body>
<
h1>Latest News</h1>
<
ul>
<?
php foreach($newsItems as $news) { ?>
 <li><b><?=$news['title']?></b><br>
 Posted at: <?=date('Y m d'$news['date']);?><br>
 <?=$news['shortDescription']?>
  </li>
<?php ?>
</body>
</html> 


See how there is no code whatsoever like $row = get_template('newsrow')? In my opinion this makes the template more loosely coupled to the other code.

Reply With Quote
  #21  
Old January 30th, 2003, 03:10 PM
JimT JimT is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: St. Louis, Missouri, USA
Posts: 2 JimT User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
As one who is now learning PHP, I found Justin's article quite insightful and helpful. I am beginning a PHP project shortly and plan to utilize the methods he describes to see how they work in my project. I look forward to his future articles.

An article like this brings ideas to those of us who are less skilled in PHP (for now!!). I have programmed (in other languages) for over 20 years and I know there are many different programming styles to perform the same task.

If someone proposes an alternate method or style, WRITE AN ARTICLE about it and share it with the rest of us. Show us why and how your theories are better, work better in the real world, etc. The reader can try out differing theories to see what works best for him. It is just part of each coder's learning curve....

It sounds like Justin and the Captain share the same general ideas and concepts, its just the minor details that are different.
Captain, SHOW US your methods in a well-written tutorial style article (if you already have such a piece, point it out to me, please), instead of ranting and raving about every difference of opinion, no matter how minor......

(This post mostly written before your immediately prior post....Thanks for your above example....)

Oh well, such is life in cyberspace.....

Last edited by JimT : January 30th, 2003 at 03:13 PM.

Reply With Quote
  #22  
Old January 30th, 2003, 03:10 PM
Sork Sork is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 8 Sork User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
But, allow me to illustrate with a complete example how I think PHP templating should be done, and how you achieve a better separation of templates and other sorts of code:
Yes that's it!
But i must disagree with you in how we manage results sets.
Because you are loading a complete result set into memory see
my aproachhere
I think is better, what you think?

Or am i wrong? I'm saying this because of that =&(reference)

Reply With Quote
  #23  
Old January 30th, 2003, 03:14 PM
Captain Proton Captain Proton is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 18 Captain Proton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
You're absolutely right, it is inefficient. But I did this just to keep the example simple, because not everyone is familiar with the concept of Iterators.

But to add to this argument, by using Smarty you have to do the same thing when you do $tpl->assign() for all database results.

I just saw that reference, I guess I've been typing too much of those in the past few days. It doesn't really matter if it's there or not.

Reply With Quote
  #24  
Old January 30th, 2003, 03:21 PM
Sork Sork is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 8 Sork User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
But to add to this argument, by using Smarty you have to do the same thing when you do $tpl->assign() for all database results.
What you mean?

...could be my english...

Reply With Quote
  #25  
Old January 30th, 2003, 03:35 PM
Captain Proton Captain Proton is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 18 Captain Proton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Sork, I mean that if you use Smarty, you also have to load a complete result set into memory when you assign template variables.

And JimT, although the article is not written by me, it does illustrate exactly what I'm trying to say: http://www.phppatterns.com/index.ph...icleview/4/1/1/ Read it and spread the word! :P

The thing that sometimes frustrate me is that so many PHP tutorial sites will teach beginners that template engines are the only way to "separate presentation from business logic" (a quote which is misinterpreted, but that's not the point). They are partially right, in that you should use templates to achieve that goal, but template engines on top of PHP have nothing to do with that. Most of these beginners then start to apply these concepts without critically thinking about the 'why' of what they are doing (I know this for a fact, because I used to be one of them and used template engines for invalid reasons).

What the article at phpPatterns also shows is that some people completely go up in what they are doing (read: the Smarty developers) and in essence invent a new programming language in their template engines. If you look at smarty.php.net, you will see that there is even a plugin to validate form submissions! Isn't separating things such as form validation and HTML output the reason the Smarty template engine was created in the first place?? I rest my case

Reply With Quote
  #26  
Old January 30th, 2003, 03:59 PM
JimT JimT is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: St. Louis, Missouri, USA
Posts: 2 JimT User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for the reference to that article....

I discovered Harry's site a few days ago in a post over on SitePoint. It is quite an interesting collection of reading material!!!

I'll certainly agree that some of these tools do seem to make things more complicated rather than less. Right now, I'm just on the search for a "straightforward" separation methodology to learn and use....

Reply With Quote
  #27  
Old January 30th, 2003, 04:03 PM
jv22 jv22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 17 jv22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Captin Proton.

About 4 months ago I embarked on a new project that I called php4gl. In the end I decided it was not worth continuing with. But there are still some docs from that idea that I would like you to read.

For example.. in this doc http://www.php4gl.net/php4GL_explained.doc it explains in great detail one way to abstract a website.

If you read it you will see it is exactly what you are talking about.

The main concept can be found here. http://www.php4gl.net/

I have given up on it because no one has shown any interest in helping with it.

The point is that my new article (i.e. get_template) was to compliment this type of arcitecture not compete with it and the whole point of get_template() is...

So that you can stay within php tags and print large chunks of html (with dynamic variables) without having to use echo!!!!!

Which was the thing you disagreed with in the first place.

But as I said in my article....

"I am going to show you the easy way and then the elegant way - (with a few steps in between)."

So as you see. I was not exactly advocating echo printing. I was advocating use a function like get_template to help you.

Perhaps I should have called the function

use_a_function_to_print_somoe_html()

Might have been less confusing....

Justin




Justin.

Last edited by jv22 : January 30th, 2003 at 04:21 PM.

Reply With Quote
  #28  
Old January 30th, 2003, 08:55 PM
r0kawa r0kawa is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 1 r0kawa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
The approach captain proton or harryF is just using PHP as the template solution. horde has done this for a long time, and i think i like their approach.

There's no different between using

{if}
do something
{/if}
{else}
do something else
{/else}
than

<?php if ?>
do something
<? } else {?>
do something else
<? } ?>

Whatever you all are doing, don't force people to do your approach. This is not a good thing (TM) . Ppl has their own opinion, that's why war happened !

cheers..

Reply With Quote
  #29  
Old January 31st, 2003, 09:43 AM
Captain Proton Captain Proton is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 18 Captain Proton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Well, then we partially agree: that you do not need template engines for templating. One thing I still don't agree with, is the following:

The way you do your templating, you still need to have some code that sort of 'drives' the template, the get_template() function. You still have to make several calls to get_template() and combine the results (what you do with 'simple_table' and 'sampe_template') into one big template. I think this sort of logic belongs in the template itself, which should simply do includes. Cause doing it this way still combines the way the template is presented with the code that accesses data.

Maybe you're right and I'm being a bit too pragmatic with this. I certainly wouldn't want to force anyone to use this approach, I just want to point out the reasons for why people should not be doing stuff that is wrong.

Still, I disagree that not breaking out of PHP tags for HTML is a good thing.

Reply With Quote
  #30  
Old January 31st, 2003, 10:41 AM
jv22 jv22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 17 jv22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Here is the difference...

Your way...
PHP Code:
<? $users $db->get_results("SELECT * FROM USERS"); ?>
<html>
<head>
    <title><?php echo $title?></title>
</head>
<table width=100% bgcolor=555555>
<?php

foreach ( $users as $user )
{

print <<<END
<tr>
    <td>
        
$user->name
    </td>
    <td>
        
$user->email
    </td>
</tr>
END;

}

?>
</table>
</body>
</html>



My way...
PHP Code:
<?php

    $users 
$db->get_results("SELECT * FROM USERS");

    
$title "User List";
    
    echo 
get_template("header");

    foreach ( 
$users as $user )
    {
        echo 
get_template("user_row");
    }

    echo 
get_template("footer");

?>


Note: The more HTML that is in the first example the more inconvenient and hard to work with it becomes.

The more HTML that is used in the second example makes no difference to the code at all. Plus the second example is much easier to read and understand for anyone.

If you can't see the benefit of this then I truely do give up.

Does any one else agree with me on this?

Cheers,
Justin.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingProgramming Tools > Article Discussion: JVís Power Tips for PHP (Volume 1)


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap