From 0dc2dfb28774be318cc34da35c982427160810c2 Mon Sep 17 00:00:00 2001 From: Tobe Osakwe Date: Mon, 28 Jan 2019 14:44:14 -0500 Subject: [PATCH 1/2] Initial commit --- .gitignore | 13 +++++++++++++ LICENSE | 21 +++++++++++++++++++++ README.md | 2 ++ 3 files changed, 36 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..7bf00e82 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +# See https://www.dartlang.org/guides/libraries/private-files + +# Files and directories created by pub +.dart_tool/ +.packages +.pub/ +build/ +# If you're building an application, you may want to check-in your pubspec.lock +pubspec.lock + +# Directory created by dartdoc +# If you don't generate documentation locally you can remove this line. +doc/api/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..8d171670 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Tobe Osakwe + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 00000000..b8b81b7d --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# angel_jinja +Angel support for the Jinja2 templating engine, ported from Python to Dart. From 2a5abb684b7b68f0da4775e9ceaa22cb295d58da Mon Sep 17 00:00:00 2001 From: Tobe O Date: Tue, 29 Jan 2019 13:06:58 -0500 Subject: [PATCH 2/2] 1.0.0-rc.0 --- .gitignore | 10 +++----- CHANGELOG.md | 3 +++ README.md | 45 ++++++++++++++++++++++++++++++++- analysis_options.yaml | 4 +++ example/main.dart | 37 +++++++++++++++++++++++++++ example/views/hello.html | 5 ++++ example/views/index.html | 6 +++++ example/views/layout.html | 10 ++++++++ lib/angel_jinja.dart | 53 +++++++++++++++++++++++++++++++++++++++ pubspec.yaml | 15 +++++++++++ 10 files changed, 181 insertions(+), 7 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 analysis_options.yaml create mode 100644 example/main.dart create mode 100644 example/views/hello.html create mode 100644 example/views/index.html create mode 100644 example/views/layout.html create mode 100644 lib/angel_jinja.dart create mode 100644 pubspec.yaml diff --git a/.gitignore b/.gitignore index 7bf00e82..50602ac6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,13 +1,11 @@ -# See https://www.dartlang.org/guides/libraries/private-files - # Files and directories created by pub .dart_tool/ .packages -.pub/ -build/ -# If you're building an application, you may want to check-in your pubspec.lock +# Remove the following pattern if you wish to check in your lock file pubspec.lock +# Conventional directory for build outputs +build/ + # Directory created by dartdoc -# If you don't generate documentation locally you can remove this line. doc/api/ diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..687440ba --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +## 1.0.0 + +- Initial version, created by Stagehand diff --git a/README.md b/README.md index b8b81b7d..840574ba 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,45 @@ -# angel_jinja +# jinja Angel support for the Jinja2 templating engine, ported from Python to Dart. + +[![Pub](https://img.shields.io/pub/v/angel_jinja.svg)](https://pub.dartlang.org/packages/angel_jinja) + +# Example +```dart +import 'dart:io'; +import 'package:angel_framework/angel_framework.dart'; +import 'package:angel_framework/http.dart'; +import 'package:angel_jinja/angel_jinja.dart'; +import 'package:path/path.dart' as p; + +main() async { + var app = Angel(); + var http = AngelHttp(app); + var viewsDir = p.join( + p.dirname( + p.fromUri(Platform.script), + ), + 'views', + ); + + // Enable Jinja2 views + await app.configure(jinja(path: viewsDir)); + + // Add routes. + // See: https://github.com/ykmnkmi/jinja.dart/blob/master/example/bin/server.dart + + app + ..get('/', (req, res) => res.render('index.html')) + ..get('/hello', (req, res) => res.render('hello.html', {'name': 'user'})) + ..get('/hello/:name', (req, res) => res.render('hello.html', req.params)); + + app.fallback((req, res) { + res + ..statusCode = 404 + ..write('404 Not Found :('); + }); + + // Start the server + await http.startServer('127.0.0.1', 3000); + print('Listening at ${http.uri}'); +} +``` \ No newline at end of file diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 00000000..c230cee7 --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1,4 @@ +include: package:pedantic/analysis_options.yaml +analyzer: + strong-mode: + implicit-casts: false \ No newline at end of file diff --git a/example/main.dart b/example/main.dart new file mode 100644 index 00000000..b40937c0 --- /dev/null +++ b/example/main.dart @@ -0,0 +1,37 @@ +import 'dart:io'; +import 'package:angel_framework/angel_framework.dart'; +import 'package:angel_framework/http.dart'; +import 'package:angel_jinja/angel_jinja.dart'; +import 'package:path/path.dart' as p; + +main() async { + var app = Angel(); + var http = AngelHttp(app); + var viewsDir = p.join( + p.dirname( + p.fromUri(Platform.script), + ), + 'views', + ); + + // Enable Jinja2 views + await app.configure(jinja(path: viewsDir)); + + // Add routes. + // See: https://github.com/ykmnkmi/jinja.dart/blob/master/example/bin/server.dart + + app + ..get('/', (req, res) => res.render('index.html')) + ..get('/hello', (req, res) => res.render('hello.html', {'name': 'user'})) + ..get('/hello/:name', (req, res) => res.render('hello.html', req.params)); + + app.fallback((req, res) { + res + ..statusCode = 404 + ..write('404 Not Found :('); + }); + + // Start the server + await http.startServer('127.0.0.1', 3000); + print('Listening at ${http.uri}'); +} diff --git a/example/views/hello.html b/example/views/hello.html new file mode 100644 index 00000000..22d1c470 --- /dev/null +++ b/example/views/hello.html @@ -0,0 +1,5 @@ +{% extends "layout.html" %} +{% block title %}hello {{ name }}!{% endblock %} +{% block body %} +

hello {{ name }}!

+{% endblock %} \ No newline at end of file diff --git a/example/views/index.html b/example/views/index.html new file mode 100644 index 00000000..cb403f32 --- /dev/null +++ b/example/views/index.html @@ -0,0 +1,6 @@ +{% extends "layout.html" %} +{% block title %}Jinja.Dart!{% endblock %} +{% block body %} +

hello Jinja.Dart!

+ hello Joe! +{% endblock %} \ No newline at end of file diff --git a/example/views/layout.html b/example/views/layout.html new file mode 100644 index 00000000..4f1f056e --- /dev/null +++ b/example/views/layout.html @@ -0,0 +1,10 @@ + + + + + {% block title %}{% endblock %} + + + {% block body %}{% endblock %} + + diff --git a/lib/angel_jinja.dart b/lib/angel_jinja.dart new file mode 100644 index 00000000..380a51b7 --- /dev/null +++ b/lib/angel_jinja.dart @@ -0,0 +1,53 @@ +import 'package:angel_framework/angel_framework.dart'; +import 'package:jinja/jinja.dart'; + +/// Configures an Angel server to use Jinja2 to render templates. +/// +/// By default, templates are loaded from the filesystem; +/// pass your own [createLoader] callback to override this. +/// +/// All options other than [createLoader] are passed to either [FileSystemLoader] +/// or [Environment]. +AngelConfigurer jinja({ + Iterable ext = const ['html'], + String path = 'lib/src/templates', + bool followLinks = true, + String stmtOpen = '{%', + String stmtClose = '%}', + String varOpen = '{{', + String varClose = '}}', + String commentOpen = '{#', + String commentClose = '#}', + defaultValue, + bool autoReload = true, + Map filters = const {}, + Map tests = const {}, + Loader Function() createLoader, +}) { + return (app) { + createLoader ??= () { + return FileSystemLoader( + ext: ext.toList(), + path: path, + followLinks: followLinks, + ); + }; + var env = Environment( + loader: createLoader(), + stmtOpen: stmtOpen, + stmtClose: stmtClose, + varOpen: varOpen, + varClose: varClose, + commentOpen: commentOpen, + commentClose: commentClose, + defaultValue: defaultValue, + autoReload: autoReload, + filters: filters, + tests: tests, + ); + + app.viewGenerator = (path, [values]) { + return env.getTemplate(path).render(values); + }; + }; +} diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 00000000..60a136cb --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,15 @@ +name: angel_jinja +description: Angel support for the Jinja2 templating engine, ported from Python to Dart. +version: 1.0.0-rc.0 +homepage: https://github.com/angel-dart/jinja +author: Tobe O +environment: + sdk: '>=2.0.0-dev <3.0.0' +dependencies: + angel_framework: ^2.0.0-alpha + jinja: ^0.0.4 +dev_dependencies: + angel_test: ^2.0.0 + path: ^1.0.0 + pedantic: ^1.0.0 + test: ^1.0.0