简体   繁体   English

文本/ x-jquery-tmpl脚本模板中的VS2010 IntelliSense

[英]VS2010 IntelliSense inside text/x-jquery-tmpl script templates

I've been using jQuery templates which I absolutely love to use. 我一直在使用jQuery模板,我非常喜欢使用它。 The only drawback from an IDE standpoint is the lack of HTML IntelliSense inside the script tag. IDE观点的唯一缺点是脚本标记内缺少HTML IntelliSense。 Is there a way to fool VS2010 so that markup inside template script tags get IntelliSense and syntax highlighting? 有没有办法愚弄VS2010,以便模板脚本标签内的标记获得IntelliSense和语法高亮?

I created a helper method for ASP.NET MVC 3 that works like this, inspired by Html.BeginForm: 我为ASP.NET MVC 3创建了一个帮助方法,其工作原理如下,受Html.BeginForm的启发:

within the view: 在视图中:

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

Anything within the @using scope will be syntax highlighted. @using范围内的任何内容都将突出显示语法。

The code for the helper: 帮助者的代码:

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

I'd solved this issue by creating simple custom serverside control (for ASP.NET 4 WebForms app.): 我通过创建简单的自定义服务器端控件(适用于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();          
    }

}

and put each jquery template inside it (instead of <script> tag): 并将每个jquery模板放入其中(而不是<script>标记):

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

will rendered it in HTML as: 将在HTML中呈现为:

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

I don't think you can unless you trick the editor to not know you are writing a script tag, like writing it in code (HTML helper or so). 我不认为你可以,除非你欺骗编辑器不知道你正在写一个脚本标签,就像在代码中编写它(HTML帮助器等)。 I'd work around it by changing the tag name (or commenting it) temporarily and bringing it back after I'm done. 我会通过暂时更改标签名称(或评论它)并在完成后将其恢复来解决它。 Sure that's a silly solution. 当然这是一个愚蠢的解决方案。

One other thing you can do is instead of using script tag, use any other tag like div setting it's style to display:none . 你可以做的另一件事是,而不是使用script标签,使用任何其他标记像div设置它的style ,以display:none It should work exactly the same way (example in middle of this article ). 它应该以完全相同的方式工作( 本文中间的示例)。

If you're using MVC you can make an extension method for the HtmlHelper like this: 如果你正在使用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());
     }
}

Use like 用得像

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

You'll get full syntax coloring and intellisense that way. 你将获得完整的语法着色和intellisense。 (though, VS might warn about invalid nesting of html elements depending on what your templates consist of) (但是,VS可能会警告html元素的无效嵌套,具体取决于模板的组成)

Another WebForms solution, not as thorough or elegant as Rusted's solution, but I drop these two functions inside of a BasePage class from which all my other pages inherit: 另一个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

Then in my aspx: 然后在我的aspx中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM