Welcome Guest, Not a member yet? Register   Sign In
Default vars for parser->parse
#1

[eluser]woutr_be[/eluser]
I'm fairly new to CodeIgniter, but I'm just learning by doing.

I've overwritten the default CI_Controller in order to load views in my general template, like this:

Code:
class Guest_Controller extends CI_Controller
{  
private $body_classes = "guest ";

public function __construct()
{
  parent::__construct();
}

public function _output($content)
{
  // Load the base template with output content available as $content
  $data['content'] = &$content;  
  echo($this->load->view('templates/html_guest', $data, true));
}
}

I also want to use this to do some extra authentication checks, like some user are only allowed certain controllers.

Now when I use this Guest_Controller, like this:
Code:
class Homepage extends Guest_Controller {

public function __contruct()
{
}

public function index()
{
  $data = array(
   'title' => 'Homagepage',
   'page_title' => 'Test title',
   'body_classes' => 'guest home'
  );
  $this->parser->parse('homepage', $data);
}
}

I now want to add some default string to my "body_class", the one that is defined in my Guest_Controller.
Is there a way to override parser->parse in my Guest_Controller in orde to do so?

This doesn't have to be parser->parse, but I can also just use $this->load->view if it's easier that way.
#2

[eluser]Rowan Wilson[/eluser]
Are you templating? If not, there's no need to use parser, just stick to using Views. Otherwise the $data array in your Homepage controller will allow you to access those strings directly, e.g. $title, $page_title, $body_classes in your view.
#3

[eluser]woutr_be[/eluser]
[quote author="rowstu" date="1356099489"]Are you templating? If not, there's no need to use parser, just stick to using Views. Otherwise the $data array in your Homepage controller will allow you to access those strings directly, e.g. $title, $page_title, $body_classes in your view.[/quote]

I'm not templating, but then the same question remains, how can I add a default value to $body_classes, I prefer to do this inside my Guest_Controller
#4

[eluser]Rowan Wilson[/eluser]
Does your Homepage controller work extending Guest_Controller like that? To me it shouldn't.

But anyway to answer your question. Inside your Guest_Controller you'll need to make your body_classes variable public and not private, then it will be available to you as

$this->body_classes
#5

[eluser]woutr_be[/eluser]
Why this shouldn't work? I'm not that experienced with CI, but it works yes.

But if I make my body_classas public inside Guest_Controller, how will it automatically add the defaults ones to it?

Basically when I call $this->load->view, that inside my Guest_Controller, the $body_classes gets added to $data['body_classes']
#6

[eluser]Aken[/eluser]
Make your $body_classes property protected or public, otherwise nothing except Guest_Controller can access/modify it.

Then, add/modify it in your controllers to set necessary values. Personally, I would leave it in an array format so you don't have to worry about making sure there are spaces around each value.

Code:
// Guest_Controller:
protected $body_classes = array('guest'); // default value(s)

// Homepage extends Guest_Controller:

function index() {
    // Add "home" class
    $this->body_classes[] = 'home';
}
#7

[eluser]woutr_be[/eluser]
Thanks, just one more problem, since $body_classes is an array, should I just use implode() to turn it into a string in my 'templates/html_guest', or can I overwrite $this->load->view somehow to do it there first?

I would prefer the latter, but just checking what the best options is.

EDIT: I just noticed that $body_classes is not available within my 'html_guest.php'
#8

[eluser]Aken[/eluser]
You'd implode() and include it in whichever data array is appropriate -- either when parsing a template, or in the _output() method.
#9

[eluser]woutr_be[/eluser]
Ah, got it, that seems to work just fine, thanks!




Theme © iAndrew 2016 - Forum software by © MyBB