使用模板系统时的Codeigniter性能

时间:2022-09-17 21:51:46

I'm still rather new to using CI but I've been playing around with building a modular template system for it and I think I've got the basic's of it down. My question is that the load times seem to be noticable when I use my template system. I was curious if this is common or if perhaps I'm doing something wrong. I know there are many variables with page load times and my site lives on a VPS so it could also just be that whoever I'm sharing resources with is a sucking them up. Could someone who is well versed in CI look over my code and see if there is anything I could do to speed up load times without sacrificing the modular structure?

我仍然是使用CI的新手,但我一直在为它构建一个模块化模板系统,我想我已经掌握了它的基本知识。我的问题是,当我使用我的模板系统时,加载时间似乎是显而易见的。如果这很常见或者我做错了,我很好奇。我知道页面加载时间有很多变量,而我的网站都存在于VPS上,所以它也可能只是我与之共享资源的人就是在吸引他们。精通CI的人能不能查看我的代码,看看我能做些什么来加快加载时间而不牺牲模块化结构?

Thanks

routes.php

 $route['default_controller'] = 'pages/load/home';
 $route['test/(:any)'] = 'sandbox/load/$1';
 $route['(:any)'] = 'pages/load/$1';
 $route['404_override'] = '';

pages.php

class Pages extends CI_Controller {

public function __construct() {
    parent::__construct();
    $this->load->model('menulinks_model');
}

public function load($what = 'home'){// Default load
    $array = array();
    //Make sure content exists
    if (!file_exists('application/views/content/'.$what.'.php')){
        // Whoops, we don't have a page for that!
        $page_content = "The page you are looking for does not exist (ERROR 404)";
    }else {
        //Get menu data
        $array['menulinks'] = $this->menulinks_model->getAllMenuLinks();

        //Get page content
        $page_content = $this->load->view('content/'.$what,$array,TRUE);
    }

    //Build Page
    $data = array();
    $data['head'] = $this->load->view('templates/headplate',$array,TRUE);
    $data['scripts'] = $this->load->view('templates/scriptplate',$array,TRUE);
    $data['content'] = $page_content;
    $data['footer'] = $this->load->view('templates/footplate',$array,TRUE);

    //Render page
    $this->load->view('templates/siteplate', $data);
}

1 个解决方案

#1


0  

The way you are building your template system i think is not the best way. try to use codeigniter helper for creating template systems it is better and optimized. for example create a helper by the name of template_helper in your codeigniter helper and insid that helper create functions for: hearder template, left template, right template content template, footer template.

你认为构建模板系统的方式不是最好的方法。尝试使用codeigniter helper来创建模板系统,它是更好和优化的。例如,在您的codeigniter帮助器中通过template_helper的名称创建一个帮助器,并确保帮助器为以下内容创建函数:听众模板,左模板,右模板内容模板,页脚模板。

example:

if(!function_existe('header_template'))
{
    //you can pass as many parameters as you want or without parameters.
    function header_template($var1,$var2)
    {
         $data = array();
         $data['var1'] = $var1;
         $data['var2'] = $var2;
         //your header view
         $this->laod->view('template/header',$data);
    }
}

and insid your controller at constructor load your template helper and than call these functions. header_template, footer_template etc... i hope this will solve your problem

并且你的控制器在构造函数中加载你的模板助手,而不是调用这些函数。 header_template,footer_template等...我希望这能解决你的问题

#1


0  

The way you are building your template system i think is not the best way. try to use codeigniter helper for creating template systems it is better and optimized. for example create a helper by the name of template_helper in your codeigniter helper and insid that helper create functions for: hearder template, left template, right template content template, footer template.

你认为构建模板系统的方式不是最好的方法。尝试使用codeigniter helper来创建模板系统,它是更好和优化的。例如,在您的codeigniter帮助器中通过template_helper的名称创建一个帮助器,并确保帮助器为以下内容创建函数:听众模板,左模板,右模板内容模板,页脚模板。

example:

if(!function_existe('header_template'))
{
    //you can pass as many parameters as you want or without parameters.
    function header_template($var1,$var2)
    {
         $data = array();
         $data['var1'] = $var1;
         $data['var2'] = $var2;
         //your header view
         $this->laod->view('template/header',$data);
    }
}

and insid your controller at constructor load your template helper and than call these functions. header_template, footer_template etc... i hope this will solve your problem

并且你的控制器在构造函数中加载你的模板助手,而不是调用这些函数。 header_template,footer_template等...我希望这能解决你的问题