handlebars2 0.0.3

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.3

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';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
60
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
50
Overall:
Weighted score of the above. [more]
70
Learn more about scoring.

We analyzed this package on Jul 18, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: web, other

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

Health issues and suggestions

Document public APIs. (-0.37 points)

318 out of 325 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Format lib/handlebars2.dart.

Run dartfmt to format lib/handlebars2.dart.

Maintenance issues and suggestions

Provide a file named CHANGELOG.md. (-20 points)

Changelog entries help developers follow the progress of your package. See the example generated by stagehand.

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

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

While nothing is inherently wrong with versions of 0.0.*, it might mean 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
rst_bootstrap ^4.3.1 4.3.1
Dev dependencies
build_runner ^1.3.1
build_web_compilers ^1.2.0
sass_builder ^2.1.2

Admin