Hellu,

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, $cy, 8));
}
} elseif (! is_null($year) && $menuIndex === 2) {
$catString .= $this->createCategoryPageString($this->site->getArticlesByCat($category, $menuIndex, $year));
$this->data['site_title'] .= ' > ' . $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?
-.-.-.-.-.-.-.-.-

-.-.-.-.-.-.-.-.-