Skip to main content

Posts

Showing posts from January, 2013

Templates in Javascript with jQuery

The jQuery template: <script id=" bookTemplate " type="text/x-jquery-tmpl">     <tr>         <td>${Author}</td>        <td>${Title}</td>         <td>${Subject}</td>     </tr> </script> Template being used: $('#authors').change(function () {         var author = $('#authors').val();         api.getBooksByAuthor({             success: function (result) {                 $('#bookTableBody').empty();                 $('# bookTemplate ') .tmpl( result ) .appendTo('# bookTableBody ');             }         }, author);     }); Example HTML code where the table cell content is applyed: <table width="600">     <thead>         <tr>             <th>Author</th>             <th>Title</th>             <th>Subject</th>         </tr>     </thead>     <tbody id=" bookTableBody ">    

Razor Helpers with ASP.NET MVC

Add a  App_Code  folder to your project and create a new  CustomHelpers.cshtml  file in it. Add you custum helpers to the file, such as: @helper Truncate(string input, int length) { if(input.Lenght <= Lenght) { @input } else { @input.Substring(0, length)<text>...</text> } } The in order to use it in your razor views, you can do something like: @CustomHelpers.Truncate(ViewBag.SomeMessage as string, 8)