3217 Posts in 853 Topics by 812 members
| Go to End | Next > | |
| Author | Topic: | 2524 Views |
-
string length after trim in conditional

29 December 2010 at 11:36pm
When using <% if SomeField %> conditional fields with only spaces in them are displayed as well. Is there a way to do something like if(trim(strlen($var))>0) in template conditionals?
I'm aware I could do this with a custom function in my php file but then I'd have to check all fields individually which seems rather elaborate.
My apologies in advance by the way if this is something documented or already previously answered.
-
Re: string length after trim in conditional

1 January 2011 at 6:20pm
Is there a way to do something like if(trim(strlen($var))>0) in template conditionals?
No, you will need a method in your controller to do this logic. You could have a function called StringExists($string) and then you have a reusable <% if StringExists(FieldName) %>
-
Re: string length after trim in conditional

3 January 2011 at 1:37am
Thanks Willr, sweet and simple! Why didn't I think of that?
-
Re: string length after trim in conditional

16 January 2011 at 6:44am
hi,
could you please tell me why this doesn't work for me? The field in the database is Varchar field. And my function looks like this:
/**
* check if a string exists
* @param unknown_type $varname
*/
public function StringExists($string) {
if(trim(strlen($string))>0) {
return true;
}
return false;
}I don't even see the function getting called. In Page.ss I have:
<% if StringExists(URL) %>
<a href="$URL" class="ext" >$Titel</a>
<% end_if %> -
Re: string length after trim in conditional

16 January 2011 at 1:25pm
You can't pass variables in the template, only literal strings. If you want to check it on a property of the current object, then your controller method should be like this:
public function StringExists() {
if(trim(strlen($this->URL))>0) {
return true;
}
return false;
}As a side note, i'm slightly baffled as to why anyone would trim() and strlen() as strlen() will return an integer :S
-
Re: string length after trim in conditional

16 January 2011 at 9:56pm
@Pigeon - to make sure if someone put a single space that the string is counted as blank?
-
Re: string length after trim in conditional

17 January 2011 at 12:33am
if(strlen(trim($this->URL))>0) {...
-
Re: string length after trim in conditional

17 January 2011 at 12:50am
@Pigeon yes, you're right of course. @swaiba answers the why, and @martimiz answers the how. My bad for putting it the wrong way around initially. Trust me however that in the actual code I have it in the right order.
| 2524 Views | ||
| Go to Top | Next > |





