template_block 0.2.0

template_block #

The 'template block' is a template engine for the code generators with a very simple syntax.

Version: 0.1.6

import "package:template_block/template_block.dart";

void main() {
  var statement = new TemplateBlock(_templateStatement);
  statement.assign("MIN", 0);
  statement.assign("MAX", 128);
  var methodFoo = new TemplateBlock(_templateMethodFoo);
  methodFoo.assign("#STATEMENTS", statement.process());
  statement = statement.clone();
  statement.assign("MIN", 512);
  statement.assign("MAX", 1024);
  methodFoo.assign("#STATEMENTS", statement.process());
  var methodBaz = new TemplateBlock(_templateMethodBaz);
  var clazz = new TemplateBlock(_templateClass);
  clazz.assign("NAME", "MyClass");
  clazz.assign("#METHODS", methodBaz.process());
  clazz.assign("#METHODS", methodFoo.process());
  var library = new TemplateBlock(_templateLibraryPart);
  library.assign("NAME", "my_lib");
  library.assign("#CLASSES", clazz.process());
  print(library.process().join("\n"));
}

final String _templateLibraryPart =
    '''
part of {{NAME}};

{{#CONSTANTS}}
{{#VARIABLES}}
{{#CLASSES}}
''';

final String _templateClass =
    '''
class {{NAME}} {
  {{#CONSTANTS}}
  {{#VARIABLES}}
  {{#METHODS}}
}
''';

final String _templateMethodFoo =
    '''
int foo(int start, int end) {
  $BAZ_NAME();
  {{#STATEMENTS}}
}
''';

const String BAZ_NAME = "baz";

final String _templateMethodBaz = '''
void $BAZ_NAME() {
}
''';

final String _templateStatement =
    '''
for (var i = {{MIN}}; i < {{MAX}}; i++) {
}''';

Output:

part of my_lib;

class MyClass {
  void baz() {
  }
  
  int foo(int start, int end) {
    baz();
    for (var i = 0; i < 128; i++) {
    }
    for (var i = 512; i < 1024; i++) {
    }
  }
  
}

Grammar

template <- ((lastLine / multiLine / singleLine / emptyLine) (NEW_LINE !EOF)?)* EOF

lastLine <- NEW_LINE &EOF

multiLine <- multiLinePrefix* multiLineKey multiLineSuffix*

multiLinePrefix <- !multiLineKey !NEW_LINE .

multiLineKey <- "{{#" IDENT "}}"

multiLineSuffix <- !NEW_LINE .

singleLine <- singleLineParts

singleLineParts <- (singleLineKey / singleLinePart)+

singleLineKey <- "{{" IDENT "}}"

singleLinePart <- !singleLineKey !NEW_LINE .

emptyLine <- &NEW_LINE

EOF <- !.

IDENT <- IDENT_START IDENT_CONT*

IDENT_START <- [A-Za-z] / "_"

IDENT_CONT <- [0-9A-Za-z] / "_"

NEW_LINE <- "\n" / "\r\n" / "\r"

0.2.0 #

  • Adaptation to Dart 2.2

0.1.6 #

  • Fixed bug template parser

0.1.5 #

  • Generated the new parser with using the new version of peg parser generator.

0.1.3 #

  • Generated the new parser according to changes in strings packages

0.1.2 #

  • Fixed bug in grammar (character _ added to the allowed part of IDENT)

0.1.1 #

  • Template parser has been rewritten using PEG grammar

example/example.dart

import "package:template_block/template_block.dart";

void main() {
  var statement = TemplateBlock(_templateStatement);
  statement.assign("MIN", 0);
  statement.assign("MAX", 128);
  var methodFoo = TemplateBlock(_templateMethodFoo);
  methodFoo.assign("#STATEMENTS", statement.process());
  statement = statement.clone();
  statement.assign("MIN", 512);
  statement.assign("MAX", 1024);
  methodFoo.assign("#STATEMENTS", statement.process());
  var methodBaz = TemplateBlock(_templateMethodBaz);
  var clazz = TemplateBlock(_templateClass);
  clazz.assign("NAME", "MyClass");
  clazz.assign("#METHODS", methodBaz.process());
  clazz.assign("#METHODS", methodFoo.process());
  var library = TemplateBlock(_templateLibraryPart);
  library.assign("NAME", "my_lib");
  library.assign("#CLASSES", clazz.process());
  print(library.process().join("\n"));
}

final String _templateLibraryPart = '''
part of {{NAME}};

{{#CONSTANTS}}
{{#VARIABLES}}
{{#CLASSES}}
''';

final String _templateClass = '''
class {{NAME}} {
  {{#CONSTANTS}}
  {{#VARIABLES}}
  {{#METHODS}}
}
''';

final String _templateMethodFoo = '''
int foo(int start, int end) {
  $BAZ_NAME();
  {{#STATEMENTS}}
}
''';

const String BAZ_NAME = "baz";

final String _templateMethodBaz = '''
void $BAZ_NAME() {
}
''';

final String _templateStatement = '''
for (var i = {{MIN}}; i < {{MAX}}; i++) {
}''';

Use this package as a library

1. Depend on it

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


dependencies:
  template_block: ^0.2.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:template_block/template_block.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
14
Health:
Code health derived from static analysis. [more]
89
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
54
Learn more about scoring.

We analyzed this package on Aug 21, 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: Flutter, web, other

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

Health issues and suggestions

Document public APIs. (-1 points)

19 out of 19 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.

Fix lib/src/template_block_parser.dart. (-9.08 points)

Analysis of lib/src/template_block_parser.dart reported 19 hints, including:

line 37 col 7: The value of the field '_cachePos' isn't used.

line 52 col 7: The value of the field '_tokenStart' isn't used.

line 99 col 13: The value of the local variable '$1' isn't used.

line 265 col 15: The value of the local variable '$1' isn't used.

line 266 col 15: The value of the local variable '$2' isn't used.

Fix lib/src/template_block.dart. (-0.50 points)

Analysis of lib/src/template_block.dart reported 1 hint:

line 248 col 9: The value of the local variable 'pos' isn't used.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
Dev dependencies
pedantic ^1.8.0