dpge74512 2014-02-14 20:50
浏览 26

CodeIgniter:显示动态标题问题

Can someone help me out with this piece of code.

I have a template set up like this:

system/core/Loader.php :

public function template($view, $vars = array(), $return = FALSE)
    {
        $template  = $this->view('includes/header', array(), $return);
        $template  = $this->view('includes/navigation', array(), $return);
        $template .= $this->view($view, $vars, $return);
        $template .= $this->view('includes/footer', array(), $return);

        if ($return)
        {
            return $template;
        }
    }

It works perfect but when I try this to display a dynamic title, it doesn't want to display the title. I guess because the data is passed to content_home rather then to includes/header:

my controller:

public function home() {
        $this->load->model("model_get");
        $data["page_title"] = "Home Page";
        $data["results"] = ($this->model_get->getData("home"));
        $this->load->template("content_home",$data);
    }

includes/header.php

<title><?php echo (isset($page_title)) ? $page_title : 'Default title text'; ?> </title>

Any ideas how to work it out ??

Thanks : )

  • 写回答

6条回答 默认 最新

  • douguai4653 2014-02-14 21:52
    关注

    In your views folder make a template file like this,
    we'll call it template_web.php

    <?php 
    
    // the preset view files for your template
    $this->load->view('includes/header');     
    $this->load->view('includes/navigation'); 
    
    // in this example we will have our content view files in a folder called 'web' 
    $templatefolder = 'web/';
    
    // this is where you can pass in 'content' view files that are unique for the page 
    // this example has 3 placeholders you can put as many as you need 
    
    if(isset($content01))
    $this->load->view($templatefolder.$content01);
    
    if(isset($content02))
    $this->load->view($templatefolder.$content02);
    
    if(isset($content03))
    $this->load->view($templatefolder.$content03);
    
     // preset template footer file 
     $this->load->view('includes/footer'); 
    

    ok now in your controller methods

    // data you want to pass to your header  
    $data['page_title'] = 'Awesome Home Page';
    
    // other data
    $data['bigbagofdata'] = $this->bigBagOf->awesomeData ; 
    
    // the name of the view files 
    $data['content01'] = 'homepage';
    $data['content02'] = 'homepage_banner'; 
    
    // your template 
    $this->load->view( 'template_web', $data );
    

    data can be passed to any of the view files. and its very easy to set up different templates this way.

    评论

报告相同问题?

悬赏问题

  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件