Part of the EllisLab Network
   
 
Images in CodeIgniter
Posted: 26 April 2007 01:34 PM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  4
Joined  04-26-2007

Where would be the correct place to put an images directory in the code igniter directroy tree?
How would I reference the images in code eg. <img> tag and in css background element?

Profile
 
 
Posted: 26 April 2007 01:53 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  277
Joined  02-07-2007

I put images/js/css/etc… in a resources folder outside of the system directory.
Accessing it is easy.

<?=base_url();?>resources/images/yourimage.jpg

I use the xhtml element <base> to set a global base url with the PHP CI base_url function. Like this:

<base href="<?=base_url();?>" />

// Now anything with an href will append the base href above before the rest of the URI.

<a href="/resources/images/yourimage.jpg">My image is here</a>
Profile
 
 
Posted: 02 June 2007 02:39 PM   [ Ignore ]   [ # 2 ]  
Grad Student
Avatar
Rank
Total Posts:  51
Joined  05-03-2007
Iksander - 26 April 2007 01:53 PM

I use the xhtml element <base> to set a global base url with the PHP CI base_url function. Like this:

<base href="<?=base_url();?>" />

// Now anything with an href will append the base href above before the rest of the URI.

<a href="/resources/images/yourimage.jpg">My image is here</a>

(Please) help me understand the above ...

Is it true that the above base setting would only apply within the single (e.g. view) file in which it was set?  ... right?

... i.e. it wouldn’t/couldn’t apply “globally” (e.g. like defining a PHP CONSTANT) for one’s whole website? ... right?

I ask while pondering this:
... about the time cost of excessive function calls, like base_url().

 Signature 

Mike

Profile
 
 
Posted: 02 June 2007 05:41 PM   [ Ignore ]   [ # 3 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  277
Joined  02-07-2007

It would apply ONLY within the scope of the DOCUMENT. Don’t look at it as in a ‘single view’ because it doesn’t apply to views, it applies to the XHTML DOCUMENT that is being OUTPUT by the application.

<html>
<
head>
  <
base href="somebaseurl" />
</
head>
<
body>
  <
a href="--base--/whatever/url">link</a>
</
body>
</
html>

In other words, anything between the start and end HTML tags will be affected by the <base> element. If you have another view that is outputting a completely different XHTML document to the browser you would have to define another <base> element (or not if you don’t want it).

[EDIT]
Anything inside the <head> element will not be affected by <base>, only elements within the <body> element can be affected - but, the scope is still within the document since you can only have one <body> element per <html> root element.

Profile
 
 
Posted: 03 June 2007 10:42 AM   [ Ignore ]   [ # 4 ]  
Grad Student
Avatar
Rank
Total Posts:  51
Joined  05-03-2007

Thanks - for the clarification/validation, Iksander.

` looks like the xHTML <base> element would be useful in reducing function calls (e.g. base_url() ), if one needs to place a number of images (or even other objects) within the same HTML document/template.

 Signature 

Mike

Profile
 
 
Posted: 03 June 2007 08:31 PM   [ Ignore ]   [ # 5 ]  
Lab Assistant
RankRank
Total Posts:  139
Joined  07-31-2006

I have

/codeigniter
/www
/www/img
/www/js
/www/css

An image tag is simply /img/someimage.jpg

Why do you need the base tag when its an relative path?

 Signature 

Andrew Somervell Beer

Profile
 
 
Posted: 03 June 2007 09:38 PM   [ Ignore ]   [ # 6 ]  
Grad Student
Avatar
Rank
Total Posts:  51
Joined  05-03-2007
a.somervell - 03 June 2007 08:31 PM

Why do you need the base tag when its an relative path?

Well, typing relative paths all over creation can get messy ... and, more importantly, can be a mess to maintain, if ever changed ... or if the site gets moved.  Life can be easier, if one has an xHTML <base> reference and/or a PHP CONSTANT which can be updated, once i.e in one place.

 Signature 

Mike

Profile
 
 
Posted: 04 June 2007 12:04 AM   [ Ignore ]   [ # 7 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  277
Joined  02-07-2007

the HREF attribute does not have to be set with base_url(). It can easily be used for relative paths, and is much handier than typing a base relative path all over the place, personally.

EDIT, too much beer, not sure that last statement made any sense…

Profile
 
 
Posted: 05 June 2007 02:21 PM   [ Ignore ]   [ # 8 ]  
Summer Student
Total Posts:  3
Joined  06-05-2007

How would the method described here compare to the notes in another thread ?

I am new, trying to make heads or tails as to where to put my images.  And why (assuming there is an advantage to some method over another).

Any guidance?

Profile
 
 
Posted: 05 June 2007 05:45 PM   [ Ignore ]   [ # 9 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  277
Joined  02-07-2007

I prefer this method, because it keeps the ‘public’ resources away from my core functionality - I also separate my application folder from the system folder (by taking it out of the CI directory altogether) to keep CI Core more easily updated.

Generally I will make a folder in the same directory with my separated CI directory named after whatever the project name is - then I have the application directory within that directory with a directory named ‘public’ along side that…

Hope that helps, alot of info if you search for it on the forums.

Profile
 
 
Posted: 05 June 2007 11:44 PM   [ Ignore ]   [ # 10 ]  
Grad Student
Avatar
Rank
Total Posts:  47
Joined  05-29-2007

Optionally you could use the helper “Snappy Source” I created. smile

 Signature 

Blog | Developing on: XAMPP v1.6.2 | Coding with: PHP v4.4.7, CodeIgniter v1.5.3, Prototype v1.5.1

Profile
 
 
Posted: 07 June 2007 01:26 PM   [ Ignore ]   [ # 11 ]  
Summer Student
Total Posts:  3
Joined  06-05-2007

Thanks to you both for the reply.  I’ll sort out which approach works best.  I appreciate the help!

(Love the pic, Gavin.)

Profile
 
 
Posted: 07 June 2007 01:53 PM   [ Ignore ]   [ # 12 ]  
Grad Student
Avatar
Rank
Total Posts:  47
Joined  05-29-2007

Ha ha, thanks

 Signature 

Blog | Developing on: XAMPP v1.6.2 | Coding with: PHP v4.4.7, CodeIgniter v1.5.3, Prototype v1.5.1

Profile
 
 
Posted: 07 June 2007 02:30 PM   [ Ignore ]   [ # 13 ]  
Grad Student
Rank
Total Posts:  64
Joined  06-05-2007

Wow, it’s not every day you learn a new HTML tag.  Nice.

I think the <base> alternative probably works best in terms of efficiency and speed, since you can just create a header view and plop in that base tag right there.  Change the structure, you just change the view file.  Only ever one function call per page (with regards to images).

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 721, on January 06, 2010 09:38 AM
Total Registered Members: 115028 Total Logged-in Users: 62
Total Topics: 122469 Total Anonymous Users: 4
Total Replies: 647385 Total Guests: 461
Total Posts: 769854    
Members ( View Memberlist )