将通用标题传递给所有视图和模型CodeIgniter [英] Passing common title to all view and models CodeIgniter

查看:75
本文介绍了将通用标题传递给所有视图和模型CodeIgniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个控制器

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

class Main extends CI_Controller {

     function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('text');

    }

    public function index()
    {
        $this->home();
    }

    public function home()
    {
            $data['title']="Somesite";
        $this->load->view("view_home", $data);

    }

        public function blog()
    {
            $data['title']="Somesite";
        $this->load->view("view_blog", $data);

    }
        public function answers()
    {
            $data['title']="Somesite";
        $this->load->view("view_answers", $data);

    }
    }

您可能会看到 $ data ['title'] 对于所有功能都是相同的,如何使其更简单,在开始处包括而不是在每个功能中写入,再次重复然后转移到视图。
是否可以将其传递给函数?

As you may see $data['title'] is same for all functions, how to make it simpler, to include at the beggining and not to write it in every function, repeat again, and then transfer to view. Is there a way to tranfer it to function?

推荐答案

在构造函数之前添加以下内容:

Before construct function add this:

public $data = array();

然后在构造函数中写入:

Then in the construct function write:

$this->data['title']="Somesite";

最后在加载视图之前添加以下内容:

And finally before load view add this:

$data = $this->data + $data;

现在您到处都有相同的 $ title

Now you have same $title everywhere.

这篇关于将通用标题传递给所有视图和模型CodeIgniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆