[英]how to load css file in codeigniter
Hi Frnds i cannot able to load css file in codeigniter.嗨 Frnds 我无法在 codeigniter 中加载 css 文件。 so please tell me how to load.
所以请告诉我如何加载。 Codeigniter sample Directory Structure
Codeigniter 示例目录结构
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 & Privacy Policy</li>
</ul>
</div>
</div>
</div>
</body>
</html>
Note:注意:
Please tell me how to load css file in codeigniter.请告诉我如何在 codeigniter 中加载 css 文件。 Even if can tell me how load javascripts and images also.
即使可以告诉我如何加载 javascripts 和图像。 I am a beginner in codeigniter MVC Framework.
我是 codeigniter MVC 框架的初学者。
First go to application/config/autoload.php.首先去application/config/autoload.php。 Then add
$autoload['helper'] = array('html','url');
然后添加
$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请访问用户指南http://ellislab.com/codeigniter%20/user-guide/helpers/html_helper.html
There are many ways to include css-files, eg:有很多方法可以包含 css 文件,例如:
Load this helper: $this->load->helper('url');
加载这个助手:
$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'); ?>">
In your view do use site_url();在您看来,请使用site_url(); to load css,js and images as:
将 css、js 和图像加载为:
<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:把它放在head标签和任何你想显示图像的地方使用这个:
<img title="yourimagetitle"src="<?php echo site_url();?>assets/images/yourimage.extension" height="100" width="200">
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')并加载这个帮助文件 $this->load->helper('url')
you can use my answer on this question how to load css, js dynamically in codeigniter您可以在这个问题上使用我的回答如何在 codeigniter 中动态加载 css、js
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?您也可以使用本教程如何有条件地加载 css、javascript?
The best practice in codeigniter for set up the assets as below flow: codeigniter 中设置资产的最佳实践如下流程:
But you want the flow as below:但你想要的流程如下:
So if you want to keep the assets folder inside the application, then you've to do some extra functionality as below: First, make sure that you've loaded the URL helper in your controller which is:因此,如果您想将 assets 文件夹保留在应用程序中,那么您必须执行一些额外的功能,如下所示:首先,确保您已在控制器中加载了 URL 帮助程序,即:
$this->load->helper("url");
you have to first create a helper named "my_helper.php" at "application/helpers" directory with this code:您必须首先使用以下代码在“application/helpers”目录中创建一个名为“my_helper.php”的帮助程序:
if ( ! function_exists('asset_url()'))
{
function asset_url() {
return base_url().'application/assets/';
}
}
Now, you have to load this helper into your controller as below:现在,您必须将这个助手加载到您的控制器中,如下所示:
$this->load->helper("my_helper");
Now replace your .htaccess code at 'application/'' directory with the below code:现在用以下代码替换“application/”目录中的 .htaccess 代码:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|assets|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
After doing all the above functionality, you have to declare your assets in view as below:完成上述所有功能后,您必须在视图中声明您的资产,如下所示:
<link rel="stylesheet" href="<?php echo asset_url(); ?>css/style.css">
First open config.php file and give base_url like首先打开 config.php 文件并给出 base_url 之类的
$config['base_url'] = 'http://localhost/your_file_name/';
after this load helper into your controllers file like this:在此加载助手到您的控制器文件之后,如下所示:
$this->load->helper('url');
then goto your view folder and give link like this:然后转到您的视图文件夹并提供如下链接:
<link rel="stylesheet" href="<?php echo base_url(); ?>assets/stylesheet/main.css">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.