how to load css file in codeigniter

2019-01-26 19:03发布

问题:

Hi Frnds i cannot able to load css file in codeigniter. so please tell me how to load. Codeigniter sample Directory Structure

  1. views
  2. models
  3. controllers
  4. assets a) stylesheets b) javascript c) images
  5. Config

Controller:

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    class Learnersway extends CI_Controller
    {
        function index()
        {
            $this->load->view('home_learnersway');
        }
    }
    ?>

View

    <html>
    <head>

        <link rel="stylesheet" type="text/css" href=“assets/stylesheets/main.css”   />

    </head>
    <body>
    <div class="wrapper">
    <div class="header">
    <div class="title"><img height="136" src="../images/learnersway.jpg" width="272" /></div>

    <div class="main_menu">
    <ul>
        <li>HTML</li>
        <li>XML</li>
        <li>CSS</li>

    </ul>
    </div>
    </div>

    <div class="main_container">
    <div class="sub_menu">
    <ul>
        <li>HTML Introduction</li>
        <li>HTML Editors</li>
        <li>HTML Basic</li>
        <li>HTML Elements</li>
        <li>HTML Attributes</li>
        <li>HTML Headings</li>
        <li>HTML Paragraphs</li>
        <li>HTML Formatting</li>
        <li>HTML Links</li>
        <li>HTML Head</li>

    </ul>
    </div>

    <div class="sub_container">
    <div>Under Construction...</div>
    </div>
    </div>

    <div class="footer">
    <div class="left">All Rights Reserved @ LearnersWay.com</div>

    <div class="right">
    <ul>
        <li>About Us</li>
        <li>Terms &amp; Privacy Policy</li>

    </ul>
    </div>
    </div>
    </div>
    </body>
    </html>

Note:

Please tell me how to load css file in codeigniter. Even if can tell me how load javascripts and images also. I am a beginner in codeigniter MVC Framework.

回答1:

First go to application/config/autoload.php. Then add $autoload['helper'] = array('html','url');

<link rel="stylesheet" href="<?php echo base_url('assets/stylesheets/main.css')?>"/>
//OR
<?php echo link_tag('assets/stylesheets/main.css')?>

//Image
<?php echo img('asset/images/learnersway.jpg')?>
//OR
<img src="<?php echo base_url('asset/images/learnersway.jpg')?>" />

//Javascript
<script src='asset/javascript/yourscript.js'></script>

Please visit userguide http://ellislab.com/codeigniter%20/user-guide/helpers/html_helper.html



回答2:

There are many ways to include css-files, e.g.:

Load this helper: $this->load->helper('url'); and then you can use this in your view:

<link rel="stylesheet" type="text/css" href=“<?php echo base_url(); ?>path/to/css-file">

or

<link rel="stylesheet" type="text/css" href=“<?php echo base_url('path/to/css-file'); ?>">


回答3:

In your view do use site_url(); to load css,js and images as:

<link rel="stylesheet" type="text/css" href=“<?php echo site_url();?>assets/stylesheets/main.css”  
<script type="text/javascript" language="javascript" src="<?php echo site_url(); ?>assets/javascript/yourjs.js"></script>

Put this both in head tag and wherever you want to show images use this:

<img title="yourimagetitle"src="<?php echo site_url();?>assets/images/yourimage.extension" height="100" width="200">


回答4:

use below lines in your template file

<link rel="stylesheet" href="<?php echo $base?>/application/css/style.css" type="text/css">

and load this helper file $this->load->helper('url')



回答5:

you can use my answer on this question how to load css, js dynamically in codeigniter

    public function index() { 
// View "css_js_view" Page.
 //conditional 
if(your condition){
 $this->data = array( 'css' => site_url()."your css file", //this directory you css 'js' => site_url()."your js file" //this directory you js 
);
 }else{
 $this->data = array( 'css' => site_url()."your css file", //this directory you css 'js' => site_url()."your js file" //this directory you js 
);
 } 
$this->load->view('load_view',$this->data);

}

than on your view

<!--Load css and js--> 
    <link rel="stylesheet" type="text/css" href="<?php echo $css; ?>">
     <script src="<?php echo $js; ?>"></script>

also you can ue this tutorial How to load css,javascript conditionally?