gpt4 book ai didi

CodeIgniter 和布局?

转载 作者:行者123 更新时间:2023-12-04 02:44:24 25 4
gpt4 key购买 nike

很难说出这里问的是什么。这个问题是模棱两可的、模糊的、不完整的、过于宽泛的或修辞的,无法以目前的形式得到合理的回答。如需帮助澄清这个问题以便重新打开它,visit the help center .




9年前关闭。




什么 CodeIgniter 库可以提供这种功能?

http://media.railscasts.com/videos/008_content_for.mov

在 Rails 上看起来很简单,但我无法在 codeigniter 上找到一种简单的方法来实现这一点..请帮助..我讨厌必须在文档正文中插入我的样式或 javascript

最佳答案

我讨厌 Smarty 或 http://williamsconcepts.com/ci/codeigniter/libraries/template/ 之类的模板

使用我自己的 Zend-like 布局实现。

添加钩子(Hook)/application/hooks/Layout.php:

<?php

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

/**
*
*/
class Layout {

function render() {

global $OUT;
$CI = & get_instance();
$output = $CI->output->get_output();
if (!isset($CI->layout)) {
$CI->layout = "default";
}
if ($CI->layout != false) {
if (!preg_match('/(.+).php$/', $CI->layout)) {
$CI->layout .= '.php';
}

$requested = BASEPATH . '../application/layouts/' . $CI->layout;
$default = BASEPATH . '../application/layouts/default.php';

if (file_exists($requested)) {
$layout = $CI->load->file($requested, true);
} else {
$layout = $CI->load->file($default, true);
}

$view = str_replace("{content}", $output, $layout);
$view = str_replace("{title}", $CI->title, $view);

$scripts = "";
$styles = "";
$metas = "";
if (count($CI->meta) > 0) { // Массив с мета-тегами
$metas = implode("\n", $CI->meta);
}
if (count($CI->scripts) > 0) { // Массив со скриптами
foreach ($CI->scripts as $script) {
$scripts .= "<script type='text/javascript' src='" . base_url() . "js/" . $script . ".js'></script>";
}
}
if (count($CI->styles) > 0) { // Массив со стилями
foreach ($CI->styles as $style) {
$styles .= "<link rel='stylesheet' type='text/css' href='" . base_url() . "css/" . $style . ".css' />";
}
}

if (count($CI->parts) > 0) { // Массив с частями страницы
foreach ($CI->parts as $name => $part) {
$view = str_replace("{" . $name . "}", $part, $view);
}
}
$view = str_replace("{metas}", $metas, $view);
$view = str_replace("{scripts}", $scripts, $view);
$view = str_replace("{styles}", $styles, $view);
$view = preg_replace("/{.*?}/ims", "", $view); // Подчищаем пустые неподгруженные части шаблона
} else {
$view = $output;
}
$OUT->_display($view);
}

}

?>

然后在/application/config/config.php 中启用 Hook :
$config['enable_hooks'] = TRUE;

之后添加到/application/config/hooks.php:
$hook['display_override'][] = array('class' => 'Layout',
'function' => 'render',
'filename' => 'Layout.php',
'filepath' => 'hooks'
);

创建文件夹/application/layouts/

创建文件/application/layouts/default.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>{title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
{metas}
{scripts}
{styles}
<link rel="icon" href="<?= base_url() ?>favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="<?= base_url() ?>favicon.ico" type="image/x-icon" />
</head>
<body>
<div class="head">
{head}
</div>
<div class="content">
{content}
</div>
</body>
</html>

从 Controller 使用它:
class main extends MY_Controller {

function __construct() {
parent::Controller();
$this->parts[head] = $this->load->view("frontend/global/header.php", null, true);
$this->scripts = array("JQuery/jquery-1.4.2.min", "JQuery/form", "Core", "Frontend");
$this->styles = array("style");
$this->title = "Blah blah";
}

function index() {
$this->load->view('frontend/index.php');
}


}

关于CodeIgniter 和布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2956836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com