JQuery's $ is in conflict with that of StringTemplate.Net in ASP.Net MVC

15,455

Solution 1

You can of course move your js logic into a .js file. But if you want it inline with your StringTemplate views, you can escape it using the \$ construct.

In addition, you can simply use the jQuery("selector"), instead of $("selector") construct if you want to avoid the escaping syntax.

Here's a good article on using StringTemplate as a View Engine in MVC.

There's also an accompanying OpenSource engine, along with some samples.

Also, as mentioned above, you can modify your Type Lexer. (make it an alternate character to the $).

Solution 2

I would highly recommend Spark. I've been using it for awhile now with jQuery and haven't ran into a single issue so far.

Solution 3

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.

Solution 4

Perhaps jQuery.noConflict will work for you

Solution 5

I really like the syntax in Django, so I recommend NDjango :)

Share:
15,455

Related videos on Youtube

OnesimusUnbound
Author by

OnesimusUnbound

All original source snippets I post on Stack Exchange sites are dedicated to the Creative Commons Zero (CC0).

Updated on April 17, 2022

Comments

  • OnesimusUnbound
    OnesimusUnbound about 2 years

    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.

  • OnesimusUnbound
    OnesimusUnbound over 15 years
    I think this option is not applicable for StringTemplate. Stringtemplate is not a JavaScript library. The noConflict() is used when another JavaScript library is used and that library uses the $
  • OnesimusUnbound
    OnesimusUnbound over 15 years
    ST uses \$ to denote it as $ when the engine generates the output, but it looked awkward. Well, I might look on nVelocity and see if I'll like it. :)
  • OnesimusUnbound
    OnesimusUnbound over 15 years
    Follow up. nVelocity has an issue for $ (mail-archive.com/[email protected]/…) but then nVelocity looks cleaner.
  • loudej
    loudej over 15 years
    Spark won't collide with jQuery because $ is only recognized directly in front of an {expression}. There's no valid jQuery statement that starts with "${".
  • ANaimi
    ANaimi over 15 years
    Why not use "jQuery" instead of "$"
  • OnesimusUnbound
    OnesimusUnbound over 15 years
    I always wanted the '$' :). Anyway, you have a point ANaimi :). I guess I can let go of the '$'
  • OnesimusUnbound
    OnesimusUnbound over 15 years
    Spark looks very promising. I seemed to like it.
  • DevelopingChris
    DevelopingChris almost 15 years
    using jQuery instead of $ is a good habbit anyway, since prototype and ms ajax libraries hijack $ anyway, and you want to know explicitly what is jQuery and what is not so good.
  • Dave Thieben
    Dave Thieben almost 14 years
    I've been using Spark a lot lately, and it's been working great so far. highly recommended.
  • jimmyo
    jimmyo over 11 years
    I had experience with it, but I don't see any option for localize content. Using the global resources on my test.aspx template page would parse as is "<%=Resources.Test.FirstName%>".