1

I'm new to working with CI and I have a question. More like a design method suggestion. I want to create a base template for static pages. Basically I want to load doc type, head, and the body but nothing else. I want the content to be loaded by whatever class/function I call via the URL. I know I can do this with a HTML template and str_replace() but is there a better way or some fancy CI method I'm not familiar with?

Here is what I have so far and it works but it's not the best.

class Sandbox extends CI_Controller {

    public function __construct() {
        parent::__construct();
        //echo "hello world?"; 
    }

    public function load($what){
        if ($what == 'something'){

            if (!file_exists('application/views/content/'.$what.'.php')){
                // Whoops, we don't have a page for that!
                show_404();
            }
            $data = array();
            $this->load->view('templates/header', $data);
            $this->load->view('content/'.$what, $data);
            $this->load->view('templates/footer', $data);
        }
        else { // Default load

            $data = array();
            $data['message'] = 'Hello World';
            //$this->load->view('templates/header', $data);
            $this->load->view('content/sandbox', $data);
            //$this->load->view('templates/footer', $data);
        }
    }
}

When I load the view method it seems to work but I had to add the doctype and head to the template. I would prefer to load those seperately so do not have to create multiple headers. What I am looking for is a way to specifically load parts of the page and whatever scripts/css that are required for that given page.

Thanks for any advice.

4

1 回答 1

0

您在控制器中的函数名称是对 url 的引用,例如:

www.ci.com/sandbox/load

上面的 url 将加载function load()到沙箱控制器中。

要创建模板,您只需创建一个包含所需基础知识的视图,然后将内容传递给该视图,例如:

function home(){
$data['content'] = $this->load->view('home_content',,TRUE);
this->load->view('template', $data)
}

通过TRUE在第三个参数中使用,您将返回数据而不是显示它。

然后在模板文件中,echo $content; 您还可以使用相同的方法创建单独的头文件和其他文件以包含在模板中,即:

function home(){
$data['content'] = $this->load->view('home_content',,TRUE);
$data['head'] = $this->load->view('head',,TRUE);
this->load->view('template', $data);
}

您还可以将数据变量传递给您要返回的视图,即:

$data['content'] = $this->load->view('home_content',,TRUE);
$data['admin_script'] = $this->load->view('admin_script',,TRUE);
$data['head'] = $this->load->view('head',$data,TRUE);

我希望这有帮助!:)

如果你想加载不同的页面,你应该为它们创建单独的函数并创建一个新的路由规则:Codeigniter: URI ROUTING

于 2013-07-08T16:46:14.943 回答