【问题标题】:Codeigniter - How to set multiple template layout and assets (css,js and images)?Codeigniter - 如何设置多个模板布局和资产(css、js 和图像)?
【发布时间】:2015-12-27 17:09:19
【问题描述】:

我正在为我的 Web 项目使用 codeigniter php 框架。我的问题是如何为css、js和图像等资产设置多个模板布局和基本路径?我想设置前端、登录和后端布局。所以我需要设置 3 个布局。

我的项目结构

这些是我的项目的示例代码。

MY_Controller.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class My_Controller extends CI_Controller {
  protected $layout = 'admin';

  protected $stylesheets = array(
    'app.css'
  );
  protected $javascripts = array(
  'app.js'
  );

  protected function render($content) {
    $view_data = array(
      'content' => $content,
      'stylesheets' => $this->get_stylesheets(),
      'javascripts' => $this->get_javascripts()
    );
    $this->load->view($this->layout,$view_data);
  }

  protected function get_stylesheets() {
    return $this->stylesheets;
  }

  protected function get_javascripts() {
    return $this->javascripts;
  }


}

主页.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends My_Controller {
  public function index() {
    $content = $this->load->view('home/index',null,true);
    $this->render($content);
  }
}

?>

我在互联网上找到了这个链接,但不适合我的项目。 http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html

【问题讨论】:

  • 我使用 williamsconcepts 模板,它对我来说效果很好。为什么不适合你??只是想知道?

标签: php codeigniter


【解决方案1】:

我用过这样的,希望这个样本对你有帮助

用户控制器(controllers/user.php)

<?php
    class User extends CI_Controller 
    {
        function displayuser()
        {
            $data['users'] = array(1,2,3);
            $data['inc_page'] = 'display'; // views/display.php page
            $this->load->view('user_layout', $data);
        }
    }
?>

显示视图 (views/display.php)

<table border="0" cellspacing="0" cellpadding="3">
    <?php
        foreach($users as $userid)
        {
    ?>
    <tr>
        <td><?php echo $userid;?></td>
    </tr>
    <?php
        }
    ?>
</table>

用户布局 (views/user_layout.php)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php echo $this->load->view($inc_page); ?>
</body>
</html>

【讨论】:

    【解决方案2】:

    你可以使用 site_url(); 在根目录下创建一个 css 文件夹。 假设该文件夹被命名为“css”。

    然后假设我们在该文件夹中创建了名为 style.css 的文件。 所以我们只需要在 site_url 中调用它。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    相关资源
    最近更新 更多