开发者

Jquery Templates with Razor how to use Razor within text/html scripts

Ok so this is a little random but..

Using MVC 3 (with Razor view engine) with Knockout.js which uses jQuery Templating i've come across a little problem i'm sure is possible to solve.

In order to use jQuery-Tmpl you need to supply a template in

<script type="text/html">
...template elements go here...
</script>

Now the problem is that the razor view engine doesn't seem to generate HTML inside of these specific script tags. It handles standard html, (script type="text/javascript") fine but appears to just 开发者_如何学JAVAnot do anything with the aforementioned script tag.

Does anyone know how to get around this issue i.e. how to use MVC 3 Razor with jQuery-Tmpl?


There is a pretty good solution in this blog post: http://www.wiredprairie.us/blog/index.php/archives/1204

This creates a "template" helper that emits the script start/end tags.

Otherwise, I have some ideas for putting templates in external files, which would be another way to avoid this issue. It involves storing the templates in .html files and injecting them into the page into script tags. There are certainly many ways that this could be accomplished though on the client or server side, just a few ideas.


A more general approach if you want to keep things in the document is using @Html.Raw to output html without affecting the edit-time syntax state.

For example:

@Html.Raw("<script type='text/x-dot-template' id='awesome_template'>")
   <!-- insert some awesomeness here -->           
@Html.Raw("</script>")

I happen to like the helper method suggested above a little better, but it has not always been something I was able to implement, so this is an alternative with its own benefits (namely clarity over ease of use and terseness)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜