Page 1 of 2

View (MVC) Function / Class

Posted: Mon Nov 03, 2008 9:46 am
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;
}

Re: View (MVC) Function / Class

Posted: Mon Nov 03, 2008 11:13 am
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.

Re: View (MVC) Function / Class

Posted: Mon Nov 03, 2008 11:19 am
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.

Re: View (MVC) Function / Class

Posted: Mon Nov 03, 2008 12:05 pm
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?

Re: View (MVC) Function / Class

Posted: Mon Nov 03, 2008 1:31 pm
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.

Re: View (MVC) Function / Class

Posted: Mon Nov 03, 2008 1:32 pm
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...

Re: View (MVC) Function / Class

Posted: Mon Nov 03, 2008 2:18 pm
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?

Re: View (MVC) Function / Class

Posted: Mon Nov 03, 2008 3:16 pm
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.

Re: View (MVC) Function / Class

Posted: Wed Nov 05, 2008 10:16 am
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;
}

Re: View (MVC) Function / Class

Posted: Wed Nov 05, 2008 10:23 am
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.

Re: View (MVC) Function / Class

Posted: Wed Nov 05, 2008 11:20 am
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; ?>

Re: View (MVC) Function / Class

Posted: Wed Nov 05, 2008 11:58 am
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?

Re: View (MVC) Function / Class

Posted: Wed Nov 05, 2008 12:26 pm
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();
    }
}

Re: View (MVC) Function / Class

Posted: Wed Nov 05, 2008 3:32 pm
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";
?>

Re: View (MVC) Function / Class

Posted: Wed Nov 05, 2008 3:41 pm
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;
}