0

I'm a bit confused here.

I have a simple controller which loads a view. The view contains a form and links some CSS files. I don't really want to do ../../css/global.cssin my link tag. I want to use the base_url() method and then go /css/.

I know a friend uses the following:

  <link href="{base_url}css/style.css" rel="stylesheet" type="text/css" />

However, I can't get that to work. He uses CodeIgniter 1.7 though, I'm using the latest (2.something) version. I'm new to CodeIgniter and I wanted to mess around with it, but I can't even link a simple CSS file :(

My view is in /logic/views/index.php, my css files are in /css/

Thanks a bunch.

1
  • the syntax {base_url} may be from CI's template parsing class - or I believe that smarty templates use this syntax too. (also you could just use an absolute path - /css/global.css) but your app will be more portable using the base_url! Commented Mar 27, 2011 at 16:06

4 Answers 4

4

I put my css files in the root directory and link them like this

<?php echo link_tag('css/forie.css'); ?>  
<?php echo link_tag('css/reset.css'); ?>
<?php echo link_tag('css/main.css'); ?>

Using link_tag allows me to access them easily

Sign up to request clarification or add additional context in comments.

Comments

3

I think your problem is that base_url is a function in ci 2+ so try this instead

<link href="<?php echo base_url() ?>css/style.css" 
rel="stylesheet" type="text/css" />

It depends how you defined base_url if you did an ending slash otherwise just add a slash so

/css/style.css

3 Comments

I tried this, probably should have mentioned it. When I do this ` <link rel="stylesheet" type="text/css" href="<?php echo base_url() ?>css/global.css" media="all" />`, my page cuts off right before the link tag. I don't get any errors, it just cuts off :(
I had to load the URL helper as stated below.. And then I had some problems with my .htaccess file. It worked now, thanks a lot!
base_url() has existed since version 1.2
3

You can use the URL helper to ease your URL woes :)

http://codeigniter.com/user_guide/helpers/url_helper.html

Usage

Load it up in your bootstrap

$this->load->helper('url');

And whenever you need something you can use

echo site_url("/css/style.css");

Or just assign it as a handy base url so you can use it wherever you want.

$base_url = site_url('/');
<link href="{$base_url}css/style.css" rel="stylesheet" type="text/css" />
<?php echo 'base url is' . $base_url?>

Note

Remember to define your proper base URL in the config file before using this method.

1 Comment

Thanks. I used the method Tyler Rice provided in combination with part of yours, apparently I forgot to load the URL helper (silly me). Now I just have to fix something else because well.. they get linked correctly in my source code, but they still don't get loaded.
1

for CI 2+ you can add $this->load->helper('url'); before you load the view and then add <link href="<?php echo base_url().'css/style.css';?>" rel="stylesheet" type="text/css" /> into your view file.

1 Comment

This does not generate relative path structure, but provides full path including http:// for every css etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.