stencil 2.1.6

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 72

Stencil

A super simple type-safe, analyzer and code-completion friendly HTML template library for Dart.

Think code-safe, code-completable alternative to mustache.

Features

  • [x] Data binding
    • [x] Content
    • [x] Attribute
  • [x] Conditional nodes (if and else)
  • [x] Loops
  • [x] Components

Usage

Simple data binding

Model

class Movie {
  final String name;

  final String description;

  final int stars;

  Movie(this.name, this.description, this.stars) {
    if (stars < 0 || stars > 5)
      throw new ArgumentError.value(stars, 'stars', 'Must be between 0 and 5!');
  }
}

Components

class MovieCardComponent extends Component {
  final Movie movie;

  MovieCardComponent(this.movie);

  @override
  String render() {
    return '''
<div class="movie-card">
  <div>${movie.name}</div>
  <div>${movie.description}</div>
</div>
    ''';
  }
}

Usage

final Movie castaway = new Movie(
    'Cast away',
    'Obsessively punctual FedEx executive Chuck Noland (Tom Hanks) is en route to an assignment in Malaysia when his plane crashes over the Pacific Ocean during a storm.',
    4);

print(new MovieCardComponent(castaway).render());

Rendering

<div class="movie-card">
  <div>Cast away</div>
  <div>Obsessively punctual FedEx executive Chuck Noland (Tom Hanks) is en route to an assignment in Malaysia when his plane crashes over the Pacific Ocean during a storm.</div>
</div>

Loop range

Components

class RatingComponent extends Component {
  final int stars;

  RatingComponent(this.stars);

  @override
  String render() {
    return '''
<div class="stars-holder">
  ${until(stars, (i) => '<div class="star-filled"></div>\n  ')}
  ${until(5-stars, (i) => '<div class="star"></div>')}
</div>''';
  }
}

Usage

print(new RatingComponent(4).render());

Rendering

<div class="stars-holder">
  <div class="star-filled"></div>
  <div class="star-filled"></div>
  <div class="star-filled"></div>
  <div class="star-filled"></div>

  <div class="star"></div>
</div>

Component

Components

class RatingComponent extends Component {
  final int stars;

  RatingComponent(this.stars);

  @override
  String render() {
    return '''
<div class="stars-holder">
  ${until(stars, (i) => '<div class="star-filled"></div>\n  ')}
  ${until(5-stars, (i) => '<div class="star"></div>')}
</div>''';
  }
}

class MovieCardComponent extends Component {
  final Movie movie;

  MovieCardComponent(this.movie);

  @override
  String render() {
    return '''
<div class="movie-card">
  <div class="movie-name">${movie.name}</div>
  <div class="movie-description">${movie.description}</div>

  ${comp(new RatingComponent(movie.stars))}
</div>
    ''';
  }

Usage

final Movie castaway = new Movie(
    'Cast away',
    'Obsessively punctual FedEx executive Chuck Noland (Tom Hanks) is en route to an assignment in Malaysia when his plane crashes over the Pacific Ocean during a storm.',
    4);

print(new MovieCardComponent(castaway).render());

Rendering

<div class="movie-card">
  <div class="movie-name">Cast away</div>
  <div class="movie-description">Obsessively punctual FedEx executive Chuck Noland (Tom Hanks) is en route to an assignment in Malaysia when his plane crashes over the Pacific Ocean during a storm.</div>

  <div class="stars-holder">
  <div class="star-filled"></div>
  <div class="star-filled"></div>
  <div class="star-filled"></div>
  <div class="star-filled"></div>

  <div class="star"></div>
</div>
</div>

Changelog

2.1.6

  • Dart 2 stable

2.1.3

  • Flexible when

2.1.2

  • Render without using Component.
  • Introducing when, forEach, range, until and map outside Component.

example/example.dart

// Copyright (c) 2017, SERAGUD. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

import 'package:stencil/stencil.dart';

/// Sample model
class Movie {
  final String name;

  final String description;

  final int stars;

  Movie(this.name, this.description, this.stars) {
    if (stars < 0 || stars > 5)
      throw new ArgumentError.value(stars, 'stars', 'Must be between 0 and 5!');
  }
}

/// Demonstrates loops using [until]
class RatingComponent extends Component {
  final int stars;

  RatingComponent(this.stars);

  @override
  String render() {
    return '''
<div class="stars-holder">
  ${until(stars, (i) => '<div class="star star-filled">\u2b50</div>\n  ')}
  ${until(5-stars, (i) => '<div class="star">\u2b50</div>')}
</div>''';
  }
}

// Demonstrates simple binding
class MovieComponent extends Component {
  final Movie movie;

  MovieComponent(this.movie);

  @override
  String render() {
    return '''
<div class="movie-card">
  <div class="movie-name">${movie.name}</div>
  <div class="movie-description">${movie.description}</div>
</div>    
    ''';
  }
}

/// Demonstrates nested components
class MovieCardComponent extends Component {
  final Movie movie;

  MovieCardComponent(this.movie);

  @override
  String render() {
    return '''
<div class="movie-card">
  <div class="movie-name">${movie.name}</div>
  <div class="movie-description">${movie.description}</div>
  
  ${comp(new RatingComponent(movie.stars))}
</div>    
    ''';
  }
}

void main() {
  final Movie castaway = new Movie(
      'Cast away',
      'Obsessively punctual FedEx executive Chuck Noland (Tom Hanks) is en route to an assignment in Malaysia when his plane crashes over the Pacific Ocean during a storm.',
      4);

  print(RatingComponent(4).render());
  print(MovieComponent(castaway).render());
  print(MovieCardComponent(castaway).render());
}

Use this package as a library

1. Depend on it

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


dependencies:
  stencil: ^2.1.6

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:


import 'package:stencil/stencil.dart';
  
Version Uploaded Documentation Archive
2.1.6 Aug 7, 2018 Go to the documentation of stencil 2.1.6 Download stencil 2.1.6 archive
2.1.5 Jul 10, 2018 Go to the documentation of stencil 2.1.5 Download stencil 2.1.5 archive
2.1.4 Jul 10, 2018 Go to the documentation of stencil 2.1.4 Download stencil 2.1.4 archive
2.1.2 Jul 10, 2018 Go to the documentation of stencil 2.1.2 Download stencil 2.1.2 archive
2.1.1 Jun 21, 2018 Go to the documentation of stencil 2.1.1 Download stencil 2.1.1 archive
0.0.2 Oct 15, 2017 Go to the documentation of stencil 0.0.2 Download stencil 0.0.2 archive
0.0.1 Oct 2, 2017 Go to the documentation of stencil 0.0.1 Download stencil 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
43
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
72
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: Flutter, web, other

No platform restriction found in primary library package:stencil/stencil.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
build_runner any
build_web_compilers any