Its my first project using CodeIgniter and it is not as easy as it seems.
I have to import different JSs and CSSs in different pages and I'm stuck.
First of all, I've seen that hardcoding echos are not CI way of doing it so I made a simple class like
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Fileload {
public function loadjs($filename)
{
echo '<script language="javascript" type="text/javascript" src="'.$filename.'"></script>';
}
public function loadcss($filename)
{
echo '<link rel="stylesheet" type="text/css" href="'.$filename.'" >';
}
}
?>
And in my controller I used it like
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->library('fileload');
$this->load->view('head');
$this->load->view('mainpage');
$this->fileload->loadjs('//cdn.jsdelivr.net/jquery.slick/1.6.0/slick.min.js');
$this->load->view('tail');
}
}
But the slick library supposed to be at the bottom right above the 'tail' is at the top inside of head> tag, which is inside view('head');
It seems that the controllers' methods are not running in the sequence I've wrote it down. It should've echoed the script file first.
Can anybody explain how this CodeIgniter controller works??
答案 0 :(得分:0)
您可以做的另一件事是将刀片模板解析器实现到CodeIgniter中。刀片将允许创建部分,这样,包含特定于页面的脚本将更容易/更清晰。 https://github.com/PhiloNL/Laravel-Blade是您可以获得它的地方。请注意,你也必须使用作曲家。
答案 1 :(得分:0)
我只是通过制作加载JS和CSS的视图
来做到这一点 $data['css']=array('main.css','navbar.css');
$data['csscdn']=array('//cdn.jsdelivr.net/jquery.slick/1.6.0/slick.css');
$data['js']=array('typeahead.js','navbar.js');
$data['jscdn']=array('//cdn.jsdelivr.net/jquery.slick/1.6.0/slick.min.js');
$this->load->view('head',$data);
$this->load->view('mainpage');
$this->load->view('tail',$data);
控制器上有这样的东西
<?php
if(isset($css)){
foreach($css as $script):?>
<link rel="stylesheet" href='<?php echo base_url()."bydcss/".$script;?>'>
<?php endforeach;
}
if(isset($csscdn)){ foreach($csscdn as $script):?>
<link rel="stylesheet" href='<?php echo $script;?>'>
<?php endforeach;
}
?>
for head.php
<?php
if(isset($js)){
foreach($js as $script):?>
<script type='text/javascript' src = '<?php echo base_url()."bydjs/".$script;?>'></script>
<?php endforeach;
}
if(isset($jscdn)){ foreach($jscdn as $script):?>
<script type='text/javascript' src = '<?php echo $script;?>'></script>
<?php endforeach;
}
?>
for tail.php
在视图上有点脏,但对控制器来说更简单