[eluser]Unknown[/eluser]
Hello, there. I'm new to CI and I have a good (in my view) question.
Why we just load views from files? I mean, if you take a look at some template systems, like Smarty, you can see that they let you load (or include) templates from a variety of sources (for example, from a database). In CodeIgniter we don't have it. Views (that can be considered as some kind of templates) are just stored and loaded from files.
I'm working on a small CMS that is inspired by Ruby's Radiant CMS. And it let the user create and edit layouts and snippets. Layouts are a little like views, but they're stored on the database. So, I've done the following adaptation, just cutting some parts from original CI_Loader class.
Code:
class MY_Loader extends CI_Loader
{
function _ci_load_buffer($_ci_data)
{
foreach (array('_ci_buffer', '_ci_vars', '_ci_return') as $_ci_val)
{
$$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
}
if ($this->_ci_is_instance())
{
$_ci_CI =& get_instance();
foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
{
if ( ! isset($this->$_ci_key))
{
$this->$_ci_key =& $_ci_CI->$_ci_key;
}
}
}
if (is_array($_ci_vars))
{
$this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
}
extract($this->_ci_cached_vars);
ob_start();
if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
{
echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', $_ci_buffer)));
}
else
{
echo eval('?>'.$_ci_buffer);
}
log_message('debug', 'Buffer loaded: \''.substr($_ci_buffer, 0, 32).'...\'');
if ($_ci_return === TRUE)
{
$buffer = ob_get_contents();
@ob_end_clean();
return $buffer;
}
if (ob_get_level() > $this->_ci_ob_level + 1)
{
ob_end_flush();
}
else
{
global $OUT;
$OUT->append_output(ob_get_contents());
@ob_end_clean();
}
}
}
That function works like _ci_load one, but instead of loading from a file, loads from a string buffer. Now I can retrieve a layout from a database and treat and load that as a normal view.
As I'm new to CI, I wonder if that was a good solution and I ask you guys, that are more experienced.
Thank you,
Dirley Rodrigues