簡體   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