Coder Social home page Coder Social logo

csharp-ember-handlebars's Introduction

csharp-ember-handlebars

.NET Library for pre-compilation of Handlebars templates directly into Ember's TEMPLATES collection.

PM> Install-Package csharp-ember-handlebars-compiler -Pre 

Example

In order to register a template, the builder expects a string with the template name and and another with its corresponding markup:

var builder = new Ember.Handlebars.TemplateBuilder();
builder.Register("application", "<h1>{{App.name}}</h1>");

To retrieve the pre-compiled templates, simply call builder.ToString(). This will return the following:

Ember.TEMPLATES["application"] = Ember.Handlebars.template(
    function anonymous(Handlebars, depth0, helpers, partials, data) {
        helpers = helpers || Ember.Handlebars.helpers; data = data || {};
        var buffer = '', stack1, escapeExpression=this.escapeExpression;
        data.buffer.push("<h1>");
        stack1 = helpers._triageMustache.call(depth0, "App.name", {hash:{},contexts:[depth0],data:data});
        data.buffer.push(escapeExpression(stack1) + "</h1>");
        return buffer;
    }
);

The above shows the basic registration of templates, but going forward you might want to add this functionality as part of your application's build process including this functionality directly into the BundleConfig class. Refer to Example Web API project.

Note: To run the Web API Sample, open the Package Manager Console and execute PM> Update-Database to create and populate the database.

Built-in IBundleTransform Implementation

This library now has a built-in implementation of IBundleTransform which can be used directly in ASP.NET MVC's BundleConfig class as shown below:

      bundles.Add(new Bundle(EmberJs.BundleNames.Templates,
          new EmberHandlebarsBundleTransform())
          .IncludeDirectory(EmberJs.TemplatesPath, "*.hbs", true)
      );

In developmet, the template will be rendered directly in the cshtml view. See ~/Views/Home/Index.cshtml

// will render appropriately based on whether optimizations are enabled
@Html.RenderEmberTemplates()   

To enable the bundle with pre-compilation of Handlebars templates, edit the web.config to disable debug, setting it to false:

 <configuration>
     ...
     <system.web>
         <compilation debug="false" targetFramework="4.5" />
         ...

Optimizations must be enabled in Global.asax.cs Application_Start method:

 BundleTables.EnableOptimizations = true;

The pre-compiled templates are minified by default, but if for some reason one needs the template not to be minified, do:

 bundles.Add(new Bundle(EmberJs.BundleNames.Templates, new EmberHandlebarsBundleTransform() { minifyTemplates = false })
    .IncludeDirectory(EmberJs.TemplatesPath, "*.hbs", true)
 ); 

Template names must follow Ember.js Naming Conventions found in:

Templates in sub directories will have their name appended to the parent directory, even if the parent directory is also a sub-directory. Consider the following router map:

 App.Router.map ->
    @resource 'shows', ->
        @route 'add'
        @resource 'show', {path: ':show_id'}, ->
            @route 'edit'
            @route 'remove' 

The directory structure for these routes could be:

 ~/
  |_scripts
    |_app
      |_templates
      |_shows
        |_show
        | |_default.hbs
        | |_edit.hbs
        | |_remove.hbs
        |
        |_index.hbs
        |_add.hbs 

The templates will be compiled as:

~/scripts/app/templates/shows/show/default.hbs  => Em.TEMPLATES["show"]
~/scripts/app/templates/shows/show/edit.hbs     => Em.TEMPLATES["show/edit"]
~/scripts/app/templates/shows/show/remove.hbs   => Em.TEMPLATES["show/remove"]
~/scripts/app/templates/shows/index.hbs         => Em.TEMPLATES["shows/index"]
~/scripts/app/templates/shows/add.hbs           => Em.TEMPLATES["shows/add"]

Note that the templates named default.hbs under a directory show will be compiled as Em.TEMPLATES["show"]. The same happens if the template has the same name of the parent directory: If that template has been named show.hbs, it would also be compiled as Em.TEMPLATES["show"].

If one does not need to separate templates in sub directories, but needs to name templates according to conventions, a character replacement will compile show-edit to Em.TEMPLATES["show/edit"] replacing the dash (-) with a slash (/) since Windows does not allow special characters in file or folder names.

Note: The built-in EmberHandlebarsBundleTransform allows your templates to have whichever extension that better suits your development process. The examples above use *.hbs as an extension but it could be something else, like *.html for example.

csharp-ember-handlebars's People

Contributors

globegitter avatar khalidabuhakmeh avatar kingpin2k avatar mastoj avatar milkywayjoe avatar myslik avatar zgramana avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

coderstash

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.