CodeIgniter Forums
Parser and if statement - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Parser and if statement (/showthread.php?tid=64972)

Pages: 1 2


Parser and if statement - sasatozovic - 04-15-2016

Hi,

I use parser for sending data from controller into view, but don't know how to now make if statement in view.

If I have for example view like this:


Code:
<html>
<head>
<title>{blog_title}</title>
</head>
<body>

<h3>{blog_heading}</h3>

{blog_entries}

<h5>{title}</h5>

<p>{body}</p>

{/blog_entries}
</body>
</html>


How to write if statement for title? I wanna to check if title for example "Michael" to write in view "Michael is the best", and for all other records to write |Welcome".

Any help?


RE: Parser and if statement - gxgpet - 04-15-2016

Hello! 

Unfortunately, this is not possible.

As you can see on Template Parsing library:

Quote:The Template Parser Class is not a full-blown template parsing solution. We’ve kept it very lean on purpose in order to maintain maximum performance.

Try to iterate your entries in your controller and set the title directly from there:

PHP Code:
foreach($blog_entries as &$entry)
{
    
$entry['message'] = '...';


Hope this helps.


RE: Parser and if statement - sasatozovic - 04-15-2016

This is my original code in contoller


PHP Code:
       $query $this->users_mod->allProducts();
 
        
        $data 
= array(
 
                 'ap_heading' => 'Product definition',
 
                 'ap_entries' => $query
                
);
 
       $content $this->parser->parse('allproducts'$dataTRUE); 
 
       $this->load->view('template'compact('content')); 


In view I go like this:
Code:
                   <tbody>
                       {ap_entries}
                           <tr>
                           <td>{id_pro}</td>
                           <td>{name}</td>
                           <td>{licensegen}</td>
                           <td>{version}</td>
                           <td>{expiration}</td>
                           <td>{options}</td>
                           <td>{username}</td>
                           <td>{active}</td>
                           </tr>
                        {/ap_entries}  
                   </tbody>

How to change my controller to get in view for active what I want to? I want to if {active} == 1 to write active, and if {active} == 0 to write disabled.

Can you help me?


RE: Parser and if statement - gxgpet - 04-15-2016

Change your controller code as this:

PHP Code:
$query $this->users_mod->allProducts();

    foreach(
$query as &$item)
    {
        if(
$item['active'] == 1)
        {
            
$item['active'] = 'Active';
        }
        else
        {
            
$item['active'] = 'Disabled';
        }
    }
 
        
        $data 
= array(
 
                 'ap_heading' => 'Product definition',
 
                 'ap_entries' => $query
                
);
 
       $content $this->parser->parse('allproducts'$dataTRUE); 
 
       $this->load->view('template'compact('content')); 



RE: Parser and if statement - sasatozovic - 04-15-2016

That doesn't work. Should I use some template engine or something like that?


RE: Parser and if statement - gxgpet - 04-15-2016

I don't see why it shouldn't work. Are you sure the field is called active?

In my opinion, you shouldn't use the parsing solution. Instead, send the data to your view and iterate it from there:

PHP Code:
    <tbody>
 
                      <?php foreach($entries as $entry): ?>
                           <tr>
                           <td><?php echo $entry->id_pro ?></td>
                           <td><?php echo $entry->name ?></td>
                           <td><?php echo $entry->licensegen ?></td>
                           <td><?php echo $entry->version ?></td>
                           <td><?php echo $entry->expiration ?></td>
                           <td><?php echo $entry->options ?></td>
                           <td><?php echo $entry->username ?></td>
               <?php if($entry->active == 1): ?>
                               <td>Active</td>
               <?php else: ?>
                   <td>Disabled</td>
               <?php endif; ?>
                           </tr>
                        <?php endforeach; ?> 
    </tbody> 



RE: Parser and if statement - sasatozovic - 04-15-2016

I have to use parse, it's was requested by client.

Do you have some advice how to solve this problem, to use some template engine as twig or something like that?


RE: Parser and if statement - PaulD - 04-15-2016

LOL, bloody clients.....

It is quite easy to implement a full blown parser like twig, you could do that. It is a bit bloated though. Are you in a position to argue/discuss the parsing thing with the client? A full blown parser offers if, foreach and switch statements that are very user friendly for designers.

gxpet is right, that approach should work. When you say it does not work, why does it not work?


RE: Parser and if statement - sasatozovic - 04-15-2016

It works without parser, but as I said I have to use parser Sad.

Beside twig, which template engine also I should consider?


RE: Parser and if statement - dmyers - 04-15-2016

If you want to try something a little more powerful than the CI parser you could give lex a shot. I has a MIT License and is available here https://github.com/pyrocms/lex
I have used it on many occasions with pretty good success.