0

If I have 10 images, 2 javascript files, and 4 css files that need to be included inside a Ci view... How is the best way to call all the files? I've tried calling all the external files using $this->load->view('image1.png') and $this->load->view('style.css'). But it doesn't seem to work properly. Any ideas on how to better approach this problem?

Michael Grigsby
  • 11,467
  • 9
  • 33
  • 52
  • you're supposed to call an html/php file in your view, which is a template, not the js,css or images. its just like your making a static page – ianace Jan 05 '12 at 02:27

1 Answers1

4

You're not using the view method correctly there. You typically assign one view and pass things like js and css to the template. This can vary depending on how you use the framework.

$this->load->view('path-to-view'); will look for a view in the view folder and not an arbitrary file.

You might look into this: http://codeigniter.com/user_guide/helpers/html_helper.html#img

for loading images, though I personally think it's pointless to call a framework's method for a basic html element like an image.

There are cases when you would use multiple views, like views to be returned as strings - loops and such may need these - but that doesn't look like the case in your question. Just in case though here's the view docs:

http://codeigniter.com/user_guide/general/views.html

To elaborate further, the general idea is to use the CI controller to handle the data for your page, pass the necessary template data to the template (like your js and css specific to this page) then assign the necessary data to the view and pass that view to the template. You may be wondering what I mean by template too, since out of box CI loads views progressively if you just call them sequentially.

in your controller you may pass the view an array of header info:

$data['css'] = array('some-path.css','another-path.css');
$this->load->view('your-view', $data);

so in your view that handles the header you might call something like this:

<head>
<?php foreach($css AS $c): ?>
    <link rel="stylesheet" type="text/css" href="<?php echo $c; ?>">
<?php endforeach; ?>
</head>

Here's a fair link to CI templates:

How to Deal With Codeigniter Templates?

It's a versatile framework with many options for using it however you are most comfortable.

Community
  • 1
  • 1
Kai Qing
  • 18,793
  • 5
  • 39
  • 57
  • I swear this is frustrating. Ci is a peace of crap when it comes to working with views within views. Okay first, echo img('images/picture.jpg'); - echo's out as html images, but i need just the link path for my css. Secondly, load->view('scripts/jquery.nivo.gallery.js', '', TRUE); ?> - is not working properly. http://crep.tk/nurbell/ - this is the site I'm trying to get the images and everything to work properly on. As you can tell from the Network Tab in the Chrome Console, that there are many errors when loading. – Michael Grigsby Jan 05 '12 at 00:43
  • 1
    CI's view within view is pretty simple so long as you maintain the proper definition of a view. It will be a piece of crap if you try to use it outside the scope of its intention. you can't load a script with view. It doesn't work that way. Like I said, pass the script urls as an array to the primary view and echo it out in a for loop in the head. Usually multiple views called are limited to things like header, content, footer and sidebars, but you can try to use them for anything as small as you need so long as you know when to use a view and when to just call a script INSIDE a view. etc – Kai Qing Jan 05 '12 at 00:51
  • Or just hardcode the dependencies into the view. – starsinmypockets Jul 14 '13 at 05:28