开发者

How to load a css in CodeIgniter?

I'm new to programming. I want to fetch the css saved in DB and load in a php file. I'm using CodeIgniter. After loading the css, I want to link to it as an external css. I tried following, but it is not working.

EDIT: defaultCss.php is the file in which I want to load the css.

$strTemplate.="<link rel='stylesheet' type='text/css' href='".base_url()."user/defaultCss.php'>"

While, I view the page source it gives "Page not found" error.

Below are my controller and view code.

     function loadDefaultCSS(){
     $this->load->model('UserModel');
     $this->UserModel->loadDefaultCSS(); 
     }

View :

        if(isset($strTemplateStyles) && $strTemplateStyles!="") {
        echo $strTemplateStyles;
        }

Model function :

     function loadDefaultCSS($strTemplateStyles){
      $data['strTemplateStyles']=$strTemplateStyles;          
      }

Why this is not working ? what i开发者_如何学JAVAs the issue ?


You can use template library for codeigniter.

It provides more flexibility for handling views, loading js and css files. Also it provides an option for splitting the views into sections like header, content, footer etc. The template library link i have provided above is easy to use and integrate. It also has very good documentation.

In the above template library the css and js files can be loaded as follows (write below code in controller) -

For loading css files -

$this->template->add_css('path to css file');

For loading js files -

$this->template->add_js('path to js file');

For detailed documentation you can refer above hyperlink.


Well the name of your controller action is loadDefaultCSS, so I would expect the URL for the generated stylesheet to be: (assuming your controller is indeed called User)

base_url()."user/loadDefaultCSS"

Does this work?:

$strTemplate .= '<link rel="stylesheet" type="text/css"
    href="'.base_url().'"user/loadDefaultCSS">';


I can see a few strange things in your code:

  • You should not use .php in your CI URLS
  • How can your view possibly get the style of the user when you're not passing it to the view from your controller?
  • How do you know what user it concerns? I assume you have not posted all your code?

What happens if you actually open the stylesheet URL that you generate? Does it throw a 404? A CI error?


The Best way to load css is to pass css paths from controller to view and render it from view in large application we its not good practice to load everything in header it can cause performance issue

login_view.php / view

css code

   <?php
    if(!empty($css_files)){
        foreach ($css_files as $css_path) {
        ?>
            <link rel="stylesheet" href="<?php echo $css_path;?>">
            <?php
        }
    }
?>

login.php /controller

$data['css_files'] = array(
base_url('assets/bootstrap/css/bootstrap.min.cs'),
base_url('assets/plugins/iChecksquare/blue.css'),
'https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css');


$this->load->view('login',$data);

same technique you can use for javascript libs

note that sequence of the files are sensitive

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜