how to load css file in codeigniter
您好 Frnds 我无法在 codeigniter 中加载 css 文件。所以请告诉我如何加载。
Codeigniter 示例目录结构
a) 样式表
b) javascript
c) 图像
控制器:
1
2 3 4 5 6 7 8 9 |
查看
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
<html>
<head> <link rel=“stylesheet” type=“text/css” href=a€?assets/stylesheets/main.cssa€? /> </head>
|
注意:
请告诉我如何在 codeigniter 中加载 css 文件。即使可以告诉我如何加载 javascripts 和图像。
我是 codeigniter MVC 框架的初学者。
首先进入application/config/autoload.php。然后添加 $autoload[‘helper’] = array(‘html’,’url’);
1
2 3 4 5 6 7 8 9 10 11 |
<link rel=“stylesheet” href=“<?php echo base_url(‘assets/stylesheets/main.css’)?>”/>
//OR <?php echo link_tag(‘assets/stylesheets/main.css’)?> //Image //Javascript |
请访问用户指南 http://ellislab.com/codeigniter /user-guide/helpers/html_helper.html
- 嗨,老板,如果我使用显示空白页面的 echo base_uri()。如果我使用 echo base_url() 显示没有 CSS 格式的内容。现在请告诉我我需要做什么。注意:我在本地系统中运行 codeigniter
- 你确定你加载了 url helper 吗? config.php 文件中的 base_url() 是什么?
- 你确定我加载了帮助程序,base_url 像这样显示为空 $config[‘base_url’]=”;
- 这是你的文件夹项目
- 嗨老板不工作。我没有创建任何项目文件夹。我将我的文件保存在应用程序文件夹的视图和控制器文件夹中。应用程序文件夹位于 Xampp Server 的 htdocs 文件夹下。所以我不知道如何找到路径。请帮我。
- 请点击这里
- 嗨,老板,我通过上面的链接并成功安装。但是无论我作为一个项目创建什么。我应该将这些文件保存在 modules_core 文件夹下,对吗?
- 我不同意在学习阶段告诉新用户 First go to application/config/autoload.php. Then add $autoload[‘helper’] = array(‘html’,’url’); … … … … … … 手动操作更有意义…
有很多方法可以包含 css 文件,例如:
加载这个助手:$this->load->helper(‘url’);,然后你可以在你的视图中使用它:
或
1
|
- 我做了 echo base_url(‘path/to/css-file’);反而
在模板文件中使用以下行
1
|
并加载这个帮助文件
$this->load->helper(\\’url\\’)
- 我应该在哪里加载视图或控制器的帮助文件。请告诉我。
- 如果我使用上面提到的代码。我收到以下错误:遇到 PHP 错误 严重性:通知消息:未定义变量:基本文件名:views/home_learnersway.php 行号:9 /application/assets/stylesheets/main.css” type=”text/css”>
- 所以代替 $base 使用你的 base_url() 路径。
- 你能告诉我如何识别我的 base_url。因为我在本地系统中运行我的源代码
在您看来,请使用 site_url();将 css、js 和图像加载为:
1
2 |
<link rel=“stylesheet” type=“text/css” href=a€?<?php echo site_url();?>assets/stylesheets/main.cssa€?
<script type=“text/javascript” language=“javascript” src=“<?php echo site_url(); ?>assets/javascript/yourjs.js”> |
把它放在head标签和任何你想显示图像的地方都使用这个:
1
|
<img title=“yourimagetitle”src=“<?php echo site_url();?>assets/images/yourimage.extension” height=“100” width=“200”>
|
首先打开config.php 文件并给出base_url like
1
|
$config[‘base_url’] = ‘http://localhost/your_file_name/’;
|
在这个加载帮助器到你的控制器文件之后,像这样:
1
|
$this->load->helper(‘url’);
|
然后转到您的视图文件夹并提供如下链接:
1
|
codeigniter 中设置资产的最佳实践如下:
- 应用
- 资产
- 系统
但你想要的流程如下:
- 应用程序 > 资产 > css > style.css
- 系统
因此,如果您想将 assets 文件夹保留在应用程序中,那么您必须执行一些额外的功能,如下所示:
首先,确保您已在控制器中加载了 URL 帮助程序:
1
|
$this->load->helper(“url”);
|
您必须首先使用以下代码在 “application/helpers” 目录中创建一个名为 “my_helper.php” 的助手:
1
2 3 4 5 6 7 8 |
if ( ! function_exists(‘asset_url()’))
{ function asset_url() { return base_url().‘application/assets/’; } |
现在,您必须将这个助手加载到您的控制器中,如下所示:
1
|
$this->load->helper(“my_helper”);
|
现在将 \\’application/\\’\\’ 目录中的 .htaccess 代码替换为以下代码:
1
2 3 |
RewriteEngine on
RewriteCond $1 !^(index\\.php|images|assets|robots\\.txt) RewriteRule ^(.*)$ /index.php/$1 [L] |
完成上述所有功能后,您必须在视图中声明您的资产,如下所示:
1
|
你可以在这个问题上使用我的回答
如何在 codeigniter
中动态加载 css、js
1
2 3 4 5 6 7 8 9 10 11 12 13 |
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); } |
比你的看法
1
2 3 |
<!–Load css and js–>
<link rel=“stylesheet” type=“text/css” href=“<?php echo $css; ?>”> <script src=“<?php echo $js; ?>”> |
您也可以使用本教程如何有条件地加载 css、javascript?
来源:https://www.codenong.com/20697954/