gpt4 book ai didi

php - CodeIgniter,无法调用库类 "Template"的函数

转载 作者:行者123 更新时间:2023-12-04 10:54:14 27 4
gpt4 key购买 nike

我正在使用 codeigniter 框架。
我的问题是我不能调用库类文件的单个函数。这是代码:

这是名为模板的库文件:

<?php      
class Template
{
var $template_data = array();
public function __construct() {
$this-> CI =& get_instance();
}

public function load($template, $view, $view_data = array()){
$this->set('content', $this->CI->load->view($view, $view_data, TRUE));
return $this->CI->load->view($template, $this->template_data);
}
}

function hallo() {
echo "Hallo";
}

function set($key, $value) {
$template_data[$key] = $value;
}

?>

这是名为“页面”的 Controller :
<?php

class Pages extends CI_Controller {

function view($page) {

if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php')) {
// Whoops, we don't have a page for that!
show_404();
}

$data['title'] = ucfirst($page);

$this->load->library('template');

$this->template->set('hallo', 'My Navigation');
$template->set('adress', 'My Adress');
$template->set('tel', '007');
$template->load('basic_template', 'Pages/'. $page, $data);
$template->hallo();

}

}

?>

如您所见,我正在加载库目录的模板。然后我想使用“set”方法和“hallo”方法,但似乎不起作用。这是错误消息:
An uncaught Exception was encountered
Type: Error

Message: Call to undefined method Template::set()

Filename: \CodeIgniter\application\controllers\Pages.php

Line Number: 16

Backtrace:

File: \CodeIgniter\index.php
Line: 308
Function: require_once

最佳答案

新模板文件是
https://drive.google.com/file/d/1zoGwFZPrusejW_V_mS_2oSyDw-gHRAPc/view?usp=sharing

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

class Template

{

var $ci;
function __construct()
{

$this->ci = &get_instance();

}

function load($tpl_view, $body_view = null, $data = null)
{
if ( ! is_null( $body_view ) )

{
if ( file_exists( APPPATH.'views/'.$tpl_view.'/'.$body_view ) )
{
$body_view_path = $tpl_view.'/'.$body_view;
}
else if ( file_exists( APPPATH.'views/'.$tpl_view.'/'.$body_view.'.php' ) )
{
$body_view_path = $tpl_view.'/'.$body_view.'.php';
}

else if ( file_exists( APPPATH.'views/'.$body_view ) )

{
$body_view_path = $body_view;
}

else if ( file_exists( APPPATH.'views/'.$body_view.'.php' ) )

{
$body_view_path = $body_view.'.php';
}

else

{
show_error('Unable to load the requested file: ' . $tpl_name.'/'.$view_name.'.php');
}



$body = $this->ci->load->view($body_view_path, $data, TRUE);

if ( is_null($data) )

{
$data = array('body' => $body);
}
else if ( is_array($data) )

{
$data['body'] = $body;
}

else if ( is_object($data) )

{
$data->body = $body;
}

}
$this->ci->load->view('templates/'.$tpl_view, $data);
}
}?>

关于php - CodeIgniter,无法调用库类 "Template"的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59308232/

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