Welcome Guest, Not a member yet? Register   Sign In
Loading a view from a database entry and/or string
#1

[eluser]JayTee[/eluser]
OK, so I have an FAQ page. While a static view file works perfectly well for the page, it requires access to the view files to perform quick updates and/or additions/deletions; which is a pain if I'm not on my working PC.

Why not use a DB? -- great idea!

But wait... what if the FAQ has links and stuff in it that I want to use? What if the links are internal to the site and I want to use (gasp) a helper? What if I want to have "view stuff" in the db entry?

I cobbled this together (hacked the existing loader library) and did a quick test - seems to work for what I need.

application/libraries/MY_Loader.php:
Code:
<?php  if(!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Loader extends CI_Loader
{
    
  function string_view($view, $vars = array(), $return = FALSE)
  {
    return $this->_ci_load_string(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
  }

  function _ci_load_string($_ci_data)
  {
    // Set the default data variables
    foreach (array('_ci_view', '_ci_vars', '_ci_return') as $_ci_val)
    {
      $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
    }

    // This allows anything loaded using $this->load (views, files, etc.)
    // to become accessible from within the Controller and Model functions.
    // Only needed when running PHP 5
    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;
        }
      }
    }

    /*
     * Extract and cache variables
     *
     * You can either set variables using the dedicated $this->load_vars()
     * function or via the second parameter of this function. We'll merge
     * the two types and cache them so that views that are embedded within
     * other views can have access to these variables.
     */    
    if (is_array($_ci_vars))
    {
      $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
    }
    extract($this->_ci_cached_vars);
                
    /*
     * Buffer the output
     *
     * We buffer the output for two reasons:
     * 1. Speed. You get a significant speed boost.
     * 2. So that the final rendered template can be
     * post-processed by the output class.  Why do we
     * need post processing?  For one thing, in order to
     * show the elapsed page load time.  Unless we
     * can intercept the content right before it's sent to
     * the browser and then stop the timer it won't be accurate.
     */
    ob_start();
                
    // If the PHP installation does not support short tags we'll
    // do a little string replacement, changing the short tags
    // to standard PHP echo statements.
    
    if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
    {
      //the replace happens on the string rather than a view file
      echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', $_ci_view)));
    }
    else
    {
      echo eval('?>'.$_ci_view);
    }
    
    // Return the file data if requested
    if ($_ci_return === TRUE)
    {        
      $buffer = ob_get_contents();
      @ob_end_clean();
      return $buffer;
    }

    /*
     * Flush the buffer... or buff the flusher?
     *
     * In order to permit views to be nested within
     * other views, we need to flush the content back out whenever
     * we are beyond the first level of output buffering so that
     * it can be seen and included properly by the first included
     * template and any subsequent ones. Oy!
     *
     */    
    if (ob_get_level() > $this->_ci_ob_level + 1)
    {
      ob_end_flush();
    }
    else
    {
      // PHP 4 requires that we use a global
      global $OUT;
      $OUT->append_output(ob_get_contents());
      @ob_end_clean();
    }
  }
}

Here's the test:
application/controllers/home.php
Code:
<?php
class Home extends Controller
{
  function __construct()
  {
    parent::Controller();
  }

  function index()
  {
    $this->load->string_view('Hello, will you visit the <?php echo anchor("home/faq","faq"); ?>?','');
  }
  function dbtest()
  {
    $this->load->model("faq");
    $faq_list = $this->faq->get_faq();
    $this->load->view('faq',$faq_list);
  }
}

For the DB test:
application/views/faq.php
Code:
<dl>
&lt;?php foreach($faq_list as $faq): ?&gt;
<dt>&lt;?php echo $faq->question; ?&gt;</dt>
<dd>&lt;?php $this->load->string_view($faq->answer,''); ?&gt;</dd>
&lt;?php endforeach; ?&gt;

An example entry in the FAQ answer could be something like:
Code:
If none of the FAQ meets your needs, please &lt;?php echo anchor("home/contact","Contact Us"); ?&gt; and we'll try and help




Theme © iAndrew 2016 - Forum software by © MyBB