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条回答
Anthone
2楼-- · 2020-01-27 03:25

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

查看更多
地球回转人心会变
3楼-- · 2020-01-27 03:32

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.

查看更多
Deceive 欺骗
4楼-- · 2020-01-27 03:33

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.

查看更多
干净又极端
5楼-- · 2020-01-27 03:33

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/

查看更多
乱世女痞
6楼-- · 2020-01-27 03:34

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$
查看更多
Evening l夕情丶
7楼-- · 2020-01-27 03:34

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

查看更多
登录 后发表回答