简体   繁体   中英

Passing variables between php pages

I'm new to the Kohana Framework. I have a problem - How can I pass the variable $title from Layout.php to Head.php?

In controller:

<?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');
    }
}

In view 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>

In view 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>

You could do something like this:

$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;

Note the $this->template->head->title = $title; you need to pass it along manually to the head view.

You can use set() or bind(). See example:

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

Ref: http://kohanaframework.org/3.3/guide/kohana/mvc/views

What you are looking for is set_global

http://docs.kohanaphp.com/core/view#set_global

It will allow you to set a variable for all your views to be able to use. You won't be passing it per say but it will still do what you want.

Example fix

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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