include css js in view page of codeigniter

不羁岁月 提交于 2020-01-17 01:42:06

问题


hey everyone i'm facing a problem in codeigniter how to include css and jquery.

+application
  +views
    -welcome_message.php
  +assets
     +css
      -template.css
     +js
+system
+user_guide.


<link href='<?php echo base_url(); ?>application/assets/css/template.css' rel="stylesheet" type="text/css" />

firebug show path like:-

<link type="text/css" rel="stylesheet" href="http://localhost/CodeIgniter_2.1.3/application/assets/css/template.css">

i don't understand why it's css is not working.

i find this thing on firebug:-

<link type="text/css" rel="stylesheet" href="http://localhost/CodeIgniter_2.1.3/assets/css/template.css">
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /CodeIgniter_2.1.3/assets/css/template.css was not found on this server.</p>
</body></html>
</link>

回答1:


Best practice is to create an assets Directory outside the APPLICATION Directory not inside.

so you're directory structure would be

+APPLICATION
+ASSETS
+SYSTEM

its up to you how you divide the resources inside the ASSETS DIrectory

now you can use base_url()/assets/your-directories/yourfiles.ext to access whatever you wanted. Or create your own helper

i.e

style_url();
script_url();
image_url();



回答2:


Place css in root folder of site, inside assets dir and try this:

<link href='<?php echo base_url(); ?>assets/css/template.css' rel="stylesheet" type="text/css" />

Also, check image paths in it.




回答3:


Try like this

<link rel="stylesheet" href="<?php echo site_url();?>/assets/css/template.css">

We cont use base url in some cases that your site url will include the index.php but your base url will not.And you need to give type="text/css" for this.

And also makesure that your css files should have the permissions to read and execute(may also)




回答4:


So Simple

Set Your base_url in application/config/config.php file example given below

$config['base_url'] = 'your site url here';

After include CSS or JS files as

<!-- If Your Path is yoursitefolder/theme/css/style.css -->
<link rel="stylesheet" href="<?php echo base_url('theme/css/style.css'); ?>"> 

<!-- If Your Path is yoursitefolder/theme/js/vendor.js -->
<script src="<?php echo base_url('theme/js/vendor.js'); ?>"></script>


来源:https://stackoverflow.com/questions/17060736/include-css-js-in-view-page-of-codeigniter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!