CodeIgniter Forums

Full Version: Can I use system variables or functions if using view parser
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've worked through the tutorial (https://www.codeigniter.com/userguide3/t...index.html) - very good BTW - and the decided to redo it using the parser library for all views/templates instead of php. Now while I've managed to get the examples to all work ok, I have some questions:

- The input form example wants to display validation errors using validation_errors() function. Cam this be called/shown is a view rendered via the parser library? Or do we have to collect any errors in a parser friendly format and inject them as 'data'?

- It would be good reference base_url in the for input view, instead of just hoping that relative paths work ok - is this possible?

Cheers
Mark
(04-24-2019, 09:50 PM)markir Wrote: [ -> ]- The input form example wants to display validation errors using validation_errors() function. Cam this be called/shown is a view rendered via the parser library? Or do we have to collect any errors in a parser friendly format and inject them as 'data'?

IMO, there is no problem using functions from the Form Helper, URL Helper, or well... any "Helper" in a view file - with or without using the template parser. For the most part, those functions are returning strings of HTML which really isn't much different from what some of the more advanced template engines offer. So, if you're wanting to use the following, do it.

PHP Code:
<?php echo form_error('username'); ?>


(04-24-2019, 09:50 PM)markir Wrote: [ -> ]- It would be good reference base_url in the for input view, instead of just hoping that relative paths work ok - is this possible?

If you are asking if it is OK to use base_url() in a view I say, Absolutely. That said, there is a bit of overhead in using that helper function. If you need to use that value multiple times It can be a lot more efficient to capture the return of base_url() in a variable instead of repeated calls to the function.
Thanks - I hadn't considered using <php? ?> in the to-be-parsed template files. While that would work, I guess I was really looking for a way to keep the template files looking 'templated' rather than 'coded', e.g: my news/create view looks like:

Code:
<form action="create" method="post" accept-charset="utf-8">
 
   <label for="title">Title</label>
   <input type="input" name="title" /><br />

   <label for="text">Text</label>
   <textarea name="text"></textarea><br />

   <input type="submit" name="submit" value="Create news item" />

</form>

I was hoping I'd be able to put in something like {form_error('username')} or {base_url()} which looks in keeping with a template
The parser included with CodeIgniter is quite limited. You can achieve your hope with a more full-featured template engine like Smarty, Twig, or dozens of others. They aren't as convienent as the CI parser, they require some work to integrate, but it isn't that hard. Many (all?) can be installed with Composer which helps.
(04-26-2019, 07:37 AM)dave friend Wrote: [ -> ]The parser included with CodeIgniter is quite limited. You can achieve your hope with a more full-featured template engine like Smarty, Twig, or dozens of others. They aren't as convienent as the CI parser, they require some work to integrate, but it isn't that hard. Many (all?) can be installed with Composer which helps.

Cheers, will check them out.
For completeness, I'll follow up with how I found adding a fuller featured template engine.

After a bit of reading, it seemed like Smarty was gonna suit me best, so grabbed that and its Codeigniter integration code (Smartie altho the way it is loaded in the autoloader makes the latter invisible). Overall pretty painless. Redoing the Codeigniter News tutorial, essentially the changes were:

Code:
$this->load->view('x', $data);
to:
$this->smarty->view('x.tpl', $data);

<php? echo $x ?> to {$x}
<php? echo $var['x'] ?> to {$var.x}
<php? foreach ... ?> to {foreach ...}

<php? echo base_url(); ?> to {base_url()}

So my news/create view then looked like:

Code:
<h2>{$title}</h2>
 
{validation_errors()}

<form method="post" action="{base_url()}news/create">

   <label for="title">Title</label>
   <input type="input" name="title" /><br />

   <label for="text">Text</label>
   <textarea name="text"></textarea><br />

   <input type="submit" name="submit" value="Create news item" />

</form>

So finally, thanks for you help and suggestions.

regards
Mark