handlebars2 0.0.2

  • README.md
  • Example
  • Installing
  • Versions
  • 46

A handlebar.js wrapper in DART

This is an handlebars.js wrapper for the DART developer. Sometime you might need to inject HTML template into the slot of the your web layout without using angular framework. The handlebars.js is a pretty neat template engine, fast, efficient and easy-syntax. There is just no reason not to use it.

How to use it in DART

here is the template


    String html = '''
    {{#each blogs}}
      <div class="entry">
        <h1>{{title}}</h1>
        <div class="body">
          {{body}}
        </div>
      </div>
    {{/each}}
  ''';

here is the context data contract


class Context{
  List<Blog> blogs;
  Context(this.blogs);
}

class Blog {
  String title;
  String body;
  Blog(this.title, this.body);
}

here is how handlebars is used to compile the template


    import 'dart:html';
    import 'package:handlebars.js/handlebars.dart' as Handlebars;

    List<Blog> blogs = <Blog>[
        Blog('Science','Science Blogs'),
        Blog('Technology', 'Technology Blogs'),
        Blog('Engineering', 'Engineering Blogs'),
        Blog('Art','Art Blogs'),
        Blog('Maths', 'Maths Blog'),
    ];

    final container = document.querySelector('.container');
    final String content = Handlebars.compile(html)(Context(blogs));
    container.innerHtml = content;

Note that you have to create context contract to be able to pass blogs to the html template. You don't need to do that. you can pass in Map literal object but you have to use package js to wrap around the map literal object as shown below


    //you have to import this library
    import 'dart:js_util' as js;
    ...
    final String content = Handlebars.compile(html)(js.jsify({
        'blogs': blogs
    }));
    ...    

Here is the result on the web page

Template Result

example/main.dart

import 'dart:html';

import 'package:handlebars2/handlebars2.dart' as Handlebars;

void main(){
  var container = document.querySelector('.container');
  
  String html = '''
    {{#each blogs}}
      <div class="entry">
        <h1>{{title}}</h1>
        <div class="body">
          {{body}}
        </div>
      </div>
    {{/each}}
  ''';

  List<Blog> blogs = <Blog>[
    Blog('Science','Science Blogs'),
    Blog('Technology', 'Technology Blogs'),
    Blog('Engineering', 'Engineering Blogs'),
    Blog('Art','Art Blogs'),
    Blog('Maths', 'Maths Blog'),
  ];
  
  final String content = Handlebars.compile(html)(Context(blogs));
  container.innerHtml = content;
}

class Context{
  List<Blog> blogs;
  Context(this.blogs);
}

class Blog {
  String title;
  String body;
  Blog(this.title, this.body);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  handlebars2: ^0.0.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:handlebars2/handlebars2.dart';
  
Version Uploaded Documentation Archive
0.0.2 Sep 20, 2018 Go to the documentation of handlebars2 0.0.2 Download handlebars2 0.0.2 archive
0.0.1 Sep 20, 2018 Go to the documentation of handlebars2 0.0.1 Download handlebars2 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
19
Health:
Code health derived from static analysis. [more]
90
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
50
Overall:
Weighted score of the above. [more]
46
Learn more about scoring.

We analyzed this package on Dec 5, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

Detected platforms: web, other

Primary library: package:handlebars2/handlebars2.dart with components: js.

Health issues and suggestions

Document public APIs (-9.76 points)

281 out of 288 API elements (library, class, field or method) have no adequate dartdoc content. Good documentation improves code readability and discoverability through search.

Fix lib/handlebars2.dart. (-0.50 points)

Analysis of lib/handlebars2.dart reported 1 hint:

line 640 col 16: Name types using UpperCamelCase.

Maintenance issues and suggestions

Maintain CHANGELOG.md. (-20 points)

Changelog entries help clients to follow the progress in your code.

The description is too short. (-20 points)

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Package is pre-v0.1 release. (-10 points)

While there is nothing inherently wrong with versions of 0.0.*, it usually means that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
js ^0.6.1+1 0.6.1+1
Dev dependencies
build_runner ^0.10.2
build_web_compilers ^0.4.3
sass_builder ^2.1.1