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.
Related
I am trying to load a custom CSS file for just my 404 page in my child theme's header.php. I am able do that by checking for 404 status with the built-in WP function, and when the condition is true, insert link to the file. For some reason though, the site header (including navigation menu) disappears. Any suggestions to get both the header to display and use the 404 function? Code for linking to the file is below.
<head>
<?php
if ( is_404() )
{
echo '<link rel="stylesheet" type="text/css" href="/wp-content/themes/theretailer-child/404style.css">';
}
?>
<?php wp_head(); ?>
</head>
Thanks in advance.
You should add Your CSS file the Wordpress Way:
add_action('wp_enqueue_scripts', 'my_custom_css');
function my_custom_css()
{
if(is_404())
{
wp_enqueue_style('custom-404-css', get_stylesheet_directory_uri() . '/css/custom-404-css.css');
}
}
This code should be placed in your functions.php on your theme.
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 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.
I have read some posts about how to add css style in mPDF, I am using Yii framework and I tried many ways with no result, any idea about how to make it work?
$this->layout="//layouts/pdf";
$mPDF = Yii::app()->ePdf->mpdf();
$html = $this->render('pdf', array('dataProvider'=>$dataProvider), true);
$stylesheet = file_get_contents(Yii::app()->request->baseUrl.'/css/print.css');
$stylesheet .= file_get_contents(Yii::app()->request->baseUrl.'/css/main.css');
$mPDF->WriteHTML($stylesheet, 1);
$mPDF->WriteHTML($html, 2);
$mPDF->Output('Inmuebles',EYiiPdf::OUTPUT_TO_BROWSER);
Thats the controller code.
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/print.css" media="print" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/main.css" />
Thats the code in the layout/pdf
Error:
file_get_contents(/yii/yiitest/css/print.css): failed to open stream: No such file or directory
Try this way (in the controller), it worked for me:
$mPDF = Yii::app()->ePdf->mpdf();
$html = $this->render('pdf', array('dataProvider'=>$dataProvider), true);
$stylesheet = file_get_contents(/* path to first css file */);
$stylesheet .= file_get_contents(/* path to second css file */);
$mPDF->WriteHTML($stylesheet, 1);
$mPDF->WriteHTML($html, 2);
echo $mPDF->Output('Inmuebles',EYiiPdf::OUTPUT_TO_BROWSER);
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