简体   繁体   English

codeigniter中的模板

[英]Template in codeigniter

I am trying to develop a website using a templating method I have created different files and would like to run a query and that query would be available on all templates is it possible to do that? 我正在尝试使用模板方法开发一个网站我创建了不同的文件,并希望运行一个查询,并且该查询可用于所有模板是否可以这样做? Like I would like to give you an example what I am doing here in the controller I have created a variable like this 就像我想给你一个例子,我在控制器中做了什么,我创建了一个像这样的变量

$data['navigation'] = 'templates/main_menu';
$this->load->view('main');

Okay so now here what I did in the view of main is I included header and footer there and called the navigation file dynamically like this 好的,现在我在主视图中所做的是我在那里包含页眉和页脚,并像这样动态调用导航文件

$this->load->view($navigation);

Okay so now What I am trying to do is want to get an icons stored in database and other settings as well so I placed a query in the header file 好的,现在我想要做的是想要在数据库和其他设置中存储图标,所以我在头文件中放置了一个查询

<?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

But I am getting an error as Message: Undefined variable: settings and if i place this settings query within the main_menu.php file it works so like what should be the way i mean it does not make sense to call the query again and again if I would be requiring what will be the best way to do and what is tthe use of model would it be used for the queries if it is then if I place this settings query within the model how would I access it is it possible please advise me 但我得到一个错误消息:未定义的变量:设置,如果我将此设置查询放在main_menu.php文件中,它的工作原理应该是这样的,我的意思是一次又一次地调用查询是没有意义的我会要求什么是最好的方法,如果我将这个设置查询放在模型中,我将如何使用模型将它用于查询是否可以访问它是否有可能请告诉我

Thank you 谢谢

hi below is code of controller. 嗨,下面是控制器的代码。 i have created the common_library.php file in which i have written all common function that I want to use all views. 我创建了common_library.php文件,在其中我编写了要使用所有视图的所有常用函数。

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

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

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