Welcome Guest, Not a member yet? Register   Sign In
Best practices -> where to put template arrays
#1

[eluser]eveiga[/eluser]
Hi there,

Right now i'm using calendaring class and table helper in a simple site i'm developing. My question is, regarding developing best practices, where would I put the templates that I have created to the styling of both?

At the moment, i'm using the controllers constructor because I use both templates in 90% of the functions!

Best regards,
Edgar
#2

[eluser]bretticus[/eluser]
I would assume to follow CI convention, you're store them in either the config.php file or your own and autoload it.

Details:
http://ellislab.com/codeigniter/user-gui...onfig.html
#3

[eluser]JayTee[/eluser]
I just noticed something today (probably not "news" to everyone - but it's news to me)

If you put a file in your application/config folder with the SAME NAME as your library, CI will load that config file auto-magically Smile

All you gotta do for loading your template (for the calendar) is put it into application/config/calendar.php

I'll use the example [url="http://ellislab.com/codeigniter/user-guide/libraries/calendar.html"]from the documentation[/url] and make it so that it will work with this technique:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config = array( 'template' => '

   {table_open}<table border="0" cellpadding="0" cellspacing="0">{/table_open}

   {heading_row_start}<tr>{/heading_row_start}

   {heading_previous_cell}<th><a href="{previous_url}">&lt;&lt;</a></th>{/heading_previous_cell}
   {heading_title_cell}<th colspan="{colspan}">{heading}</th>{/heading_title_cell}
   {heading_next_cell}<th><a href="{next_url}">&gt;&gt;</a></th>{/heading_next_cell}

   {heading_row_end}</tr>{/heading_row_end}

   {week_row_start}<tr>{/week_row_start}
   {week_day_cell}<td>{week_day}</td>{/week_day_cell}
   {week_row_end}</tr>{/week_row_end}

   {cal_row_start}<tr>{/cal_row_start}
   {cal_cell_start}<td>{/cal_cell_start}

   {cal_cell_content}<a href="{content}">{day}</a>{/cal_cell_content}
   {cal_cell_content_today}<div class="highlight"><a href="{content}">{day}</a></div>{/cal_cell_content_today}

   {cal_cell_no_content}{day}{/cal_cell_no_content}
   {cal_cell_no_content_today}<div class="highlight">{day}</div>{/cal_cell_no_content_today}

   {cal_cell_blank}&nbsp;{/cal_cell_blank}

   {cal_cell_end}</td>{/cal_cell_end}
   {cal_row_end}</tr>{/cal_row_end}

   {table_close}</table>{/table_close}
');
The biggest difference here is that instead of creating that "prefs['template']" variable suggested in the calendar plugin documentation, you create a $config variable in the calendar.php file; which is an array with a 'template' item.

You can even set your other preferences such as local_time, start_day, etc (see the documentation) and they will get set at load time.

The "magic" happens in CI's Loader.php file (take a look around line 860).

Without going over the code, here's what happens: When you call the "load" method
Code:
$this->load->library('libname')
CI will try to load a file from your config folder with the same name as the library. If the file exists AND it has a variable called "$config", your library class will get loaded with the $config array passed to the constructor.

You can use the calendar's initialize method to pass an additional config array if needed - it won't over-write your existing settings.

Pretty kewl :coolsmile:
#4

[eluser]JayTee[/eluser]
Just for fun and games - I decided to extend the Calendar class (just a little bit) to make it a little "cleaner" than the big-long-string that the documentation suggests.

application/config/calendar.php:
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config = array('template' => array(
    'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
    'heading_row_start' => '<tr>',
    'heading_previous_cell' => '<th><a href="{previous_url}">&lt;&lt;</a></th>',
    'heading_title_cell' => '<th colspan="{colspan}">{heading}</th>',
    'heading_next_cell' => '<th><a href="{next_url}">&gt;&gt;</a></th>',
    'heading_row_end' => '</tr>',
    'week_row_start' => '<tr>',
    'week_day_cell' => '<td>{week_day}</td>',
    'week_row_end' => '</tr>',
    'cal_row_start' => '<tr>',
    'cal_cell_start' => '<td>',
    'cal_cell_start_today' => '<td>',
    'cal_cell_content' => '<a href="{content}">{day}</a>',
    'cal_cell_content_today' => '<a href="{content}"><strong>{day}</strong></a>',
    'cal_cell_no_content' => '{day}',
    'cal_cell_no_content_today' => '<strong>{day}</strong>',
    'cal_cell_blank' => '&nbsp;',
    'cal_cell_end' => '</td>',
    'cal_cell_end_today' => '</td>',
    'cal_row_end' => '</tr>',
    'table_close' => '</table>'
    )
);
I like the array approach - I think it's more readable (at least with my IDE's code highlighting).

Replace the Calendar Libraries "parse_template" method: (application/libraries/MY_Calendar.php):
Code:
&lt;?php    if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Calendar extends CI_Calendar
{
    function parse_template()
    {
        $this->temp = $this->default_template();
        if ($this->template != '' && count($this->template) > 0)
        {
          $this->temp = $this->template;
        }
    }
}

voila - you can now use the array-based template instead of the other one.

--

Edit:

If you wanted to just over-ride individual template items - you could change the MY_Calendar class so that it iterates over your template array and only replaces the keys in the $this->temp variable that the Calendar class uses... just a thought.




Theme © iAndrew 2016 - Forum software by © MyBB