Welcome Guest, Not a member yet? Register   Sign In
how do I include() files within a view?
#1

[eluser]Ascenti0n[/eluser]
Hi all

What I'm trying to do is have html files included within a section of a view file for certain content types/categories.

So say with the controller I have retrieved the files' path from the database, and then call the view - eg:

Code:
$data[$file-path] = "topcs/";
$this->load->view('main_view', $data);

and in the view, I would like to do something like:

Code:
<!--
      CONTENT GOES HERE
-->
        
<?php include($file-path . 'static-page.php'); ?>

As you may have guessed, this didn't work. Can someone please explain what I need to do.

Thanks
#2

[eluser]TheFuzzy0ne[/eluser]
Please could you be more specific than "this didn't work"?

I can see several errors with your code above:

1) I think this is a typo:

$data[$file-path] = "topcs/";

Should be

$data['file_path'] = "topics/";

2) You can't use hyphens in variable names, since they are interpreted as arithmetic operators, and what follows will be read as being a string. I'm sure you'd get a parse error with the code you have.
#3

[eluser]Ascenti0n[/eluser]
Sorry for vagueness. In the interest of keeping this simple and to illustrate better the problem I am facing, I added the following to my view, in place of the previous code:

Quote:<?php include(base_url() . 'welcome-home.php'); ?>

and I see this in my browser:
Quote:Severity: Warning

Message: include() [function.include]: URL file-access is disabled in the server configuration

Filename: views/mic.html

Line Number: 81

any ideas?
#4

[eluser]TheFuzzy0ne[/eluser]
It's because you're trying to include a file from a remote location (or rather via the HTTP protocol), and your server has the furl_open directive disabled. Your base URL starts with http://, which is a URL, but the file path you try to include would probably be better off like this:

Code:
include(APPPATH.'path/to/file'.EXT); # Assuming the file is within your application directory.
#5

[eluser]jedd[/eluser]
I'd probably pull this in to the controller.


Code:
$data[$static_bit] = file ($filename);   // How you establish $filename is up to you
$this->load->view('main_view', $data);

view:
Code:
echo $static_bit;

Establishing $filename, as you've discovered, is non-obvious because you are trying to keep your code repository (within CI) fairly portable. You should rely on the url_base (and similar) rather than go absolute references. You might be able to stick this in your ./assets/static_pages/ directory and still pull it out with the CI functions .. but haven't tried, so can't say for sure.
#6

[eluser]jedd[/eluser]
Oh, just read ... sorry, should have done this first.

[url="http://ellislab.com/codeigniter/user-guide/helpers/file_helper.html"]CI User Guide - for File Helper[/url]:
Quote:read_file('path')

Returns the data contained in the file specified in the path. Example:
Code:
$string = read_file('./path/to/file.php');

The path can be a relative or full server path. Returns FALSE (boolean) on failure.

Note: The path is relative to your main site index.php file, NOT your controller or view files. CodeIgniter uses a front controller so paths are always relative to the main site index.
#7

[eluser]Ascenti0n[/eluser]
Thank you all very much. I will give all of those suggestions a go.

I suppose it would be easier to pull the content from the database, but I just don't want to do that.




Theme © iAndrew 2016 - Forum software by © MyBB