JQuery's $ is in conflict with that of StringT

2020-01-27 03:10发布

I am exploring ASP.NET MVC and I wanted to add jQuery to make the site interactive. I used StringTemplate, ported to .Net, as my template engine to generate html and to send JSON. However, when I view the page, I could not see it. After debugging, I've realized that the $ is used by the StringTemplate to access property, etc and jQuery uses it too to manipulate the DOM. Gee, I've looked on other template engines and most of them uses the dollar sign :(.

Any alternative template engine for ASP.Net MVC? I wanted to retain jQuery because MSFT announced that it will used in the Visual Studio (2008?)

Thanks in Advance :)

Update

Please go to the answer in ASP.NET MVC View Engine Comparison question for a comprehensive list of Template engine for ASP.NET MVC, and their pros and cons

Update 2

At the end I'll just put the JavaScript code, including JQuery, in a separate script file, hence I wouldn't worry about the $ mingling in the template file.

Update 3

Changed the Title to reflect what I need to resolve. After all "The Best X in Y" is very subjective question.

14条回答
唯我独甜
2楼-- · 2020-01-27 03:37

JQuery can be disambiguated by using the jQuery keyword like this:

jQuery(

instead of this:

$(

I would consider this a best practice. It eliminates any possibility of clashing with another library, and makes the code more readable.

查看更多
劫难
3楼-- · 2020-01-27 03:38

I've been using ANTLR StringTemplate for ASP.NET MVC project. However what I did was to extend the StringTemplate grammar (template.g) to recognize '%' (aspx.template.g) as delimiters. You can find these files if you download the StringTemplate.net version. I generated the corresponding files: AspxTemplateLexer.cs, AspxTemplateParser.cs, AspxTemplateParserTokenTypes.cs and AspxTemplateParserTokenTypes.txt.

In addition I altered StringTemplateLoader.cs to recognize the extensions .aspx and .ascx which Visual Studio recognizes. This way I am not stuck with the .st extension and clients don't know the difference.

Anyway after rebuilding StringTemplate I have the behavior that I want. What I like about StringTemplate is that it does NOT permit ANY code to be embedded in the template. It looks like Spark like the default ASP/MVC template is code permissive which makes the templates less portable.

I would prefer is "<%" and "%>" as delimiters but unfortunately the ANTLR grammar seems somewhat difficult and fragile to alter unless someone else has done it. On the other had StringTemplate has a great support community and a great approach to separation -- which is the point of MVC.

查看更多
登录 后发表回答