You can do this either as an inline macro within your template, ... or by setting up as a macro in the Developer section and including the reference from there. Since you asked for an inline macro, here's one way to do this:
I know the "if" condition looks a bit long-winded, but it's the approach I have found is the safest for me, since it doesn't assume that the property you're checking exists, has a value, or is even a boolean. You did mention a "0" or "1" value, although my example is showing how to work the "yes/no" datatype that comes with the installation. (If you really were storing a 0 or 1, you would want to modify this one line and possibly make it simpler.) My example also assumes false unless explicty found to be true. (Although maybe for NULL you could use a grey or blank dot?)
Thing is... I have to do this about 50 times over for different document type fields. I was thinking maybe create some sort of function or helper or whatever it's called for razor,
Inline razor function
Hi,
I am creating a table which will display a set of true/false values as green and red dots (images).
What I want to do is have a function which takes the 0 or 1 value and returns a path to the correct image.
I want to do all of this inline inside the template.
I am new to razor and I have no idea where to even start.
Can anyone show me an example or point me at some resource?
Thank you!
Hi,
You can do this either as an inline macro within your template, ... or by setting up as a macro in the Developer section and including the reference from there. Since you asked for an inline macro, here's one way to do this:
<umbraco:Macro runat="server" language="cshtml">
@{
string src = "/images/red_dot.png"; // assume false unless found to be true
if (Model.myPropertyAlias != null && Model.myPropertyAlias.GetType() == typeof(bool) && (bool)Model.myPropertyAlias)
{
src = "/images/green_dot.png";
}
<img src="@src" alt="@myPropertyAlias " />
}
</umbraco:Macro>
I know the "if" condition looks a bit long-winded, but it's the approach I have found is the safest for me, since it doesn't assume that the property you're checking exists, has a value, or is even a boolean. You did mention a "0" or "1" value, although my example is showing how to work the "yes/no" datatype that comes with the installation. (If you really were storing a 0 or 1, you would want to modify this one line and possibly make it simpler.) My example also assumes false unless explicty found to be true. (Although maybe for NULL you could use a grey or blank dot?)
Good luck!
Thanks Funka.
Thing is... I have to do this about 50 times over for different document type fields. I was thinking maybe create some sort of function or helper or whatever it's called for razor,
but I have no idea how to go about it.
Hi Elad, ... you are right about creating a helper if you want to use this more than once.
Here's how I might set that up:
And then to use it...
Good luck!
is working on a reply...