I have a problem with static file in CodeIgniter.. I try everything for add some css and js file but it's always a 404 error. I have followed some ask here and other place but nothing.
In particular i have followed this answer: How to access static assets in CodeIgniter?
Here is where i link css and js file:
<link href="<?php echo base_url(); ?>assets/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="<?php echo base_url(); ?>assets/static/css/signin.css" rel="stylesheet">
<script src="<?php echo base_url(); ?>assets/bootstrap/assets/js/ie-emulation-modes-warning.js"></script>
<script src="<?php echo base_url(); ?>assets/bootstrap/assets/js/ie10-viewport-bug-workaround.js"></script>
And my directory is this:
/applications
/system
/assets
- bootstrap
- static
Try this :
<link href="<?php echo base_url('assets/bootstrap/dist/css/bootstrap.min.css'); ?>" rel="stylesheet">
If it doesn't work, check in your config file (config/config.php) if the variable $config['base_url'] is correct.
Related
Im trying to load a css file with codeigniter framework in my view. I would like to use the base_url, i followed some tuto on the internet but it's still not working.
My controller :
public function clean_art()
{
$data = array('title' => 'Clean Art');
$this->load->view('games/clean_art', $data);
$this->load->helper('url');
}
The autoload :
$autoload['helper'] = array('url','form','text');;
The view :
<link type="text/css" href="<?php echo base_url(); ?>application/views/styles/cleanArt.css">
The skeleton of codeigniter :
Create assets directory outside of the application directory then move all css,js,images to assets directory.
Ex:
<link rel="stylesheet" href="<?php echo base_url() ?>assets/css/style.css">
It's working for me.
I need to force CSS changes to go live immediately and I found that add a version to CSS would help to do that as bellow.
<link rel="stylesheet" href="css/style.css?version=123456" media="all"/>
I need to automate this as it is difficult to change the main file always when need to do small change to css file.
So I found following line of code(sample) in PHP doing the same job.
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); echo '?' . filemtime( get_stylesheet_directory() . '/style.css'); ?>" type="text/css" media="screen, projection" />
I have tried to convert this line to Smarty, but it is giving error.
code :
<link rel="stylesheet" href="css/style.css?version={#filemtime:css/style.css} />
error:
syntax error: unrecognized tag: #filemtime:...........
Anybody has an idea how to do this ?
Thanks in advance
You can add this modifier to smarty plugin folder,
function smarty_modifier_filemtime($path){
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
return filemtime( YOUR_HOME_DIR.$path );
}
then call it like bellow,
<link rel="stylesheet" href="/css/style.css?v={'/css/style.css'|#filemtime}" type="text/css">
I am a bit confuse about the difference between the two url scenarios like
$config['base_url'] = '';
$config['base_url'] = 'localhost/blabla';
<link rel="stylesheet" type="text/css" href="<?php echo base_url(). 'css/style.css'; ?>" />
If I will set the base_url() as on the first given the site would apply the css but if I set it to the second given example the css would not apply and the browser logs could not found(404) but it has the same generated url like
localhost/blabla/css/style.css
WHY? i am reading the documentation in elislab website but cant figure it out. Any input guys..
$config['base_url'] = '';
$config['base_url'] = 'localhost/blabla';
Try this, should work for the both above:
<?php
$this->load->helper('url');
?>
<link rel="stylesheet" type="text/css" href="<?php echo base_url()?>/css/style.css" />
I am new to wordpress.. I have installed my theme named 'hare'. Now I want to add some javascripts files as well as css files into the index.php page. But I am not finding the desired output.
following is the code i have written..
<?php get_header(); ?>
// Some content
<?php get_footer(); ?>
<!-- JQuery libs
================================================== -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<!-- js jQuery wait for images Plugin ====================== -->
<script src="<?php bloginfo(template_directory ); ?>/javascripts/jquery.waitforimages.js"></script>
<!-- js jQuery flexslider Plugin ====================== -->
<script src="<?php bloginfo(template_url); ?>/javascripts/jquery.flexslider-min.js"></script>
<!-- jQuery Cycle Plugin ====================================== -->
<script src="<?php bloginfo(template_url); ?>/javascripts/jquery.cycle.all.js"></script>
<!-- jQuery Cycle Plugin ====================================== -->
<script src="<?php bloginfo(template_url); ?>/javascripts/jquery.fullscreen-min.js"></script>
<!-- js jQuery jcarousellite Plugin ====================== -->
<script src="<?php bloginfo(template_url); ?>/javascripts/jcarousellite_1.0.1.min.js"></script>
<!-- js Fancybox Plugin ================================= -->
<link rel="stylesheet" href="<?php bloginfo(template_directory); ?>/javascripts/fancyBox-2/jquery.fancybox.css">
<script src="<?php bloginfo(template_url); ?>/javascripts/fancyBox-2/jquery.fancybox.pack.js"></script>
<!--fancybox helpers-->
<link rel="stylesheet" href="<?php bloginfo(template_directory); ?>/javascripts/fancyBox-2/helpers/jquery.fancybox-buttons.css"/>
<script src="<?php bloginfo(template_url); ?>/javascripts/fancyBox-2/helpers/jquery.fancybox-buttons.js"></script>
<!-- js jQuery qtip plugin ====================== -->
<script src="<?php bloginfo(template_url); ?>/javascripts/jquery.qtip-1.0.0-rc3.min.js"></script>
<!-- toTop ====================== -->
<script src="<?php bloginfo(template_url); ?>/javascripts/goToTop.js"></script>
<!-- js jQuery my own functions ====================== -->
<script src="<?php bloginfo(template_url); ?>/javascripts/functions.js"></script>
<!-- <script src="javascripts/jquery.tweet.js"></script> -->
<!-- JS twitter scripts ================================== -->
<script src="http://twitter.com/javascripts/blogger.js"></script>
<script src="http://twitter.com/statuses/user_timeline/EnvatoWebDesign.json?callback=twitterCallback2&count=5"></script>
<!-- End Document
================================================== -->
</body>
</html>
Is my this way is correct??
If not then please correct me..
Thank you in advance
In your comments you may be referring to these guidelines from yahoo. There are, however a few caveats to this rule. Most importantly, Google Analytics prefers it's snippet be placed within the <head> section and won't allow you to use Analytics for Webmaster Tools authentication unless the snippet is in head.
More importantly, you don't want to include your JS and CSS files directly in your theme templates like this. It works, but it is very un-WordPress-y.
The "correct" way to include additional scripts and styles in a WordPress theme is to use the wp_enqueue_script() and wp_enqueue_style() functions within a hook in functions.php, like so:
function my_custom_styles_function() {
wp_enqueue_style( 'my-style', get_stylesheet_directory() . DS . 'javascript' . DS . 'my-plugin' . DS . 'my-plugin-style.css', array(), '1.0' );
...
}
add_action('wp_enqueue_scripts', 'my_custom_styles_function');
WordPress actually has jQuery included by default, although without the $ shortcut established. You can use WordPress's jQuery and start all of your custom scripts with jQuery(...); instead of $(...); but this can cause problems for some plugins. If you want to include your own version of jquery, you should first use wp_dequeue_style() to dequeue the "built-in" jQuery.
Finally, if you do want to include you scripts in the footer, the wp_enqueue_script() function has a flag, $in_footer to defer particular scripts to the footer.
The answer provide by #qccreative is the right one. You should include all the js/css files you need to your page using the functions.php file that you must have within your theme. if you dont have it just create one and make a function for import your files just like the answer above.
you can even manage in what page you want to load your scripts.
also this is the safest way WP provide for including scripts in an organized way.
this hook that wordpress provide WP_ENQUEQUE_SCRIPTS its a function that will add the scripts within your action to the site.
heres another example:
function load_javascript_files(){
wp_register_script( 'jquery-accordion', get_template_directory_uri() . '/js/jquery.accordion.js', array('jquery'),true );
wp_enqueue_script('jquery-accordion');
}
add_action('wp_enqueue_scripts', 'load_javascript_files');
Good luck buddy
From the code you have put you would create a header.php file in your themes folder and include the JS in there. An example would be like follows:
`<!DOCTYPE html>
<html>
<head>
<title><?php bloginfo( 'name' ); ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="<?php bloginfo( 'template_url' ); ?>/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="<?php bloginfo( 'template_url' ); ?>/jQuery-validate.js" type="text/javascript"></script>
<link rel="stylesheet" href="<?php bloginfo( 'template_url' ); ?>/style.css" type="text/css" />
<?php wp_head(); ?>
</head>
<body>
/**
* Proper way to enqueue scripts and styles.
*/
function custom_links() {
wp_enqueue_style( 'style-name', get_stylesheet_uri() );
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'custom_links' );
If you need to linked your CSS file than you can use wp_enqueue_style and if you need to linked JavaScript than you can use wp_enqueue_script.
wp_enqueue_scripts this is work on frontend side of wordpress.
admin_enqueue_scripts this is works on backend side of wordpress.
I hope it's help to all people.
I am using this in my viewregistration.php file
<html>
<head>
<link rel="stylesheet" type="text/css" href="<?php echo $base_url; ?><?php echo $css; ?>reg_style.css" />
<link rel="stylesheet" type="text/css" href="<?php echo $base_url; ?><?php echo $css; ?>style.css" />
</head>
<body>
<?php $this->load->view('include/header');?>
<?php $this->load->view('registration_view.php');?>
<?php $this->load->view('include/footer');?>
</body>
</html>
And I am calling it in my conltroller as
$data['title']= 'Registration';
$this->load->view("viewregistration.php", $data);
And in my controller I am using
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->database();
$this->load->model('user_model');
$this->load->model('systemdata');
$this->data['css'] = $this->systemdata->get_css_filespec();
$this->data['scripts'] = $this->systemdata->get_scripts_filespec();
$this->data['base_url'] = $this->systemdata->get_base_url();
But the css file is not loading. It show some error like
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: base_url
and
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: css
What I am doing wrong?
I have used autoload url helper. But the result remains same.
Your variables aren't defined because you're not actually passing them to your view. $data and $this->data are not the same thing.
Here, you are correctly passing the $data array to your view:
$data['title']= 'Registration';
$this->load->view("viewregistration.php", $data);
But notice here how you're assigning your variables to $this->data, which never gets passed to the view:
$this->data['css'] = $this->systemdata->get_css_filespec();
$this->data['scripts'] = $this->systemdata->get_scripts_filespec();
$this->data['base_url'] = $this->systemdata->get_base_url();
You either need to assign your variables to $data or pass $this->data to your view.
I dont think this line is necessary.
$this->data['base_url'] = $this->systemdata->get_base_url();
What you can do instead, is this.
<link rel="stylesheet" type="text/css" href="<?php echo base_url($css.'reg_style.css'); ?>" />
base_url() is a CI function that gives you, the base url.
Sources: http://codeigniter.com/user_guide/helpers/url_helper.html