ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

can i add variable in template view (with parser? )

August 28, 2008 12:16am

Subscribe [2]
  • #1 / Aug 28, 2008 12:16am

    if i render the view with template parser class :

    $this->parser->parse('blog_template', $data);

    can i add number looping in every row in view, for example :

    <?php $i = 0; ?>
      {blog_entries} <? $i++; ?>
         Number : <?=$i?>
         {title}  
         {body}    
         
    
      {/blog_entries}

    I can’t get next loop number with $i++ in template parser method, any idea ?
    how must i do ? I want “no PHP code” in my view…

    thanks.

  • #2 / Aug 28, 2008 1:44am

    Colin Williams

    2601 posts

    Just pass the {i} in with the $data array, as you are with {title} and {body}

  • #3 / Aug 28, 2008 3:48am

    can you give me an example?

  • #4 / Aug 28, 2008 5:09am

    Colin Williams

    2601 posts

    $entries = array(
       array('title' => 'Hello', 'body' => 'My name is Carl'),
       array('title' => 'Hi There!', 'body' => 'My name is Anita'),
    );
    
    $i = 0;
    foreach ($entries as $entry)
    {
       $i++;
       $entry['i'] = $i;
    }
    
    $this->parser->parse('template', array('blog_entries' => $entries));

    And then the template:

    {blog_entries}
         Number : {i}
         {title}  
         {body}   
         
    
    {/blog_entries}
  • #5 / Aug 28, 2008 8:32am

    with your example code, the result is like this :
    Number : {i} Hello   My name is Carl
    Number : {i} Hi There!  My name is Anita

    I wish i can show this :
    Number : 1. Hello   My name is Carl
    Number : 2. Hi There!  My name is Anita

    How must I do??

  • #6 / Aug 28, 2008 8:50am

    xwero

    4145 posts

    Colin forgot to add the changed subarray to the parent array

    foreach ($entries as $tel=>$entry)
    {
       $i = $tel+1;
       $entries[$tel]['i'] = $i;
    }
  • #7 / Aug 28, 2008 8:53am

    thanks, it’s fixed 😊

  • #8 / Aug 28, 2008 3:46pm

    Colin Williams

    2601 posts

    Ah.. thanks, xwero. Typed that up really fast.

    But hopefully Abdul isn’t always going to rely on a forum to fix simple matters of array manipulation. Hopefully you’ve grasped the concept, Abdul, and don’t just take the code we give you, paste it in, and hope it works.

  • #9 / Aug 29, 2008 11:21am

    Thank’s, but i have the problem again :
    I was used the array manipulation in my application, like this :

    $entries = array(  
                             array('title' => 'Hey', 'body' => 'My Name is Ikhsan'),  
                             array('title' => 'Hi!', 'body' => 'How are you today ?'),  
                             array('title' => 'Hello!', 'body' => 'I'm very Handsome :d ?'),  
                             );            
                    
                 // i have a simple comment(detail for every posted posting)  
                 foreach ($entries as $key => $entry)  
                 {  
                     $no = $key+1;  
                     $entries[$key]['no'] = $no;  
                     $entries[$key]['comment'] = array(  
                                     array('name'=>'samson'.$no,'comment'=>'comment'.$no),  
                                     array('name'=>'ryan'.$no,'comment'=>'komentar'.$no)  
                                     );  
                      //detail comment
                     foreach($entries[$key]['comment'] as $keys => $val){  
                                 $entries[$key]['comment'][$keys]['numcomment'] = $keys + 1;  
                     }  
                 }

    And the View, like this :

    {blog_entries}  
          Number post : {no}  
          {title}    
          {body}     
          <b>The Comments : </b>  
                  {comment}  
                         {numcomment}. {name}   {comment}  
                  {/comment}  
     {/blog_entries}

    And my question is, with no PHP code in view :
    Can i create if statement in template(or if is only in controller? => ) ? (with template parser method)
    for example : if {numcomment%2!=0 then <font color=red>{comment}</font>}

  • #10 / Aug 29, 2008 2:10pm

    Colin Williams

    2601 posts

    Also Note: 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.

    Read more here

    The answer is, if you want no code (I think you mean logic) in your view, it HAS to be in the controller.

    for example : if {numcomment%2!=0 then <font color=red>{comment}</font>}

    So, yeah, that wouldn’t be PHP code, but it’s still some sort of programming language. You’re either going to allow logic in your views or not (logic being conditional statements and loops, etc.)

    You might want to instead use something like Smarty, which is a more robust template parser engine.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases