Welcome Guest, Not a member yet? Register   Sign In
Template Library Version 1.4.1

[eluser]JoeWS[/eluser]
EDIT: Solved
I am new to using hooks and didn't realize I needed this one important line:
Code:
$CI->output->_display();
Oops! Sorry if anyone read all the way through my post Smile

---

I am having some issues with using the display_override hook. I get an error: "Cannot render the 'header' region. The region is undefined." when I try to set the header region.

Here is what I am trying to accomplish:
Whatever gets output by my application will get buffered and then sent to the 'content' region using Template Library 1.4.1. If the 'header' region is empty, I use the default header view for the 'header' region. If the 'footer' region is empty, I use the default footer view for the 'footer' region.

My main files are listed below. The application is basically going to be an interactive site. The application name has been obfuscated.


application/config/config.php
Code:
$config['enable_hooks'] = TRUE;

application/config/hooks.php
Code:
$hook['display_override'] = array(
                                'class'    => 'Display',
                                'function'    => 'renderTemplate',
                                'filename'    => 'Display.php',
                                'filepath'    => 'hooks',
                            );

application/config/template.php
Code:
$template['active_template'] = 'default';

$template['default']['template'] = 'index';
$template['default']['regions'] = array(
   'title',
   'header',
   'content',
   'footer',
   'sidebar_right',
   'sidebar_left',
);
$template['default']['parser'] = 'parser';
$template['default']['parser_method'] = 'parse';
$template['default']['parse_template'] = FALSE;

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

class Display
{
    var $CI                = null;
    
    function Display()
    {
        $this->CI =& get_instance();
    }
    
    function renderTemplate()
    {
        $this->CI->load->library('template');
        
        $template =& $this->CI->template;
        
        // Check if we need to use a template other than the default
        if ( $this->CI->input->get('feed', FALSE) ) {
            // Use the RSS Feed template
            $template->set_template('rss');
        }
        
        // Add Overall Stylesheet
        $template->add_css('media/css/myapp.css');
        
        // Add JQuery from Google CDN
        $template->add_js('http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js');
        
        // Add Overall JavaScript
        $template->add_js('media/js/myapp.js');
        
        
        
        // Get Region Data
        $this->getHeader($template);
        
        $this->getContent($template);
        
        $this->getSidebars($template);
        
        $this->getFooter($template);
        
        // Render the template
        $template->render();
    }
    
    /**
     * Get the page header
     */
    private function getHeader( &$template )
    {
        $header_data = $template->render('header', true);
        
        if ( ! is_string($header_data) || strlen(trim($header_data)) == 0 ) {
            $overwrite = FALSE;
            $data      = NULL;
            
            // Use Default Header
            $template->write_view('header', 'default_header', $data, $overwrite);
        }
    }
    
    /**
     * Get the main content
     */
    private function getContent( &$template )
    {
        $content  =  $this->CI->output->get_output();
        
        $template->write('content', $content);
    }
    
    /**
     * Get the left and/or right sidebars
     */
    private function getSidebars( &$template )
    {
        // TODO: Get Left and Right Sidebar
    }
    
    /**
     * Get the page footer
     */
    private function getFooter( &$template )
    {
        $footer_data = $template->render('footer', true);
        
        if ( ! is_string($footer_data) || strlen(trim($footer_data)) == 0 ) {
            $overwrite = FALSE;
            $data      = NULL;
            
            // Use Default Footer
            $template->write_view('footer', 'default_footer', $data, $overwrite);
        }
    }
    
}


application/views/default_header.php
Code:
<h1>My App</h1>

application/views/default_footer.php
Code:
<p>&copy;&nbsp;2011 My App</p>

[eluser]JoeWS[/eluser]
Side note: I did edit the constructor in application/libraries/Template.php

from
Code:
function CI_Template()
   {
      // Copy an instance of CI so we can use the entire framework.
      $this->CI =& get_instance();
      
      // Load the template config file and setup our master template and regions
      include(APPPATH.'config/template'.EXT);
      
      if (isset($template))
      {
         $this->config = $template;
         $this->set_template($template['active_template']);
      }
   }

to
Code:
function CI_Template()
   {
      // Copy an instance of CI so we can use the entire framework.
      $this->CI =& get_instance();
      
      // Load the template config file and setup our master template and regions
      require_once APPPATH.'config/template'.EXT;
      
      if (isset($template))
      {
         $this->config = $template;
         $this->set_template($template['active_template']);
      }
   }

because I was not getting any output to the browser at all.


And the index file, in case you need to see it:


application/views/index.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;!--[if lt IE 7 ]> &lt;html class="no-js ie6" lang="en"&gt; <![endif]--&gt;
&lt;!--[if IE 7 ]>    &lt;html class="no-js ie7" lang="en"&gt; <![endif]--&gt;
&lt;!--[if IE 8 ]>    &lt;html class="no-js ie8" lang="en"&gt; <![endif]--&gt;
&lt;!--[if (gte IE 9)|!(IE)]>&lt;!--&gt; &lt;html class="no-js" &gt; &lt;!--<![endif]--&gt;
&lt;head&gt;
    &lt;meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"&gt;
    &lt;meta http-equiv="content-type" content="text/html;charset=UTF-8"&gt;
    &lt;meta name="description" content="My App"&gt;
    &lt;meta name="author" content="WebSpark Inc"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
    
    &lt;!--    Icons    --
    &lt;link rel="shortcut icon" href="/favicon.ico"&gt;
    &lt;link rel="apple-touch-icon" href="/apple-touch-icon.png"&gt;
    --&gt;
    
    &lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;
    
    &lt;!--    Include style sheets and javascript        --&gt;
    &lt;?php
    echo $_scripts;
    echo $_styles;
    ?&gt;
    
&lt;/head&gt;
&lt;body&gt;
    &lt;!--    Page Wrapper    --&gt;
    <div id="wrapper">
        
        &lt;!--    Display Header    --&gt;
        &lt;?php if ( strlen($header) > 0 ) { ?&gt;
        <div id="header">
            &lt;?php echo $header; ?&gt;
        </div>
        &lt;?php } ?&gt;
        
        &lt;!--    Main Section    --&gt;
        <div id="main">
            
            &lt;!--    Display Right Sidebar    --&gt;
            &lt;?php if ( strlen($sidebar_right) > 0 ) { ?&gt;
            <div id="sidebar-right">
                &lt;?php echo $sidebar_right; ?&gt;
            </div>
            &lt;?php } ?&gt;
            
            &lt;!--    Display Main Content    --&gt;
            <div id="content">
                &lt;?php echo $content; ?&gt;
            </div>
            
            &lt;!--    Display Left Sidebar    --&gt;
            &lt;?php if ( strlen($sidebar_left) > 0 ) { ?&gt;
            <div id="sidebar-left">
                &lt;?php echo $sidebar_left; ?&gt;
            </div>
            &lt;?php } ?&gt;
            
        </div>
        
        &lt;!--    Display Footer    --&gt;
        &lt;?php if ( strlen($footer) > 0 ) { ?&gt;
        <div id="footer">
            &lt;?php echo $footer; ?&gt;
        </div>
        &lt;?php } ?&gt;
        
    </div>
&lt;/body&gt;
&lt;/html&gt;

[eluser]JoeWS[/eluser]
After stepping through the code a little more today, I think the problem may lie in me not understanding the display_override hook in CI rather than any issue with the Template library.

It looks like my change to the constructor in CI_Template was causing the region error. I put that back to an include and now the error is gone but I still do not get any output to the page. If I use my template without the hook then everything works swimmingly.

[eluser]SpYk3[/eluser]
Question about "add_js"
libraries/Template.php
line 443

Wouldn't it make more since to change the operation in case someone is using their own method for creating js file location strings? For instance, I have an url_helper extension in almost all my proggies that includes direct links to my js, css and media folders and files with simple calls like "jsplugs_url('layout')"

Somthing like
Code:
$filepath = (strpos($script, 'buff'.base_url()) ? $script : base_url().$script);

or possibly even better, checking for possibly outside js files added in
Code:
$filepath = (strpos($script, "buffhttp://") ? $script : base_url().$script);
*'buff' added to remove possiblity of a 0 return which can be misread as false on getting var*


???

Just an idea.
I'm still new to this thing, just dl'd it today so maybe i'll find better reasons not too

[eluser]Colin Williams[/eluser]
Template is a library, not a controller. It has no URI callback. To understand libraries better, visit http://ellislab.com/codeigniter/user-gui...aries.html

[eluser]greenflash[/eluser]
Fantastic library, very natural with CI, as it always should be with a plugin Smile
I was amazed that I could keep lines like the one below in my views/template.php and it still all would work fine.

Code:
&lt;?php $this->load->view('layout\header');?&gt;

So I implemented in 3 minutes. Great. One thing, I guess it's just syntactic sugar Smile but is it possible to make some changes to make method chaining possible?

For example if write_view returns $this, I can do something like the stuff below:

Code:
// Prepare template, filling up region "content" with people_view + data & render
$this->template->write_view('content', 'people_view',$data)
               ->render();

[eluser]staJr[/eluser]
Dear Colin Williams,
Thanx you so much for your library ! It does a really good work Smile
A little question :

Using your add_js & add_css methods, how can I echo $_styles & $_scripts NOT in a template file BUT within a view file I will load in a region (after) using the write_view method.

I tried to put both add_js() & add_css() in a $data array used as a parameter by write_view() but it only output '1' (witch is expected).

Thanxs for your help.

Jean.

[eluser]Dani[/eluser]
Hi, Im new to both CI and template. I have created views/template.php and it works fine, but the file is starting to get rather big. What is the best practice to f.ex. put the menu in a separate file ? Should I use
&lt;?=include('menu.php')?&gt; or &lt;?=$this->load->view('menu.php')?&gt; in the views/template.php file, or is there a better practice ?

[eluser]Mauricio de Abreu Antunes[/eluser]
Is this lib viable yet?

[eluser]Unknown[/eluser]
This is the great library. I use it in all my projects.




Theme © iAndrew 2016 - Forum software by © MyBB