Welcome Guest, Not a member yet? Register   Sign In
Extended Parser functionality?
#1

[eluser]fireproofsocks[/eluser]
I like having a simple view, devoid of raw PHP, so I've been using the parser class. I'm new to CI, so I don't know if there are extensions to the parser's functionality? E.g. is there a way to track some basic variables when you've got a loop defined by {items} ... {/items} -- e.g. it'd be great to have a built-in variable that counted which item of the array you're on, e.g. {n}.

It'd also be nice to tap into view-level filters, e.g. force a particular value to go through a function in the view layer. All of this is possible in raw PHP, of course, but I'd like to stay away from that if possible.

Thanks.
#2

[eluser]Colin Williams[/eluser]
You might want to look into using something more robust like Smarty. I don't see the point, though. Eventually, even with smarty, you end up with something that is close enough to PHP that you might as well just use <?php and go on with life...
#3

[eluser]moodh[/eluser]
Even if it's "ugly" it might be warranted to keep the views clean, most of CI's templateclass can be sorted with a hack and a foreach before sending to the view:
Code:
<?php
foreach($q->result_array() as $k => $v) {
  $v['key'] = $k; // set the key as you asked in your first question
  $o[] = $v;
  
}
// above should be in a model, and $data['variables'] should be set by a model call, but whatever for now
$data['is_logged_in'] = array(); // trick to emulate if-block: if(false)
$data['is_not_logged_in'] = array(array()); // trick to emulate if-block: if(true)
$data['variables'] = $o;
$this->parser->parse('view.php', $data);
?>

which means you can use the following in the parsed view:
Code:
{variables}
  {key} is the iteration
  {whatever}
  {whatever2}
{/variables}

{is_logged_in}you're logged in, yey!{/is_logged_in}
{is_not_logged_in}please login!{/is_not_logged_in}

With variables, loops and simple if-statements most things can be solved in the model (or controller) but it's of course more resource heavy, but sometimes nice views is actually worth it =)

There's a few hacks to the parser class to allow multiple blocks of the same name (for example {is_logged_in} twice), and also some other various improvements, search abit and you'll find them!




Theme © iAndrew 2016 - Forum software by © MyBB