How to render a piece of Html into a variable in Razor? In Spark I used to write the following code:
<content var="t">
<a class="tab" href="">Tab name</a>
</content>
<content var="tc">
<div class="tabcontent">
<p>Here goes tab content</p>
</div>
</content>
!{tabs(t, tc)}
two variables get passed to a macro that does all the nice wrapping of the content into the tab sheet.
What's the best way to do the same in Razor?
Update: I think I got it..
In Razor, the @<text>...</text>
construct can be user to produce lambda expressions, which can be reused later, which is an extended equivalent of assigning a piece of HTML to a variable. The above example can be implemented in the following way:
Func<int, object> t =
@<text>
<a class="tab" href="">Tab name</a>
</text>;
Func<int, object> tc =
@<text>
<div class="tabcontent">
<p>Here goes tab content</p>
</div>
</text>;
@tabs(t(0), tc(0))
I just can't figure out how to write parameterless lambdas (Func<object>
). the int
parameter in both lambdas above is a dummy. Razor seems to require one parameter (and already creates a variable "item" to denote it within the expression).
If you have an IHtmlContentBuilder in your Razor view, you can use these extensions.
Usage
Just in case anyone else finds this post (as I did), Andy's Update is almost there. In addition to the example given, all you have to do to access the 'int' in the example given is reference
@item
. In@<text></text>
blocks, the variableitem
contains the model it was called on.Here's an example of how it can be used:
In most instances you should probably use a partial view instead of a function like this. But in the rare instances that a partial view is not possible (such as when using the RazorEngine library), this works.
Basically the OP already answered the problem in that you could do something like:
If the text is for a single line only you could even use the "@:" operator, just remmebr to have a semicolon (or if it needs any closing brackets or parenthesis) on the next line, as in the following example:
However you can capture it directly as a string if you want
You could even encapsulate it in a function:
Perhaps you can use HtmlString? I don't think I like this much, but here's what I'd try as an exact translation of what you have...
So... I don't know what your Spark macro looks like, but it seems like an opportunity to use a helper in Razor. You might have something like:
Then you call that from in your page like so: