JQuery's $ is in conflict with that of StringTemplate.Net in ASP.Net MVC
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 :)
Related videos on Youtube
OnesimusUnbound
All original source snippets I post on Stack Exchange sites are dedicated to the Creative Commons Zero (CC0).
Updated on April 17, 2022Comments
-
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.
-
Mauricio Scheffer almost 14 years
-
-
OnesimusUnbound over 15 yearsI 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 over 15 yearsST 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 over 15 yearsFollow up. nVelocity has an issue for $ (mail-archive.com/[email protected]/…) but then nVelocity looks cleaner.
-
loudej over 15 yearsSpark 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 over 15 yearsWhy not use "jQuery" instead of "$"
-
OnesimusUnbound over 15 yearsI always wanted the '$' :). Anyway, you have a point ANaimi :). I guess I can let go of the '$'
-
OnesimusUnbound over 15 yearsSpark looks very promising. I seemed to like it.
-
DevelopingChris almost 15 yearsusing 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 almost 14 yearsI've been using Spark a lot lately, and it's been working great so far. highly recommended.
-
jimmyo over 11 yearsI 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%>".