【问题标题】:VS2010 IntelliSense inside text/x-jquery-tmpl script templatesVS2010 IntelliSense inside text/x-jquery-tmpl 脚本模板
【发布时间】:2011-01-24 21:29:05
【问题描述】:

我一直在使用我非常喜欢使用的 jQuery 模板。从 IDE 的角度来看,唯一的缺点是 script 标签内缺少 HTML IntelliSense。有没有办法欺骗 VS2010,让模板脚本标签内的标记获得 IntelliSense 和语法高亮?

【问题讨论】:

  • 您是否正在使用 ASP.NET MVC?
  • 这似乎已在 VS2012 中修复

标签: jquery html visual-studio-2010 intellisense


【解决方案1】:

受 Html.BeginForm 的启发,我为 ASP.NET MVC 3 创建了一个类似这样的辅助方法:

在视图中:

@using (Html.BeginHtmlTemplate("templateId"))
{
    <div>enter template here</div>
}

@using 范围内的任何内容都将突出显示语法。

助手的代码:

public static class HtmlHelperExtensions
{
    public static ScriptTag BeginHtmlTemplate(this HtmlHelper helper,string id)
    {
        return new ScriptTag(helper,"text/html", id);
    }
}

public class ScriptTag : IDisposable
{
    private readonly TextWriter writer;

    private readonly TagBuilder builder;

    public ScriptTag(HtmlHelper helper, string type, string id)
    {
        this.writer = helper.ViewContext.Writer;
        this.builder = new TagBuilder("script");
        this.builder.MergeAttribute("type", type);
        this.builder.MergeAttribute("id", id);
        writer.WriteLine(this.builder.ToString(TagRenderMode.StartTag));
    }

    public void Dispose()
    {
        writer.WriteLine(this.builder.ToString(TagRenderMode.EndTag));
    }
}

【讨论】:

  • 这是最好、最优雅的答案。它应该得到投票。
【解决方案2】:

我通过创建简单的自定义服务器端控件(用于 ASP.NET 4 WebForms 应用程序)解决了这个问题:

[ToolboxData("<{0}:JqueryTemplate runat=server></{0}:JqueryTemplate>")]
[PersistChildren(true)]
[ParseChildren(false)]
public class JqueryTemplate : Control {
    private bool _useDefaultClientIdMode = true;

    [DefaultValue(ClientIDMode.Static)]
    [Category("Behavior")]
    [Themeable(false)]
    public override ClientIDMode ClientIDMode {
        get {
            return (_useDefaultClientIdMode) ? ClientIDMode.Static : base.ClientIDMode;
        }
        set { 
            base.ClientIDMode = value;
            _useDefaultClientIdMode = false;
        }
    }

    protected override void Render(HtmlTextWriter writer) {
        writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/x-jquery-tmpl");
        writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
        writer.RenderBeginTag(HtmlTextWriterTag.Script);
        base.Render(writer);
        writer.RenderEndTag();          
    }

}

并将每个 jquery 模板放入其中(而不是 &lt;script&gt; 标签):

<some:JqueryTemplate runat="server" ID="myTemplateId"> 
... your template code goes here ...
</some:JqueryTemplate>

将其在 HTML 中呈现为:

<script type="text/x-jquery-tmpl" id="myTemplateId">
... your template code goes here ...
</script>

【讨论】:

    【解决方案3】:

    如果你使用 MVC,你可以像这样为 HtmlHelper 创建一个扩展方法:

    public static class HtmlExtensions
    {
         public static MvcHtmlString Template(this HtmlHelper htmlHelper, string id, Func<dynamic, HelperResult> content)
         {
             var sb = new StringBuilder();
             sb.AppendFormat("<script type='text/html' id='{0}'>", id);
             sb.Append(content(null));
             sb.Append("</script>");
    
             return new MvcHtmlString(sb.ToString());
         }
    }
    

    像这样使用

    @Html.Template("whatever", @<text>
        <tr><td>template stuff here</td></tr>
    </text>)
    

    您将通过这种方式获得完整的语法着色和智能感知。 (不过,VS 可能会根据您的模板组成警告 html 元素的无效嵌套)

    【讨论】:

      【解决方案4】:

      我不认为你可以,除非你欺骗编辑器不知道你正在编写一个脚本标签,比如用代码编写它(HTML helper 左右)。我会通过临时更改标签名称(或评论它)并在完成后将其恢复来解决它。当然这是一个愚蠢的解决方案。

      您可以做的另一件事是不要使用script 标签,而是使用任何其他标签,例如div,将其设置为styledisplay:none。它应该以完全相同的方式工作(this article 中间的示例)。

      【讨论】:

      • 不客气。那么,如果它是问题的正确答案,您可以标记答案吗?非常感谢。
      • 将其渲染为隐藏的 div 可能会产生副作用,因为浏览器随后将解析内容。另一种解决方案是将它们放在单独的文件中并从文件中删除脚本标签。然后通过 httphandler 或 code 从外部渲染脚本标签。
      • 这对处理程序和额外请求的额外工作很少有副作用,但也可能稍后可以缓存。是的。每个选项肯定都有优点和缺点......
      • 还没有尝试过,但我正在考虑在 mvc 中制作一个部分视图,其中包含没有脚本标签的模板,然后在脚本标签内使用 html.partial 渲染它。
      • 取决于模板的大小,这也是一个非常好的主意。
      【解决方案5】:

      另一个 WebForms 解决方案,不像 Rusted 的解决方案那样彻底或优雅,但我将这两个函数放在 BasePage 类中,我的所有其他页面都继承自该类:

      Public Function Template(ByVal id As String) As String
          ' the type attribute does not matter as long as it's not "text/javascript"
          Return "<script id='" + id + "' type='text/template'>"
      End Function
      
      Public Function Template() As String
          Return "</script>"
      End Function
      

      然后在我的 aspx 中:

       <%= Template("templateID")%>
           ... your template code goes here ...
       <%= Template()%>
      

      【讨论】:

        猜你喜欢
        • 2013-02-27
        • 2012-02-14
        • 2013-10-15
        • 2014-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-18
        相关资源
        最近更新 更多