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 4112474
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:12:02+00:00 2026-05-20T22:12:02+00:00

I am currently trying to separate my Views as much as possible from my

  • 0

I am currently trying to separate my Views as much as possible from my Controllers. Ideally I would like to have minimal PHP in my view, except variable names, etc. These variables are being passed in from the controller.

But is it clean to have IF statements (or the like) within a view?

For example

// Controller 

$data['status'] = 'pass';

$this->load->view("Status Page", $data);

And ..

<!-- View -->
<div>
    <?php if($status === 'pass') { ?>
    <img src='passIcon.jpg'>Pass
    <?php } else { ?>
    <img src='failIcon.jpg'>Fail
    <?php } ?>
</div>

The closest thing I found to an answer on SO was
Conditionals in Views

This was for ASP, and I guess the principles still apply. I could bring the conditional statements back into the controller, but then the controller would be creating HTML and sending it to the view, which isn’t right either.

Is there any way to avoid this cross over? Or will there always be fragments of PHP in views?

  • 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-20T22:12:02+00:00Added an answer on May 20, 2026 at 10:12 pm

    From my point of view, it’s the view’s job to render the data , so if you need conditions to display it proprely then by all means do it , as it will avoid duplicate html code to split it in 2 views and test the var in the controller .

    Allso another good practice would be to use the alternative syntax in the view as it makes following gode much easyer . Eg :

    <!-- View -->
    <div>
        <?php if ( $status === 'pass' ) : ?>
            <img src='passIcon.jpg'>Pass
        <?php else : ?>
            <img src='failIcon.jpg'>Fail
        <?php endif; ?>
    </div>
    

    However taking you’re example a bit further you can have the src set in the controller ( i must admit that there will be times where you’ll need conditional in views ) :

    Controller

    $data['src'] = ( $data['status'] === 'pass' ) ? 'passIcon.jpg' : 'failIcon.jpg';
    $data['text'] = ( $data['status'] === 'pass' ) ? 'Pass text' : 'Fail text';
    
    $this->load->view("Status Page", $data);
    

    View

    <!-- View -->
    <div>
        <img src='<?php echo $src; ?>'><?php echo $text; ?>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently trying at add ajax to a site, after much reading I discovered that
Im currently trying to downlaod a audio track from a WCF, i need some
I'm currently trying out Aptana Studio 3 for PHP development (I'm pretty new to
I'm currently trying to get the most popular productID from my MSSQL Database. This
I'm trying to incorporate grand central dispatch in my table view. I have a
I'm trying to pass a null value from a RenderAction to another view. But
We have a set of views (quite a few) that we're trying to implement
I'm trying to build an eclipse plugin view where I have a view that
I have a simple controller that goes to a view under views/mobile/home/index.html.haml It works
Im currently trying to learn some stuff about encryption, it's algorithms and how it

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.