我正在尝试使用模板方法开发一个网站 我已经创建了不同的文件并想运行一个查询,并且该查询将在所有模板上可用,是否可以这样做?就像我想给你一个例子,我在 Controller 中做了什么,我创建了一个这样的变量
$data['navigation'] = 'templates/main_menu';
$this->load->view('main');
好吧,现在我在 main View 中所做的是我在其中包含页眉和页脚,并像这样动态调用导航文件
$this->load->view($navigation);
好吧,现在我想做的是将图标存储在数据库和其他设置中,所以我在头文件中放置了一个查询
<?php $settings = $this->db->get_where('settings', array('id' => 2));\\ This query is just placed above the doctype in the header file and I would like to be called every where ?>
templates/main_menu.php
$settings->row()->header_bg_color
但我收到一个错误消息: undefined variable :设置,如果我将此设置查询放在 main_menu.php 文件中,它的工作方式就像应该的方式一样我的意思是再次调用查询没有意义再一次,如果我需要最好的方法是什么,模型的用途是什么,如果是的话,它会被用于查询吗,如果我把这个设置查询放在模型中,我将如何访问它是可能的请多多指教
谢谢
最佳答案
hi 下面是 Controller 的代码。我已经创建了 common_library.php 文件,我在其中编写了我想要使用所有 View 的所有常用函数。
class Maintenance extends CI_Controller {
function Maintenance() {
parent::__construct();
// file for all common function
include('application/controllers/common/common_library.php');
$this->load->model('maintenance_model');
$this->load->library('session');
$this->load->library('form_validation');
}
function index(){
/*
* @ desc : default function that loads the maintenance_view.
*
*
*/
$data['common_query'] = commonQuery() // define this function in common_library file
$data['model_data'] = $this->maintenance_model->projectdetail();
$data['formtitle'] = 'Maintenance List | BMS';
$this->load->view('maintenance_view',$data);
}
}
关于php - codeigniter 中的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36369925/