views:

133

answers:

2

using This Template Library

when i try and use the add_js() function it errors out. I add the regions $_scripts to the template header file but when i load the page it says undefined variable _scripts.

any thoughts?

changed <?= $_scripts ?> to <?= (isset($_scripts)) ? $_scripts : “”; ?>

and obviously i lose the error but the js file still isnt loading.

I did an echo at each step though the add_js() method and the output is correct. I also did an output of $this->js and they both had the corret output. So it gets through the get_js method fine and sets the class variable fine but i dont get anything added to the actual page.

A: 

What is your code?

turbod
my code is below. I am on two different computers and havent registered yet.
Karl
A: 

sorry different computer now.

here is the controller method:

function registrationForm(){
    $this->template->set_template('single');
    $this->template->write_view('header', 'templates/header_template');
    $this->template->write_view('footer', 'templates/footer_template');
    $this->template->write_view('center', 'user/registration_form');
    $this->template->add_js('js/jquery.min.js');
    $this->template->add_js('js/validate.jquery.js');
    $this->template->render();
}

here is the head section:

    <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="<?= base_url() ?>css/style.css" type="text/css" rel="stylesheet" />
<?= (isset($_scripts)) ? $_scripts : ""; ?>
<?= (isset($_styles)) ? $_styles : ""; ?>
</head>

and the add_js() method:

function add_js($script, $type = 'import', $defer = FALSE)
   {
      $success = TRUE;
      $js = NULL;

      $this->CI->load->helper('url');

      switch ($type)
      {
         case 'import':
            $filepath = base_url() . $script;
            $js = '<script type="text/javascript" src="'. $filepath .'"';
            if ($defer)
            {
               $js .= ' defer="defer"';
            }
            $js .= "></script>";
            break;

         case 'embed':
            $js = '<script type="text/javascript"';
            if ($defer)
            {
               $js .= ' defer="defer"';
            }
            $js .= ">";
            $js .= $script;
            $js .= '</script>';
            break;

         default:
            $success = FALSE;
            break;
      }

      // Add to js array if it doesn't already exist
      if ($js != NULL && !in_array($js, $this->js))
      {
         $this->js[] = $js;
         $this->write('_scripts', $js);
      }

      return $success;
   }

template.php config file

/*
|--------------------------------------------------------------------------
| Default Template Configuration (adjust this or create your own)
|--------------------------------------------------------------------------
*/

$template['default']['template'] = 'templates/default_template';
$template['default']['regions'] = array(
    'header',
    'left',
    'right',
    'footer',
);
$template['default']['parser'] = 'parser';
$template['default']['parser_method'] = 'parse';
$template['default']['parse_template'] = FALSE;

/*
|--------------------------------------------------------------------------
| Default Template Configuration (adjust this or create your own)
|--------------------------------------------------------------------------
*/
$template['single']['template'] = 'templates/single_template';
$template['single']['regions'] = array(
    'header',
    'center',
    'footer',
);
$template['single']['parser'] = 'parser';
$template['single']['parser_method'] = 'parse';
$template['single']['parse_template'] = FALSE;

/* End of file template.php */
/* Location: ./system/application/config/template.php */
Karl
I'm not sure if it matters, but why not have the header and footer areas defined in your "single" template? One of the reasons I latched onto this library was so I didn't have to add the header and footer views for each controller method. Can you post your template library config?
someoneinomaha
Actually it never occurred to me but i will do that now. just old habit i guess.the template library is autoloaded in the CI autoload config.keep in mind the above code is not changed with your suggestion yet. thanks for that.I have added the template config file above.
Karl
this has something to do with php and short tags.
Karl