DooPHP IRC channel


template variable access

Discussion about new desired features.

template variable access

Postby RichardM » Sun Sep 20, 2009 10:53 pm

Hi,

I've been looking through my logs and sometimes I will have a template which may contain code similar to:
Code: Select all
<div class="error">{{errorMsg}}</div>


But in my Controller Action I might not have defined $data['errorMsg'] before calling $this->view()->render('blah', $data); so when the template is compiled and used php will log the warning noting that 'errorMsg was not defined'. Therefore I'm wondering if we should have a check in place for isset(...) ? ... : ''; so it will return an empty string if its not been defined so as to avoide PHP warnings? I know I could avoid having the variables in the template or initialise all of them to '' before hand but I am not sure this is something I want to do in every view...especially if I start updating the templates?


Richard
Note: code samples my not be 100% accurate.
RichardM
 
Posts: 1329
Joined: Sun Aug 30, 2009 6:03 pm
Location: Cumbria, UK

Re: template variable access

Postby leng » Mon Sep 21, 2009 11:06 am

You can check it with if statement.
If you want a shorter code, just write a function in template tags that do that.

Code: Select all
function sample($var){
    return (isset($var)) ? $var : '';
}
Just Doo IT!
leng
 
Posts: 1482
Joined: Thu Jul 16, 2009 11:33 pm

Re: template variable access

Postby RichardM » Mon Sep 21, 2009 11:54 am

I'd forgotten about the ability to add in extra template functions :oops: . I'll see about integrating this into the tutorial later on. Also more KISS friendly that my original idea to force it for every template.


Richard
Note: code samples my not be 100% accurate.
RichardM
 
Posts: 1329
Joined: Sun Aug 30, 2009 6:03 pm
Location: Cumbria, UK

Re: template variable access

Postby RichardM » Wed Sep 23, 2009 3:24 am

Hey,

Just tried this and still getting the error. I think if I'm not mistaken the error is because writing

Code: Select all
<p>{{sample(myUndefinedVar)}}</p>


results in something like
Code: Select all
<p><?php echo sample($data['myUndefinedVar']); ?></p>


So php will try and read the undefined array element before its passed to sample to be processed.

right now I have opted for the following
Code: Select all
<!-- if {{isset(username)}}  -->{{username}}<!-- endif -->


but i find this looks kinda messy in my template file.

Have you any other ideas?


Richard
Note: code samples my not be 100% accurate.
RichardM
 
Posts: 1329
Joined: Sun Aug 30, 2009 6:03 pm
Location: Cumbria, UK

Re: template variable access

Postby leng » Wed Sep 23, 2009 3:42 am

hmm, why would it be undefined? Anyway you can process it in the Controller and set it to null
Just Doo IT!
leng
 
Posts: 1482
Joined: Thu Jul 16, 2009 11:33 pm


Return to Features Request

Who is online

Users browsing this forum: No registered users and 1 guest

cron