Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Elad Lachmi 112 posts 144 karma points
    May 16, 2012 @ 20:38
    Elad Lachmi
    0

    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!

  • Funka! 398 posts 661 karma points
    May 17, 2012 @ 01:50
    Funka!
    2

    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!

  • Elad Lachmi 112 posts 144 karma points
    May 17, 2012 @ 06:10
    Elad Lachmi
    0

    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.

  • Funka! 398 posts 661 karma points
    May 19, 2012 @ 04:03
    Funka!
    0

    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:

    @helper drawBooleanImage(dynamic prop)
    {
      string src = "/images/red_dot.png"; // assume false unless found to be true
      if (prop != null && prop.GetType() == typeof(bool) && (bool)prop)
      {
        src = "/images/green_dot.png";
      }
      <img src="@src" alt="@prop" />
    }
    

    And then to use it...

    <table>
      <tr>
        <th>Name</th>
        <th>Field 1</th>
        <th>Field 2</th>
        <th>Field 3</th>
      </tr>
      @foreach(var something in somethingElse)
      {
        <tr> 
          <td>@something.Name</td>
          <td>@drawBooleanImage(something.myProp1)</td>
          <td>@drawBooleanImage(something.myProp2)</td>
          <td>@drawBooleanImage(something.myProp3)</td>
        </tr>
      }
    </table>
    

    Good luck!

     

Please Sign in or register to post replies

Write your reply to:

Draft