Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3784854
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:26:23+00:00 2026-05-19T11:26:23+00:00

I understand that views should only display information to the user and not do

  • 0

I understand that “views” should only display information to the user and not do any real “thinking”.

If I have a field in a form that i wasnt to restrict some user levels accessing what I do is put a condition into my view:

        <td style="v-align: middle;">
        <?php 
            if ($auth['level_id'] == 6) {

                echo $form->input('product_date',
                    array('class' => 'input-box',
                          'div' => false,
                          'label' => false,                          
                          'readonly' => 'readonly',
                          'style' => 'width:100px; margin-top: 8px; float:left;',
                          'value' => $productiondate,
                          'tabindex' => 3013
                          )
                );
                echo '<div style="padding-left: 10px; float:left;"><a href="#" id="supplier_submit" name="supplier_submit"><img src="/img/submit.png" border="0"/></a></div>';
            }
            else {

                echo $form->input('product_date_ro',
                    array('class' => 'input-box',
                          'div' => false,
                          'label' => false,                          
                          'readonly' => 'readonly',
                          'style' => 'width:100px',
                          'value' => $productiondate,
                          'tabindex' => 3013
                          )
                );

            }
        ?>

        </td>               

What is best practice for not doing this?

Regards

Paul

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T11:26:23+00:00Added an answer on May 19, 2026 at 11:26 am

    You have to make that decision at some point. Wherever you put it, you need the if ($level = 6) switch somewhere. If you absolutely want to keep it out of the view, the only other possible place is the controller. The only thing you could do in the Controller is to render a different View. This is perfect in the sense that it keeps all logic out of the view, but you’ll end up with a lot of duplicate code with only small differences.

    What you could do:

    // Controller
    switch ($level) {
        case 6 :
            $this->render('level6_view');
            break;
        ...
        default :
            $this->render('normal_view');
    }
    
    
    // Views
    echo $this->element('standard_elements');
    echo // something special for level 6
    echo $this->element('rest_of_standard_elements');
    

    This places the logic in the controller while avoiding the worst duplication. The more complex your views get the less workable this solution is though.

    You could also simply abstract the decision a bit to the controller and only set flags for the view:

    // Controller
    $flags = array('render_foo' => false, 'render_bar' => true);
    if ($level == 6) {
        $flags['render_foo'] = true;
    }
    $this->set(compact('flags'));
    

    This would provide better separation of internal logic. The actual decision on what to render would still be done in the View though.

    The best solution for you is probably somewhere in between. The most important thing to remember is that there’s nothing wrong with having logic in the view. It’s virtually impossible to not have if statements in views. Views can be intelligent and full of code, that’s absolutely no problem. They should just not contain any code that is concerned with anything other than outputting the data handed to it by the controller. And, naturally, the code should be as concise and readable as possible. You need to find the right balance between abstracting things to helpers, elements or entirely different view files based on the situation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand that a view should only display stuff and do no logic itself
I have a page which displays a form that a logged-in user can use
If I understand MVC correctly Controllers should only contain logic that glue the model
I understand that partial views are used to render parts of a view. But
I understand that some countries have laws regarding website accessibility. In general, what are
I'm developing a new desktop application that will have several views such as a
I understand that in MVVM: the View knows about the ViewModel the ViewModel knows
I understand that we need to create MXML file to define a view. Suppose
I understand that there are several ways to blend XNA and WPF within the
I understand that they are both supposed to be small, but what are the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.