Welcome Guest, Not a member yet? Register   Sign In
Displaying html only if condition is true
#1

[eluser]Dagobert Renouf[/eluser]
Hello guys,

I have some part of my controller that populates the $data array with an "admin_notification" value only if there is a notification.
To avoid errors in my view, I first set $data['admin_notification'] = "".

Here's my problem: I can't wrap the <?= $admin_notification; ?> in any paragraph or thing like that, because in the case of no notification and depending on my css, this would display an empty box (concidering it is wrapped in a box) and I just don't want any box at all.

So as I don't know if the notification exists in the view, I need to wrap data in my controller before sending it to the view (example : $data['notification'] = '<p>' . $error . '</p>';

I'm forced to put my html in my controller and that sucks !

How do I make it clean ?
#2

[eluser]Bramme[/eluser]
put the html in a view (box.php or so) and do it like this:

Code:
box.php:
<p>&lt;?=$notification?&gt;</p>

and in your controller:
$data['notification'] = $this->load->view('box', array('notification' => $error), true);
The third variable, true, returns your view as a string, instead of outputting it.
#3

[eluser]xwero[/eluser]
I call those things microviews, view files that only have one purpose and therefor can be reuse by different apps.

You can create a microviews helper
Code:
function view_message($message)
{
    $ci = & get_instance();
    return $ci->load->view('microviews/error_message',array('message'=>$message),TRUE);
}
And functions for other microviews you need.
In your controller or in your view you do
Code:
$data[’notification’] = view_message($error);
You could do the load->view every time but it is monkey work.
#4

[eluser]Dagobert Renouf[/eluser]
thank you Bramme and xwero.

I'll rule with xwero solution, which is clearly the better one (I didn't think a so cool solution would exist, that's great).
#5

[eluser]Bramme[/eluser]
[quote author="Dagobert Renouf" date="1215631732"]thank you Bramme and xwero.

I'll rule with xwero solution, which is clearly the better one (I didn't think a so cool solution would exist, that's great).[/quote]agreed, putting the stuff in a helper is tad clearer.
#6

[eluser]xwero[/eluser]
You have to think about all the typing you don't have to do when you build a site/app, being a lazy typer really pays off Smile




Theme © iAndrew 2016 - Forum software by © MyBB