0

I am trying to use WebMatrix to create static html. (Think CMS.)

I have this helper in App_Code/CardHelpers.cshtml

@helper Cards (string mysuit){

// Class Tags
var ss = Html.Raw("<span class = \"spade\">"); 
var sh = Html.Raw("<span class = \"heart\">");  
var se = Html.Raw("</span>");

// Suits
var S = Html.Raw(ss + "&spades;" + se); 
var H = Html.Raw(sh + "&hearts;" + se);

<p> @mysuit and @H</p>

}

I call it with

@CardHelpers.Cards("S")

The static html output is

<p> S and <span class = "heart">&hearts;</span></p>

So I can use @H inside the helper to create the html I want, but how can I pass in a suit (such as "S") and create the appropriate html. Here, I just get back the S, but what I want to return is

<span class = "spade">&spades;</span>
soupman55
  • 71
  • 1
  • 7

1 Answers1

0

The whole point of Razor is that you can mix markup and C# syntax. So what you want to do is put a conditional or switch statement in that selects the correct output for the given input, like this:

@{ 
  string result = "";
  switch(mysuit) { 
    case "H": result = H; break;
    case "S": result = S; break;
    default: break;
  }
  <p> @mysuit and @result</p>
}
Peter Gluck
  • 8,168
  • 1
  • 38
  • 37