我是Kohana框架的新手。我有一个问题 - 如何将变量$title
从Layout.php传递给Head.php?
在控制器中:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Admin_Quanly extends Controller_Template {
public $template='admin/layout';
function _showWithTemplate($subview,$title)
{
$admin_path = 'admin/';
$this->template->head = View::Factory(''.$admin_path.'head');
$this->template->subview = View::Factory(''.$admin_path.''.$subview.'');
$this->template->title = $title;
}
public function action_index()
{
$this->_showWithTemplate('subview/home','Trang quản trị hệ thống');
}
}
在视图Layout.php中:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php echo $head?>
</head>
<body>
</body>
</html>
在视图中Head.php:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?=$title?></title>
<base href="<?=URL::base()?>">
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript" src="javascript/jquery.min.js"></script>
<script type="text/javascript" src="javascript/ddaccordion.js"></script>
答案 0 :(得分:1)
你可以这样做:
$admin_path = 'admin/';
$this->template->head = View::Factory(''.$admin_path.'head');
$this->template->head->title = $title;
$this->template->subview = View::Factory(''.$admin_path.''.$subview.'');
$this->template->title = $title;
请注意$this->template->head->title = $title;
您需要手动将其传递到头部视图。
答案 1 :(得分:0)
您可以使用set()或bind()。见例:
$view = View::factory('user/roadtrip')
->set('places', array('Rome', 'Paris', 'London', 'New York', 'Tokyo'));
->bind('user', $this->user);
答案 2 :(得分:0)
您正在寻找的是set_global
http://docs.kohanaphp.com/core/view#set_global
它允许您为所有视图设置一个变量,以便能够使用。你不会按照说法传递它,但它仍然可以做你想要的。
示例修复
function _showWithTemplate($subview,$title)
{
$admin_path = 'admin/';
$this->template->head = View::Factory(''.$admin_path.'head');
$this->template->subview = View::Factory(''.$admin_path.''.$subview.'');
$this->template->set_global('title', $title);
}