通过 oocss 脚本添加 标签

本文关键字:标签 添加 oocss 脚本 通过 | 更新日期: 2023-09-27 18:37:10

我正准备更新一个网站,我正在考虑使用 oocss。 我知道对oocss有很多不同的意见,但到目前为止,它似乎是我的项目的不错选择。 我唯一不喜欢的是使用 HTML 中的标签来设置复杂模块的样式。 oocss 文档说使用某种脚本来插入这些标签,使它们在不再需要时更容易删除,但它没有详细说明如何做到这一点。如果有人能指出我的解决方案或有关如何实现这一点的更多信息,我将不胜感激。 我的网站是用 c# 中的 asp 编写的,我将使用剃刀模板。 如果可能的话,我还想实现这个服务器端,而不是使用 java 脚本。 谢谢。

这是一个复杂模块的 html 示例:

<div class="mod complex"> 
    <b class="top"><b class="tl"></b><b class="tr"></b></b>
<div class="inner">
    <div class="bd">
        <p>Lorem ipsum.</p>
    </div>
</div>
<b class="bottom"><b class="bl"></b><b class="br"></b></b> 

感谢马克,我四处寻找一些 html 助手示例并找到了这篇文章。

我稍微修改了代码,所以我的助手看起来像:

    public class Complex : IDisposable
{
    private readonly ViewContext _viewContext;
    private bool _disposed = false;
    public Complex(ViewContext viewContext)
    {
        _viewContext = viewContext;
    }
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
    protected virtual void Dispose(bool disposing)
    {
        if (!_disposed)
        {
            _disposed = true;
            _viewContext.Writer.Write(
                @"</div>
                    <b class=""bottom""><b class=""bl""></b><b class=""br""></b></b>
                    </div>" 
            );
        }
    }
}
public static class HtmlExtensions
{
    public static Complex Complex(this HtmlHelper htmlHelper)
    {
        htmlHelper.ViewContext.Writer.Write(
            @"<div class=""mod complex"">
                  <b class=""top""><b class=""tl""></b><b class=""tr""></b></b>
                    <div class=""inner"">"
        );
        return new Complex(htmlHelper.ViewContext);
    }
}

然后在我看来,我包括:

    @using (Html.Complex())
{
    <div class="bd">
        <p>Lorem ipsum.</p>
    </div>
}

但是现在有没有办法在视图中包含一个 css 类,以便将其放入第一个div 的类中? 如果这是一个愚蠢的问题,我很抱歉,但我对 C# 和 MVC 很陌生。 这是我到目前为止得到的:

我将静态类 html.extensions 更改为:

    public static class HtmlExtensions
{
    public static  Flow Flow(this HtmlHelper htmlHelper, string classname)
    {
        htmlHelper.ViewContext.Writer.Write(@"<div class="" " + classname + @"mod complex flow"">
                  <b class=""top""><b class=""tl""></b><b class=""tr""></b></b>
                    <div class=""inner"">"
        );
        return new Flow(htmlHelper.ViewContext);
    }
}

把这个放在我的观点中:

@using (Html.Flow("bgtest"))
    {
        <div class="bd">
         <div style="height:442px; width:980px; background-color: grey;"></div>
        </div>
    }

我收到此错误:CS1501:方法"Flow"没有重载需要 0 个参数。

通过 oocss 脚本添加 <b> 标签

我不确定是否有任何开源可以帮助您在 MVC 或 Razor 中实现 oocs ASP.NET。我建议使用 oocss 创建可重用的 html 助手来渲染模块(按钮、分页等)。

例如,使用 oocss 创建按钮。可以使用单独的 html 帮助程序CssButton,该帮助程序使用传递的模型呈现具有预期 html 的内容。因此,您可以在视图中的任何位置重用,

@Html.CssButton(model)

用于创建自定义 html 帮助程序

http://www.asp.net/mvc/tutorials/older-versions/views/creating-custom-html-helpers-cs

甚至您可以将所有呈现模块的 html 帮助程序包装在一个单独的程序集中,以便它可以跨项目重用。

http://blogs.msdn.com/b/davidebb/archive/2010/10/27/turn-your-razor-helpers-into-reusable-libraries.aspx