Zero items vs. x items in a view

Published on and tagged with cakephp  software engineering  view

Probably most applications have a scenario where you have to display either zero items or one or more items. A blog, for example, can contain posts or no posts. If there are no posts, you may want to inform the visitor when the blog will start. In the other case you simply show the posts. As you see, what is shown depends on the number of posts (resp. the number of items).

This leads to the question: Where do you decide what is shown? In the controller or in the view?

One could argument that we have two different views, and so the controller has to decide which view to use. In this case the code would look like:

// posts_controller.php
function index() {
    $posts = $this->Post->findAll();

    if (!empty($posts)) {
        $this->set('posts', $posts);
    } else {
        $this->render('no_posts');
    }
}

// posts/index.ctp
foreach($posts as $post) {
    echo $post['Post']['title'];
}

// posts/no_posts.ctp
Sorry, no posts yet.

On the other hand one could argument that the view should decide itself how it will react if there is no data provided. Then the code looks like:

// posts_controller.php
function index() {
    $this->set('posts', $this->Post->findAll(););
}

// posts/index.ctp
if (!empty($posts)) {
    foreach($posts as $post) {
        echo $post['Post']['title'];
    }
} else {
    echo "Sorry, no posts yet.";
}

I don’t know which approach is “better”, personally I tend to the first approach where the logic is in the controller.

6 comments baked

  • SebJ

    It’s a nerverending debate, but as it’s “presentation logic”, I’ll go for the 2nd approach.
    Another example, what if there is “no post” | “1 post” | “more posts”?
    I prefer an if-elseif-else condition in the view, and not 3 views.

  • JMG

    I generally use the first solution; but if the view tends to turn into a blank slate, I create an specific view. This is a case-by-case situation.

  • speedmax

    Considering, the amount of view data is required.. usually trivial message, it would make sense to group them together..

    depends on the complexity of the view i guess, for simple “no found” message, many would agree to go the second approach, whereas a complex not found view with suggested information such as ebay or google not-found view.. probably the first approach.

    that is why many template engine, there is a presentational construct loop-else for view. for example, h2o template which i created use following syntax.

    {% for post in posts %}
    {{ post.name }}
    {{ post.content }}
    {% else %}
    there a currently not blog posts
    {% endfor%}

  • cakebaker

    @all: Thanks for your comments!

    @SebJ: As the others already said, it depends on the situation. If it is only a little bit of text that’s different, than it is overkill to create three views. On the other hand if you have for each case a full-fledged page with specific information, then it’s imho better to create separate views.

  • Abba Bryant

    I know it’s overkill but I tend to create an element that can either recieve the posts data or query for it if the data isn’t provided.

    I like to keep my view logicless ( as in that anything with embedded display logic comes in via a renderElement call )

    Using the reusable requestAction trick and some cacheing as well as being very careful when i let the view grab the data or the controller. I find this works quite well.

    Also if you never see a requestAction or more than a couple of lines of concurrent php in a view file. If you need to find the logic its in the elements folder in subfolders named by controller.

    Easy.

  • cakebaker

    @Abba Bryant: Thanks for your comment!

    I think it depends on the situation whether your solution is overkill. If you add this functionality to almost each element, then it is probably overkill ;-)

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License