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

14

17

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.

Histrionism answered 6/10, 2008 at 4:30 Comment(3)
I have heard great things about StringTemplate, it will be interesting to see if you can get this working by changing the ANTLR delimiters as suggested below...Rudolphrudwik
What could be a better delimiter for '$'? Could it be '<$ ... $>'?Histrionism
#1451819Ephram
R
13

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 $).

Rosalie answered 14/2, 2009 at 20:13 Comment(1)
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.Rueful
K
10

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.

Karlin answered 8/10, 2008 at 16:19 Comment(3)
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 "${".Zlatoust
Spark looks very promising. I seemed to like it.Histrionism
I've been using Spark a lot lately, and it's been working great so far. highly recommended.Aleman
P
7

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.

Peterpeterborough answered 6/10, 2008 at 4:30 Comment(0)
A
6

Perhaps jQuery.noConflict will work for you

Adrenalin answered 6/10, 2008 at 4:32 Comment(3)
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 $Histrionism
Why not use "jQuery" instead of "$"Harken
I always wanted the '$' :). Anyway, you have a point ANaimi :). I guess I can let go of the '$'Histrionism
C
3

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

Colitis answered 6/10, 2008 at 4:30 Comment(0)
K
3

Have a look at the mvccontrib project. They have 4 different view engines at the moment which are brail, nhaml, nvelocity and xslt.

http://www.codeplex.com/MVCContrib

Kwon answered 6/10, 2008 at 19:50 Comment(0)
U
3

In case you want to stick with StringTemplate (ST) see this article from the ST wiki. You may also change the behaviour totally by editing Antlr.StringTemplate.Language\DefaultTemplateLexer.cs and replacing the "$" with what you want.

Usurious answered 27/10, 2008 at 18:44 Comment(0)
D
1

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.

Dorotheadorothee answered 6/10, 2008 at 4:30 Comment(0)
D
1

JsonFx.NET has a powerful client-side templating engine with familiar ASP.NET style syntax. The entire framework is specifically designed to work well with jQuery and ASP.NET MVC. You can get examples of how to build real world UI from: http://code.google.com/p/jsonfx-examples/

Dowager answered 6/10, 2008 at 4:30 Comment(0)
K
1

Have you tried $$ or /$ to escape the dollar signs in string template? I'm not sure about ST specifically but thats how most template engines work.

As for other templating engines, I really loved nVelocity when I used it on a project.

Karlenekarlens answered 6/10, 2008 at 4:44 Comment(2)
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. :)Histrionism
Follow up. nVelocity has an issue for $ (mail-archive.com/[email protected]/…) but then nVelocity looks cleaner.Histrionism
P
0

Found Mustache to be the most fool-proof, easiest-to-use, lightest full-featured templating engine for .Net projects (Web and backend)

Works well with .Net 3.5 (meaning it does not need dynamic type and .Net 4.0 to work for mixed type models, like Razor).

The part that I like the most is ability to nest arbitrary IDicts within and have the engine do the right thing. This makes the mandatory-for-all engines reboxing step super-simple:

var child = new {
    nested = "nested value"
};
var parent = new {
    SomeValue = "asdfadsf"
    , down = child
    , number = 123
};

var template = @"This is {{#down}}{{nested}}{{/down}}. Yeah to the power of {{number}}";

string output = Nustache.Core.Render.StringToString(template,parent);
// output:
// "This is nested value. Yeah to the power of 123"

What's most beautiful about Mustache is that same exact template works exactly same in pure JavaScript or any other of 20 or so supported languages.

Penneypenni answered 6/10, 2008 at 4:30 Comment(1)
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%>".Vasilikivasilis
J
0

If I understand StringTemplate version 4 correctly you can define your own escape char in Template (or TemplateGroup) constructor.

Jaynejaynell answered 6/10, 2008 at 4:30 Comment(0)
H
0

You may need this .NET Template Engine. If you wish to use '$' character, simply use '$$'. See the code below:

{%carName = "Audi R8"/}

{%string str = "This is an $carName$"/}

$str$
$$str$$

the output will be

This is an Audi R8
$str$
Hominy answered 6/10, 2008 at 4:30 Comment(0)
D
0

You could try jsRepeater.

Denisse answered 2/11, 2008 at 3:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.