Welcome Guest, Not a member yet? Register   Sign In
Parser and if statement
#1

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?
Reply
#2

(This post was last modified: 04-15-2016, 08:35 AM by gxgpet.)

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.
Reply
#3

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?
Reply
#4

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')); 
Reply
#5

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

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> 
Reply
#7

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?
Reply
#8

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?
Reply
#9

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

Beside twig, which template engine also I should consider?
Reply
#10

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB