剃须刀 - 如何呈现的内容到一个变量 [英] Razor - how to render content into a variable

查看:146
本文介绍了剃须刀 - 如何呈现的内容到一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何渲染HTML的一部分成剃刀变量?
在星火我以前写以下code:

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?

更新:我想我懂了。

在剃刀,在 @&LT;文本&GT; ...&LT; /文本&GT; 构造可以由用户产生的lambda前pressions,可重复使用以后,这是一个变量分配一块的HTML的扩展当量。上面的例子可以通过以下方式来实现:

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))

我只是无法弄清楚如何写参数的lambda表达式( Func键&LT;对象&gt; )。上述两个lambda表达式的 INT 参数是一个假人。剃刀似乎需要一个参数(并且已经创建了一个变量项到EX pression中表示它)。

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).

推荐答案

也许你可以使用HtmlString?我不认为我喜欢这么多,但这里是我想尝试的你有什么确切的翻译...

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...

@{
    var t = new HtmlString("<a class='tab' href=''>Tab name</a>");
    var tc = new HtmlString("<div class='tabcontent'><p>Here goes tab content</p></div>");
}
@tabs(t, tc)

所以...我不知道你的星火宏是什么样子,但似乎像剃刀使用一个辅助的机会。你可能会碰到这样的:

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:

@helper Tabs(string tabName, string tabContent)
{
    <!-- some wrapper code -->   
    <a class="tab" href="">@(tabName)</a>
    <!-- some more wrapper code -->
    <div class="tabcontent">
        <p>@(tabContent)</p>
    </div>
    <!-- still more wrapper code -->
}

然后调用从你的网页像这样:

Then you call that from in your page like so:

@Tabs("Tab Name", "Here goes tab content")

这篇关于剃须刀 - 如何呈现的内容到一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆