I'm new to CI and would like to make loading of views, navigation & meta data done in an efficient way. I've just done the homepage and came up with the following:
class Home extends Controller {
function index()
{
// specify extra bits such as page title, section, extra js to load
$extra = array(
'title' => 'Funky Boots Co Home',
'开发者_Go百科section' => $this->uri->segment(1),
'js' => 'home,cycle',
'base_url'=> base_url()
);
$this->template->load('public/templates/main', 'home_view', array('extra'=>$extra));
}
}
- I'm using a basic template library so I can save repeatedly loading views
- Since I use base_url all over the place, I thought'd be better to set it as a var once
- I've got shared js files which are always include but use the 'js' value to pass the name of any js scripts needed for this specific page.
- section is passed to the view for setting body class, and through to the nav view for link highlighting
Questions
- Firstly is there anything wrong with this?
- Rather than setting all of these extra array vars for each function, I feel I should set 'section' & base_url automatically once. What's the neatest way to do that? a base controller?
There's nothing wrong with the way you're handling it, but I will offer my opinion:
Set some defaults instead of redefining those variables for every time you load a template. You could use a base controller, but since we only need these for the Template library it's better to set them in either the class itself, or in a config file that is loaded by the template, or both. When you load a specific template, you can overwrite those variables or fall back to the defaults.
Is your Template base_url
going to change between pages? If the answer is No, you don't need to set it as a variable, just call base_url()
when needed. I personally set a globally available constant called BASE_URL
for this (in my config file), but that is just preference. If it might change (but rarely), then this is a great example of why setting some default values is going to be useful.
You could probably just pass $extra
to the third parameter rather than array('extra' => $extra)
. You should be ale to stuff the array with whatever values you need, but of course this depends on what the class actually does. I have the feeling this will be fine, and save you some effort.
I would definitely use an array for the js
rather than splitting a string by commas. You'll fund this useful eventually, and it will be more readable. For instance, you will be able to add files like this:
if ($some_condition) $extra['js'] = 'accordion.js'; // add file
if ($some_other_condition) unset($extra['js']['accordion.js']); // remove file
Whereas with your current method, you will be messing with strings and commas. You might consider writing a function in your Template class called add_js()
or something. That's most likely what I would do. Actually, I am doing that. I highly recommend it. An example might look like this:
$this->template
->add_js('accordion.js') // add file
->remove_js('accordion.js'); // remove file
Or maybe even this:
$this->template->js[] = 'accordion.js';
Then in your template file, just load the files in $this->template->js
. I prefer the first method, using a function. I would prefer to use the .js extension as well, as I have a few .php files and/or CI views to load that are actually javascript files, but you may not need this. IMO it makes it clearer what is going on and removes any ambiguity.
In the end, do what works for you. You aren't breaking any rules here or doing anything bad. Personally, my template library is a bit of a mess but it's been serving me very well for years, and I constantly improve it. Consider writing more functions to handle setting sections, views, javascript and so on. I assume this is heavily used, so make it as easy as you can to work with in your controller. There is always time to refactor later.
精彩评论