Welcome Guest, Not a member yet? Register   Sign In
Data to View
#1

(This post was last modified: 04-11-2015, 08:11 AM by Urastor.)

Hellu, Smile
Let's say I want to make my controller a little bit more cleaner and my view more "attractive".
At the moment I'm using this code in my controller method:

PHP Code:
[...]
 
       $catString '';
 
       if (is_null($year) && $menuIndex === 2) {
 
           for ($cy = (int) date('Y'); $cy >= $this->config->item('cat_show_til_year'); $cy --) {
 
               $catString .= '<h2 class="cat_year"><a href="/category/search/' $category '/' $cy '">' $cy '</a></h2>';
 
               $catString .= $this->createCategoryPageString($this->site->getArticlesByCat($category$menuIndex$cy8));
 
           }
 
       } elseif (! is_null($year) && $menuIndex === 2) {
 
           $catString .= $this->createCategoryPageString($this->site->getArticlesByCat($category$menuIndex$year));
 
           $this->data['site_title'] .= ' &gt; ' $year;
 
       } else {
 
           $catString .= $this->createCategoryPageString($this->site->getArticlesByCat($category$menuIndex));
 
       }
[...] 

($this->site.. is a reference to the model)
The method createCategoryPageString, who I'm referring to, looks like this:

PHP Code:
   private function createCategoryPageString($result)
 
   {
 
       if (count($result) === 0)
 
           return '<p>Keine Beiträge vorhanden..</p>';
 
       
        $pages 
'';
 
       foreach ($result as $page) {
 
           $img_thumb parse_url(img_url() . 'akt/' $page->beiName '/thumbnail.jpg')['path'];
 
           if (file_exists(ltrim($img_thumb'/'))) {
 
               $img_link '<img src="' $img_thumb '" alt="' $page->beiTitel '" title="' $page->beiTitel '">';
 
           } else {
 
               $img_link '<img src="' img_url() . 'article_img.png" alt="' $page->beiTitel '" title="' $page->beiTitel '">';
 
           }
 
           
            $pages 
.= '<article class="cat_container"><header class="cat_header"><h2 class="hidden">Kategorie</h2>';
 
           $pages .= $img_link;
 
           $pages .= '</header><div class="cat_body">';
 
           $pages .= '<h2><a href="/page/show/' $page->beiName '">' $page->beiTitel '</a></h2>';
 
           $pages .= '<p>' character_limiter(strip_tags($page->beiInhalt), 130'...') . '</p>';
 
           $pages .= '</div></article>';
 
       }
 
       
        return $pages
;
 
   

As you can see, both methods using string concatenation to create a string, that represents an article. The problem is: all this happens in the controller, which should only use logic and not build the html for the view, right? The $catString is then send to the view and I can output the result with echo, but I'm curious how I could write it in the view and make everything a little bit more clean. I know I could just write it down but then the logic is just in the view and argh.. can't get it.

How should I do such things? Or is this the right way?
-.-.-.-.-.-.-.-.- Angel -.-.-.-.-.-.-.-.-
Reply


Messages In This Thread
Data to View - by Urastor - 04-10-2015, 03:50 PM
RE: Data to View - by ivantcholakov - 04-10-2015, 05:31 PM
RE: Data to View - by RobertSF - 04-10-2015, 05:43 PM
RE: Data to View - by Urastor - 04-10-2015, 07:10 PM
RE: Data to View - by RobertSF - 04-10-2015, 08:59 PM
RE: Data to View - by Urastor - 04-11-2015, 08:10 AM
RE: Data to View - by CroNiX - 04-11-2015, 10:01 AM
RE: Data to View - by Urastor - 04-11-2015, 11:09 AM
RE: Data to View - by InsiteFX - 04-12-2015, 03:08 AM



Theme © iAndrew 2016 - Forum software by © MyBB