Template Parser: Dont display var name if blank

#1
[eluser]Kriesi[/eluser]
Hey everyone, already did a search but couldnt find a solution to my problem:

the title basically says what I need:

I am using the template parser to display my views. since the data array that I pass to the view is populated by a database query and differs with each query I dont always get the same set of variables.

What I want to do is:

Write: {my_var} to the template

if $data[$my_var] has a value exchange {my_var} with this value. (normal behaviour)
if $data[$my_var] is not set just remove the {my_var} and dont display it...

any easy ways of pulling this of?

Thanks in advance
Kriesi

#2
[eluser]renownedmedia[/eluser]
Disable E_WARNING?

#3
[eluser]Kriesi[/eluser]
why should i do that? I dont get a warning, the view simply displays {my_var} as string and I want this string to be removed...

#4
[eluser]Jondolar[/eluser]
I would have thought that if the array variable was set but blank it would remove it but I don't use the built-in parser. Can you just replace the "missing" variable with an HTML comment like <!-- --> so it won't show up in the rendered page? A better option might be to modify the temlate parser and make it work properly.

#5
[eluser]clip[/eluser]
I did this for a solution..
Code:
if($comments == FALSE)
{
     $data['comments'] = array(array('author'=>NULL,
                       'date'=>NULL,
                       'body'=>NULL
                                     )
                );
}
else
..... do more stuff

I haven't searched as to whether or not this is a known bug or not or is working as intended.

#6
[eluser]Kriesi[/eluser]
[quote author="Jondolar" date="1258075252"]I would have thought that if the array variable was set but blank it would remove it but I don't use the built-in parser. Can you just replace the "missing" variable with an HTML comment like <!-- --> so it won't show up in the rendered page? A better option might be to modify the temlate parser and make it work properly.[/quote]

Well actually thats the case, if you set the string to $data[$my_var] = ""; it wont display. the problem I have is that I dont know which variables to set to empty (its a site with multiple languages and some of the content only gets displayed for some of the languages)

the solution I came up with is to check which variables are within the template before parsing and setting everything to empty, then make a database query and merge the 2 arrays so that all empty values that now have a value get overriden:

Code:
$template = $this->load->view('index', '', true);
preg_match_all('/\{(\w+)\}/', $template, $matches);

if($matches[1])
{        
    foreach($matches[1] as $value)
    {    
        $blankdata[$value] = "";
    }
}

$data = //database query here;
$data = array_merge((array)$blankdata, (array)$data;

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

#7
[eluser]Jondolar[/eluser]
Just an FYI,

Doing a regular expression match on every page puts a very heavy load on your site as it is probably one of the most inefficient ways to solve many programming problems. If your site grows significantly, you may need to modify your approach.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.