View (MVC) Function / Class

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

View (MVC) Function / Class

Post by psurrena »

This has been brought up before, especially in my previous questions, and I've finally implemented it. What is everyones opinion on a function or class dedicated to view? I use the below function within a template, instead of creating an additional template file.

Below is an example.

Code: Select all

function viewProject($cycle, $image, $projectName, $projectDescription){
    $output.="<div class=\"panel\" title=\"".$cycle."\">\n";
    $output.="<div class=\"wrapper\">\n";
    $output.="<div class=\"left\">\n";
    $output.="<div id=\"".$cycle."\" class=\"pics\">\n";
    $output.=imageArray($image,$projectName);
    $output.="</div>\n";
    $output.="</div><div class=\"right\">\n";
    $output.="<p><img src=\"images/".$cycle."-title.gif\" alt=\"".$projectName."\"/></p>\n";
    $output.="<p>".$projectDescription."</p>\n";
    $output.="</div></div></div>\n\n";
 
    return $output;
}
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: View (MVC) Function / Class

Post by allspiritseve »

psurrena wrote:This has been brought up before, especially in my previous questions, and I've finally implemented it. What is everyones opinion on a function or class dedicated to view? I use the below function within a template, instead of creating an additional template file.
In my opinion, you should keep all of your html in template files. I'd use a view class when you're applying presentation logic on data (formatting, etc) and/or grabbing info from the database, and then hand that information off to a template file that simply outputs the information in the correct spots.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: View (MVC) Function / Class

Post by Christopher »

My main question is whether it is more maintainable to do the same thing using a general Template class/function that loads a PHP template (or some other kind of template). Then you don't have to write custom functions each time.
(#10850)
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: View (MVC) Function / Class

Post by koen.h »

psurrena wrote:This has been brought up before, especially in my previous questions, and I've finally implemented it. What is everyones opinion on a function or class dedicated to view? I use the below function within a template, instead of creating an additional template file.

Below is an example.

Code: Select all

function viewProject($cycle, $image, $projectName, $projectDescription){
    $output.="<div class=\"panel\" title=\"".$cycle."\">\n";
    $output.="<div class=\"wrapper\">\n";
    $output.="<div class=\"left\">\n";
    $output.="<div id=\"".$cycle."\" class=\"pics\">\n";
    $output.=imageArray($image,$projectName);
    $output.="</div>\n";
    $output.="</div><div class=\"right\">\n";
    $output.="<p><img src=\"images/".$cycle."-title.gif\" alt=\"".$projectName."\"/></p>\n";
    $output.="<p>".$projectDescription."</p>\n";
    $output.="</div></div></div>\n\n";
 
    return $output;
}
What's with all those namespaces? Bad joke, I know ;-)

For me this is too much html to have in a function. Everything depends on circumstances of course, but if you have most of it in templates in your template directory, why not put all in it? Actually, to turn the question around: what makes you want to have this html snippet inside a function?
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: View (MVC) Function / Class

Post by psurrena »

Actually, to turn the question around: what makes you want to have this html snippet inside a function?
Honestly, I just enjoy trying out my options. For this project I wanted to see if a site with one template file and a few view functions might be more efficient than multiple template files and no view function. It might be, it might not...that's the fun of it.

I always get great feedback here...that's why I bring it up.
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: View (MVC) Function / Class

Post by psurrena »

arborint wrote:My main question is whether it is more maintainable to do the same thing using a general Template class/function that loads a PHP template (or some other kind of template). Then you don't have to write custom functions each time.
Good point, since I work on my own, I often don't consider maintainability...
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: View (MVC) Function / Class

Post by josh »

You are separating or structuring at a sub routine level, its better then spaghetti code but how are you implying this provides an advantage over MVC? You've demonstrated with psuedo code a model / view separation at the sub routine level, you're probably calling this class from a set of page controllers, right?

What happens if 2 unrelated classes need to generate similar HTML, you certainly wouldn't commit the design sin of just pushing the HTML generation higher up the class hierarchy right?
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: View (MVC) Function / Class

Post by panic! »

psurrena wrote:This has been brought up before, especially in my previous questions, and I've finally implemented it. What is everyones opinion on a function or class dedicated to view? I use the below function within a template, instead of creating an additional template file.

Below is an example.

Code: Select all

function viewProject($cycle, $image, $projectName, $projectDescription){
    $output.="<div class=\"panel\" title=\"".$cycle."\">\n";
    $output.="<div class=\"wrapper\">\n";
    $output.="<div class=\"left\">\n";
    $output.="<div id=\"".$cycle."\" class=\"pics\">\n";
    $output.=imageArray($image,$projectName);
    $output.="</div>\n";
    $output.="</div><div class=\"right\">\n";
    $output.="<p><img src=\"images/".$cycle."-title.gif\" alt=\"".$projectName."\"/></p>\n";
    $output.="<p>".$projectDescription."</p>\n";
    $output.="</div></div></div>\n\n";
 
    return $output;
}
The legacy ASP stuff where I work is done like this..It personally is a bit annoying.

Could you not do something like

Code: Select all

 
 
abstract class view
{
 
function __call($template_name,$arguments)
{
include('views/'.$template.'.php');
}
 
}
view::some_template();
 
Personally I just by default name my views after their action..I know that can't always be the case.
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: View (MVC) Function / Class

Post by psurrena »

I guess my big problem is when you have a basic view structure that could be easily templated but, as in the example below, it would be very useful to be able to control the amount of list items and div tags. The code below would be for jQuery tabs.

Code: Select all

<div id="tabs">
    <ul class="horizontal-menu">
        <li><a href="#t1">{t1_title}</a></li>
        <li><a href="#t2">{t2_title}</a></li>
    </ul>
</div>
 
<div id="t1">{t1_content}</div>
<div id="t2">{t2_content}</div>
How do you work with this without creating a bad function that is repetitive and just clunky:

Code: Select all

function createTabs($tabCount){
    $output.='<ul class="horizontal-menu">';
    for($i=1; $i<=$tabCount; $i++){
        $output.='<li><a href="#t'.$i.'">{t'.$i.'_title}</a></li>';
 
    }
    $output.='</ul>';
 
    for($i=1; $i<=$tabCount; $i++){
        $output.='<div id="t'.$i.'">{t'.$i.'_content}</div';
    }
 
    return $output;
}
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: View (MVC) Function / Class

Post by josh »

psurrena wrote: as in the example below, it would be very useful to be able to control the amount of list items and div tags.
Smarty has loops and if statements, or you can just use PHP as the templating language which I find more efficient.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: View (MVC) Function / Class

Post by allspiritseve »

psurrena wrote:How do you work with this without creating a bad function that is repetitive and just clunky:

Code: Select all

<ul class="horizontal-menu">
<? for ($i = 1; $i <= $tabcount; $i ++): ?>
<li><a href="#t<?=$i; ?>">{t<?=$i; ?>_title}</a></li>
<? endfor; ?>
</ul>
 
<? for ($i = 1; $i <= $tabcount; $i ++): ?>
<div id="t<?=$i; ?>">{t<?=$i; ?>_content}</div></li>
<? endfor; ?>
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: View (MVC) Function / Class

Post by psurrena »

jshpro2 wrote:
psurrena wrote: as in the example below, it would be very useful to be able to control the amount of list items and div tags.
Smarty has loops and if statements, or you can just use PHP as the templating language which I find more efficient.
I'll take a look at Smarty. When you say "just as the templating language" what do you use for logic?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: View (MVC) Function / Class

Post by Christopher »

psurrena wrote:How do you work with this without creating a bad function that is repetitive and just clunky:
PHP templates are probably the easiest solution for this. A simple PHP template class might look like this:

Code: Select all

class PhpTemplate {
    protected $data;
    protected $filename;
 
    public function __construct($filename, $data=array()) {
        $this->filename = $filename;
        $this->data = $data;
    }
    public function set($tag, $value) {
        $this->data[$tag] = $value;
        return $this;
    }
    public function render() {
        extract($this->data);
        ob_start();
        include($this->filename);
        return ob_get_clean();
    }
}
(#10850)
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: View (MVC) Function / Class

Post by psurrena »

Sorry, I thought it was going somehwere else. I'm currently using a template system...

I know there's a bit of view code here but... my average page looks like this:

Code: Select all

<?php
    #Currents - article
    require_once './library/class/currents.class.php';
 
    $id=$_GET['id'];
 
    $article=new Currents();
    $article->dbConnect();
    $value=$article->articleQuery($id);
 
    //Template
    $template="default.template.html";
 
    //Background / Navigation / Subtitle
    $tags['subtitle']=' - Headlines';
    $tags['background']="currents-background.jpg";  
    $tags['menu']="currents";
 
    //Content
    $tags['header']=$value['headlines_title'];
    $tags['content'].="<p>{$article->formatDate($value['headlines_date'])}</p>\n";
    $tags['content'].="<p>{$value['headlines_article']}</p>\n";
?>
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: View (MVC) Function / Class

Post by psurrena »

oh, and I might have made fun of this but it has just saved me a ton of work. It must be worth something:

Code: Select all

public function createTabs($tabCount){
    $this->tabCount=$tabCount;
    $output.="<div id=\"tabs\">\n";
    $output.='<ul class="horizontal-menu">';
    for($i=1; $i<=$tabCount; $i++){
        $output.='<li><a href="#t'.$i.'">{t'.$i.'_title}</a></li>';
    }
    $output.="</ul>\n\n";
 
    for($i=1; $i<=$tabCount; $i++){
        $output.='<div id="t'.$i.'">{t'.$i.'_content}</div>';
    }
    $output.="</div>\n";
 
    return $output;
}
Post Reply