Welcome Guest, Not a member yet? Register   Sign In
Parser libraby doesn't show non-nested variables
#7

As for me solution you provided looks complicated: adding aditional view file is too much. And I think it would be difficult to make CI popular with such solutions.

My solution:

1) try modifying parser library to support non-nested values along with nested ones inside loop (maybe simply by escaping non-nested ones before output to avoid vulnerability - don't think cutting of functionality because of potential vulnerability to be a good practice)

2) create helper function result_array_extend :

with callback function support (result_array_extend($blog_entries, array('non_nested'=> function($i){return $i%2+1;}))
PHP Code:
function result_array_extend($src_arr,$apnd_arr){
 
    foreach ($src_arr as &$src_item){
 
                $i=0;
 
                foreach ($src_item as &$src_item2){
 
                            foreach($apnd_arr as $key=>$apnd_item){
 
                                      if (is_callable($apnd_item)) { $src_item2[$key]=$apnd_item($i); } else {
 
                                          $src_item2[$key]=$apnd_item;
 
                                       }
 
                                       $i++;
 
                             }
 
               }
 
   
 
   return $src_arr;
 } 

or without callback function support (result_array_extend($blog_entries, $data))
PHP Code:
function result_array_extend($src_arr,$apnd_arr){
 
    foreach ($src_arr as &$src_item){
 
                foreach ($src_item as &$src_item2){
 
                            foreach($apnd_arr as $key=>$apnd_item){
 
                                          $src_item2[$key]=$apnd_item;
 
                             }
 
               }
 
   
 
   return $src_arr;
 } 


then in controller :

PHP Code:
$data = array(
 
   'non_nested'   => 'this is non-nested string',
 
 );
 
 $blog_entries => array(
 
   array('title' => 'Title 1''body' => 'Body 1'),
 
   array('title' => 'Title 2''body' => 'Body 2'),
 
   array('title' => 'Title 3''body' => 'Body 3')
 
 );

$blog_entries  result_array_extend($blog_entries,$data);
$this->parser->parse('test_view',$blog_entries); 

in view file:
Code:
...
<div>
{blog_entries}
      <div><span>{title}</span> <span>{body}</span>{non_nested}</div>
{/blog_entries}
</div>
would output:

Title 1 Body 1this is non-nested string
Title 2 Body 2this is non-nested string
Title 3 Body 3this is non-nested string

or (in case of using callback functiion)

Title 1 Body 11
Title 2 Body 22
Title 3 Body 31

some example from real life :
PHP Code:
$array_result $this->some_model->get(); //get data from db table(s)
$aditional_var $this->uri->segments(3); //get var elswhere rather than db table by means of model

$this->data['result_array'] = result_array_extend($array_result,array('aditional_var'=>$aditional_var));

$this->parser->parse('test_view',$this->data['result_array']); //provide view file with extended data from db table 

then in the view I use to form url

Code:
{result_array}
<a href="/{aditional_var}/{colum1}/{colum2}">{colum3}</a>
{/result_array}

as a result we have out model working with db and returning data from table, our controller forming data to output, view file just outputs provided by controller data and NO PHP code in view
Reply


Messages In This Thread
RE: Parser libraby doesn't show non-nested variables - by griser - 02-22-2015, 02:19 AM



Theme © iAndrew 2016 - Forum software by © MyBB