The parameter is probably not null, it's probably "".
You can use the static methods String.IsNullOrEmpty or String.IsNullOrWhiteSpace, or the respective extension built in to Umbraco:
Ah, yes. The MacroParameters indexer returns objects.
As long as the facebookTest parameter stays a string (you can have other types), you can safely cast it so it matches the method parameter.
The reason I like to use that one instead of != "" or != null is that empty values tend to be pretty volatile depending on the status of the owning object.
One day you think they're always null, the next day you discover some user managed to make just the one "", and a year from now, some junior developer manages to fill the database with whitespaces. ;)
Keep in mind that other data types for other parameters might be returned as other objects than string. Those might be anything from null to "null objects" to "crap data" or 0.
Insert an (if) statement in Partial View Macro
Hi,
How is it possible to insert a (if) statement in a macro? I have tried the following without luck:
Thanks in advance!
// René
Sorry wrong code, this code below is the correct one. But it still does not work.
Hi René
I think this code below should work for you, if you already have add the parameter to the macro.
Hope this helps,
/Dennis
Hi Dennis
I have tried the code below. But it shows the Facebook icon every time with or without data in the textbox field "facebookTest".
Have I done something wrong?
// René
The parameter is probably not null, it's probably "".
You can use the static methods
String.IsNullOrEmpty
orString.IsNullOrWhiteSpace
, or the respective extension built in to Umbraco:or
(I guess the difference gives itself away :) )
Hi Lars-Erik
Thanks!
I tried copy in your code but I get this error "the best overloaded method match for 'string.isnullorempty(string)' has some invalid arguments".
Any idea what's wrong?
Anyway, your answer kinda helped me because I tried to edit the code to this:
Can I go on with my editet code or is it a bad idea?
// René
Ah, yes. The MacroParameters indexer returns
objects
.As long as the facebookTest parameter stays a
string
(you can have other types), you can safely cast it so it matches the method parameter.The reason I like to use that one instead of
!= ""
or!= null
is that empty values tend to be pretty volatile depending on the status of the owning object.One day you think they're always
null
, the next day you discover some user managed to make just the one""
, and a year from now, some junior developer manages to fill the database with whitespaces. ;)Thank you for your answer!
Your code now works perfectly. :-)
Happy to help. :)
Keep in mind that other data types for other parameters might be returned as other objects than
string
. Those might be anything fromnull
to "null objects" to "crap data" or0
.is working on a reply...