【问题标题】:CSS Parser in CodeIgniterCodeIgniter 中的 CSS 解析器
【发布时间】:2011-03-21 01:03:39
【问题描述】:

CodeIgniter 框架中是否有任何 CSS 解析器? 如果不是:我应该在哪里存储我的 PHP 文件以解析 css(整洁的解决方案)?在 webroot 中?

【问题讨论】:

  • “解析”是什么意思。只是常规的<link href=...REAL 解析内容?
  • 我的意思是替换(即用mydomain.tld替换#url#)

标签: php css codeigniter


【解决方案1】:

我使用了两种解决方案:

应用程序/控制器/one.php

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

class One extends CI_Controller
{
  public function index()
  {
    $this->load->helper('url'); // important!, check auto-load note
    $this->load->view('one');
  }
}

/* End of file one.php */
/* Location: ./application/controllers/one.php */

application/views/one.php

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>One</title>
  <link rel="stylesheet" href="<?php echo base_url();?>css/style.css" type="text/css" media="screen"/>
</head>
<body>

<!-- page content -->

</body>
</html>

为了自动加载helper:

如果你发现你需要一个特定的帮助者 应用程序,您可以告诉 CodeIgniter 在系统期间自动加载它 初始化。这是通过打开 application/config/autoload.php 文件并将帮助程序添加到 自动加载数组。

这样就不必在每个控制器中写入$this-&gt;load-&gt;helper('url');

另一个解决方案是从您的主配置文件 (application/config/config.php) 中检索“base_url”项并避免加载 URL 帮助文件:

application/controllers/two.php

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

class Two extends CI_Controller
{
  public function index()
  {
    $this->load->view('two');
  }
}

/* End of file two.php */
/* Location: ./application/controllers/two.php */

application/views/two.php

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Two</title>
  <!-- We retrieve the config item -->
  <link rel="stylesheet" href="<?php echo $this->config->item('base_url'); ?>css/style.css" type="text/css" media="screen"/>
</head>
<body>

<!-- page content -->

</body>
</html>

希望对你有帮助

【讨论】:

    【解决方案2】:

    由于所有内容都是从根目录 (index.php) 显示的,因此无需将 url 放在 CSS 文件中,因为您可以将其相对于根目录。

    【讨论】:

    • 如果我通过 mydomain.tld/index.php 访问我的网站,一切正常。但是如果我使用 mydomain.tld/controller/subsite 相对路径不起作用
    • CSS 文件中的图像是相对于 CSS 文件所在目录的。
    猜你喜欢
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多