CodeIgniter Forums
How to check if a view exists - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to check if a view exists (/showthread.php?tid=7216)

Pages: 1 2


How to check if a view exists - El Forum - 03-30-2008

[eluser]mikegioia[/eluser]
I was wondering if it was possible to check if a View exists before loading it. The reason I'm asking is because I have user dashboards where they can load different widgets, and each widget has a View. And while the names of the views match up with the names of the widgets stored in the database, I just wanted to add another level of security there so that the script doesn't terminate with an unable to load view error.

If anyone has any suggestions I'd really appreciate it.


How to check if a view exists - El Forum - 03-30-2008

[eluser]Seppo[/eluser]
There's not an "inside" way to do it... you can simply just
Code:
$myview = 'welcome_message';
        if (is_file(APPPATH.'views/' . $myview . EXT))
        {
            $this->load->view($myview);
        } else {
            // The view doesn't exist =(
        }



How to check if a view exists - El Forum - 03-30-2008

[eluser]Tom Glover[/eluser]
I think this might work:
Code:
if ($this->load->view('view name','',TRUE)!== '')
    {
        $this->load->view('view name','',TRUE);
    } else {
        echo 'no vie file';
    }



How to check if a view exists - El Forum - 03-30-2008

[eluser]mikegioia[/eluser]
Thanks guys. Is there any documentation on the $this->load->view() function (or similar system functions) ? I didn't even know there were additional parameters you could send in to it.

I'll try out both of those.


How to check if a view exists - El Forum - 03-30-2008

[eluser]Tom Glover[/eluser]
[quote author="mikegioia" date="1206907082"]Thanks guys. Is there any documentation on the $this->load->view() function (or similar system functions) ? I didn't even know there were additional parameters you could send in to it.

I'll try out both of those.[/quote]

Code:
$this->load->view('view name','',TRUE);
the third param 'true' tells CI to output the the view as a string, this is useful for calling views within views.


How to check if a view exists - El Forum - 03-30-2008

[eluser]Seppo[/eluser]
The documentation about the views is here

With the "$this->load->view('view name','',TRUE);" method, CI triggers an error when you attempt to use a view that doesn't exist, so I'm not sure if that's what you need.


How to check if a view exists - El Forum - 03-30-2008

[eluser]nirbhab[/eluser]
Gud, infact Seppo's way of doing the task is much better. Nice Answer.

Code:
$this->load->view('view name','',TRUE);
//Won't tell whether the file exists or not.

Where as seppo's way actually gets the existence of the file.


How to check if a view exists - El Forum - 03-30-2008

[eluser]mikegioia[/eluser]
Yea I like Seppo's method a lot. I ran some tests using these ideas.

Code:
echo $this->load->view('my_view', '', TRUE);

... produced a 'could not load view' error even though I specified the load to return a string.

Code:
if (is_file(APPPATH.'views/' . $my_view . EXT))
{
     $this->load->view($my_view);
}

... worked like a charm. Thanks a lot guys.

Mike


How to check if a view exists - El Forum - 03-31-2008

[eluser]dark_lord[/eluser]
Since you guys are discussing regarding views, I think I just want to ask a question cause its this is really making me wonder why? and what is the difference, and when is it or what situation will I use it. Here is the killer question.

What is the difference with true in the view with true is typed capitalize and a true which is type in lowercase.

Code:
$this->load->view('home/blog', $data, TRUE);

VS.

Code:
$this->load->view('home/blog', $data, true);

-----

Hope you can enlighten me with this. Thanks!


How to check if a view exists - El Forum - 03-31-2008

[eluser]nirbhab[/eluser]
No Difference.
Boolean Values:
0
false
FALSE

&
1
true
TRUE

All are same.