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:
  #31  
Old January 31st, 2003, 11:09 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
.
  • Also... here is the example that you gave above using the abstraction method. Note how your HTML could grow and grow without any effect on the core code..
PHP Code:
<?php

    $results 
$db->get_results("SELECT * FROM news ORDER BY date DESC LIMIT 0,20");
    
    
$title "Latest News";
    
    echo 
use_template('header');
    
    foreach (
$results as $newsItems)
    {
    
        
$newsItems->date date('Y m d'$newsItems->date);
        
        echo 
use_template('newstemplate');
    
    }
    
    echo 
use_template('news_footer');

?>


// Header template
<html>
<body>
<h1>$title</h1>
<ul>


// Body template
<li><b>$newsItems->title</b><br>
Posted at: $newsItems->date<br>
$newsItems->shortDescription
</li>


// Footer template
</ul>
</body>
</html>

Reply With Quote
  #32  
Old January 31st, 2003, 01:37 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
This is hard for me to explain (English is not my native language), even harder to try to convince, but I will try.

First I'd like to point out that your interpretation of my way is a bit different. You have the, what one might call, 'business logic' (the SELECT * FROM users etc) in the same file as the HTML template. This is of course not separation of presentation and logic at all, but it's not how I explained it. It is more or less the way most beginners combine their HTML and PHP code. It has the major disadvantage that you cannot edit the 'business logic' code without editing the template and vice versa. Designers can indeed cause the PHP code to stop working this way.

The big difference is that I have separated these two types of code into two files. One file contains the 'business logic' and the other one only contains HTML with simple PHP for $variables and foreach() loops. There is no code for the designer to mess up. The worst thing a designer can do is accidentally move around a foreach loop. However, the risk of this happening is the same as when a designer moves a Smarty {loop from=whatever} tag or even a <!-- begin block whatever -->.

What you do in your example is not separation of presentation and other logic at all. You have the code that accesses the database and code that defines the layout of the HTML in the same file! Using my appraoch, these two concepts are in two completely separate files and a programmer can work on his file while a designer can work on his template.

You might ask "But doesn't this mean a designer has to learn PHP?" A designer only needs to know about the code to display variables (<?=$variableName?>) and that for looping (<?php foreach($set as $item) { ?>). I know now that you are not advocating templating engines, but still I'd like to write down this argument: when you use a templating engine, a designer also needs to learn about code to display {$variables} and that for looping {loop from=$set as=item}. The only difference is the syntax, so it does not matter if a designer learns one or the other.

Then one might ask "Isn't this not separation of PHP code from the HTML?" The importance of templating is not about separating all PHP code from HTML, it is about separating PHP code that has nothing to do with presentation from the HTML. Nobody has ever said you cannot use simple PHP for displaying $variables. This, I think, is a crucial misunderstanding amongst many people.

Also with your approach, a designer does not have full control over the way a page looks. He is limited to a header, a footer and a template for news items. You, as programmer, in fact determine the layout: the placement of the different subtemplates. This means that the presentation is not indepent from the other PHP code at all! When a designer wants to make a fundamental change to the layout of a page, he still has to contact you as programmer to change the code that outputs the header, newstemplate and footer templates.

I hope you understand my point.

Reply With Quote
  #33  
Old January 31st, 2003, 05:50 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
Ok. Lets put this to the test. Please show with your method the HTML template page for the following page....

http://www.devarticles.com

Reply With Quote
  #34  
Old February 1st, 2003, 06:31 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:
Also with your approach, a designer does not have full control over the way a page looks. He is limited to a header, a footer and a template for news items. You, as programmer, in fact determine the layout: the placement of the different subtemplates.
This is sort of true in the example that I gave. But that does not mean it is inherant in the method. I just gave a bad example. What I should have done is something like this.

PHP Code:
// main template
<html>
etc.
$output->news_list
etc
.
</
html>

// main list
<table>
    <
tr>
        <
td>
            
$output->title
        
</td>
    </
tr>
        
$output->list
</table>

// one row
<tr>
    <
td>
        
$output->story
    
</td>
</
tr



Now the designer can do anythin he/she likes within the context of the news page.

You may say that the programmer has to get involved if other dynamic content needs to be used. But that would be true of both types of abstraction.

Here is a more complex example that will hopefully show how very useful it is to work in this way. I have blocked out a dummy version of the devarticles.com home page using the get_template method. I would like you to note how the designer would have full controll of the main page layout. The designer also has full controll of any sub components by accessing the sub template. The designer also has the power to make a very substantial changes since evrything is written in blocks.

I should also point out that, as in your example, this main template would be the very last thing to be included. All the other blocks would have been pre-populated by this point. Which is the reason why the designer has full controll of where the blocks go... not the programmer.

I would be curious to see your template for the same page...

PHP Code:
 Dev Articles
<html>
    <
head>
        <
title>
            
$output->main_title
        
</title>
    </
head>
    <
body>
        <
table>
            <
tr>
                <
td colspan=3>
                    
$output->header
                
</td>
            </
tr>
            <
tr>
                <
td colspan=3>
                    
$output->main_links
                
</td>
            </
tr>
            <
tr>
                <
td>
                    <
table>
                        
$output->devforum
                        $output
->development
                        $output
->design
                        $output
->marketing
                        $output
->scripts
                        $output
->tools
                        $output
->advert
                    
</table>
                </
td>
                <
td width=5>
                </
td>
                <
td>
                    
$output->main_body
                
</td>
            </
tr>
            <
tr>
                <
td colspan=3>
                    
$output->footer
                
</td>
            </
tr>
        </
table>
    </
body>
</
html


Note: The $output->body template would consist of a number of templates similar to this one. Each one of those templates would represent a main page of the website such as home, about, scripts, authors (as are listed on the main link bar of devarticles).

Like the above template the varius body templates would consist of blocks. The designer would easily be able to switch and change the way that any page is displayed.

I agree with you that the last thing to be done is to output the visual content. That goes without saying. What I am proposing is a further layer of abstraction Ďon topí of that. The system that you propose does not allow for the same level of component abstraction.

So. Even though this system (get_template()) has a learning curve it is most excellent when you have a site that is built by a lot of different people because it is such a substantial abstracted architectural methodology.

Each time that you make a split (i.e. sub template or sub function), although it makes the overall project slightly more complex, it makes the individual component less complex.

Furthermore, it is much better to have 10 people working on 10 simple components than have 10 people working on one complex component. This is the principle (oop) that has been used to develop commercial software for many years why should PHP be any different?

I hope you see my point.

Justin.

Reply With Quote
  #35  
Old February 1st, 2003, 11:53 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
Therefore we agree.

I think the two of you are in complete agreement.

Reply With Quote
  #36  
Old February 1st, 2003, 12:07 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
Ok, your new example seems better indeed. I guess we agree for the most part now.

There are a few differences though. One of those difference is that in your approach, you still need to have code that is responsible for looping outside of your template. My approach simply assigns the array that needs to be looped through to a variable and puts the looping code inside the template. This template can then be a 'complete' HTML file that is editable in a WYSIWYG template. Your approach has several small HTML files, each of which can be edited by hand, but it is not possible for a designer to see the 'whole thing' in a WYSIWYG editor.

In other words, your approach forces a designer to use different files for sub-templates, while my approach allows a designer to use just one file.

Reply With Quote
  #37  
Old February 1st, 2003, 12:18 PM
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
Arrgh!

How was copper wire invented?
A Chinese and a Dutchman fighting over who owned a copper penny.

Reply With Quote
  #38  
Old February 1st, 2003, 12:19 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
hehe, it so happens to be that I'm a Dutchman, so is jv Chinese?

Reply With Quote
  #39  
Old February 1st, 2003, 08:25 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
All I can say is.....

Justin

Reply With Quote
  #40  
Old February 1st, 2003, 09:29 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
Quote:
In other words, your approach forces a designer to use different files for sub-templates, while my approach allows a designer to use just one file.
Ok. Last thing. All good designers use programs like dream weaver etc to design and mock up web site pages. They then hand this over to the programmer to split into smaller templates (as part of the team production process). From that point forward they do re-designs in the original dreamweaver doc. (i.e. one big wisywig page). When they are happy with the overall changes they fold it into the split templates. Unless the changes are smalla then they just tweak the templates.

Hope this makes sence.


Reply With Quote
  #41  
Old February 2nd, 2003, 04:30 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:
They then hand this over to the programmer to split into smaller templates (as part of the team production process).
Which is not necessary with my approach

Reply With Quote
  #42  
Old February 2nd, 2003, 07:49 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:
Which is not necessary with my approach
...which is an aproach that means 10 people work on one complex component in a linear fashion..... rather than ten people working on 10 simple components at the same time

Reply With Quote
  #43  
Old February 2nd, 2003, 07:51 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
Not at all. If a designer or a group of designer want(s) to, he can split up that main template into as many sub templates as he wants, using <?php include('subtemplate.php'); ?>. Your approach forces this, my approach leaves it up to the designer to make a template as fine-grained as he or she wants

Reply With Quote
  #44  
Old February 2nd, 2003, 09:35 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 <?php include('subtemplate.php'); ?>.
Not as safe as get_template tho... because they can use any PHP they want and also they have to get the include syntax (or any php syntax) right... both of which mean there is more chance of screwing things up!!!

Ok. Now PLEASE STOP!!!!!!

Reply With Quote
  #45  
Old February 2nd, 2003, 11:53 AM
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:
Originally posted by jv22
Not as safe as get_template tho... because they can use any PHP they want and also they have to get the include syntax (or any php syntax) right... both of which mean there is more chance of screwing things up!!!


Reply With Quote
  #46  
Old February 11th, 2003, 02:11 PM
revium revium is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 2 revium User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
use_template function

I have a question about the use_template function. Because the include statement for the the template file is called within a function, the template file does not have access to the previously set variables in the out function. Within the use_template function the variable scope is local unless declared global (which it is not), so the include file will not have access to the variables and will be unable to print out their values.

Based on this I do not see the advantage or use of the use_template function. Am I deciphering this correctly?

Reply With Quote
  #47  
Old February 11th, 2003, 04:08 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
If you read past that point you will see a function called get_template. This addresses the global variable issue.

Cheers,
Justin.

Reply With Quote
  #48  
Old February 11th, 2003, 04:17 PM
revium revium is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 2 revium User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Justin,

I realized that after the fact, so I apologize for the post. The first two examples would not work though, based on the original function. Thanks for the article.

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