简体   繁体   English

如何在CodeIgniter中加载CSS?

[英]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. 我想获取保存在数据库中的CSS并加载到php文件中。 I'm using CodeIgniter. 我正在使用CodeIgniter。 After loading the css, I want to link to it as an external css. 加载CSS之后,我想作为外部CSS链接到它。 I tried following, but it is not working. 我尝试了以下操作,但无法正常工作。

EDIT: defaultCss.php is the file in which I want to load the css. 编辑: defaultCss.php是我要在其中加载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 is the issue ? 有什么问题?

You can use template library for codeigniter. 您可以将模板库用于codeigniter。

It provides more flexibility for handling views, loading js and css files. 它为处理视图,加载js和CSS文件提供了更大的灵活性。 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) - 在上述模板库中,css和js文件可以按以下方式加载(在控制器中的以下代码中编写)-

For loading css files - 用于加载CSS文件-

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

For loading js files - 用于加载js文件-

$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 ) 好吧,您的控制器动作的名称是loadDefaultCSS ,所以我希望生成的样式表的URL为:(假设您的控制器确实称为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 您不应该在CI URL中使用.php
  • 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? 如果您实际上打开所生成的样式表URL,会发生什么? Does it throw a 404? 它会抛出404吗? A CI error? CI错误?

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 加载css的最佳方法是在大型应用程序中从控制器传递css路径以查看并从视图中呈现它,我们不建议将所有内容加载到标头中,否则会导致性能问题

login_view.php / view login_view.php /视图

css code CSS代码

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

login.php /controller login.php /控制器

$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 您可以用于javascript库的相同技术

note that sequence of the files are sensitive 请注意,文件顺序是敏感的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM