繁体   English   中英

在php页面之间传递变量

[英]Passing variables between php pages

我是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>

您可以执行以下操作:

$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; 您需要将其手动传递到主视图。

您可以使用set()或bind()。 参见示例:

$view = View::factory('user/roadtrip')
    ->set('places', array('Rome', 'Paris', 'London', 'New York', 'Tokyo'));
    ->bind('user', $this->user);

参考: http : //kohanaframework.org/3.3/guide/kohana/mvc/views

您正在寻找的是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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM