Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/devanandb/adonis-nunjucks
Nunjucks VIewProvider for AdonisJs
https://github.com/devanandb/adonis-nunjucks
adonis-framework adonisjs nunjucks provider templating
Last synced: about 1 month ago
JSON representation
Nunjucks VIewProvider for AdonisJs
- Host: GitHub
- URL: https://github.com/devanandb/adonis-nunjucks
- Owner: devanandb
- License: mit
- Created: 2018-11-10T23:29:59.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-04-07T05:21:01.000Z (over 4 years ago)
- Last Synced: 2024-09-28T17:04:42.017Z (3 months ago)
- Topics: adonis-framework, adonisjs, nunjucks, provider, templating
- Language: JavaScript
- Size: 86.9 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Adonis Nunjucks
[Nunjucks](https://mozilla.github.io/nunjucks/) templating provider for AdonisJs framework version 4.
## Installation
In order to use adonis-nunjucks
```
npm install adonis-nunjucks --save
```Once you have installed the provider from [npm](https://npmjs.org/packages/adonis-nunjucks), make sure that the ViewProvider is registered as a provider inside start/app.js file.
```javascript
const providers = [
'adonis-nunjucks/providers/ViewProvider'
]
```Make sure the default edge provider (`@adonisjs/framework/providers/ViewProvider`) is not registered as they will conflict with each other.
#### Compatibility
*This package has been rebuilt for Adonis 4 and is incompatible with Adonis 3 and earlier.
For Adonis v3 install the previous version (3.01) with :*```
npm install adonis-nunjucks --save
```## Basic Usage
Let’s start with the basic example of saying `Hello world` by rendering a nunjucks template. All of the views are stored inside `resources/views` directory and end with `.html` extension.
Create a nunjucks template at `resources/views/hello.html`. You can use an adonis/ace command to create the view.
```sh
adonis make:nunjucks home✔ create resources/views/home.html
```Now let's create a route that renders it:
```javascript
Route.get('/', ({ view }) => {
return view.render('home')
})
```The view.render method takes the relative path to the view file. There is no need to type .html extension.
## View Methods
These methods are available on the view context object in controllers and middleware.
#### view.share(locals)
Share variables as a local with this template context.| Param | Type | Description |
| --- | --- | --- |
| locals |Object
| Key value pairs |###### *Example*
Quite often you want to share request specific values with your views, this can be done in middleware or controllers by passing an object to the share method.
```javascript
class SomeMiddleware {
async handle ({ view }, next) {
view.share({
apiVersion: request.input('version')
})await next()
}
}
```Inside your views, you can access it like any other variable
```nunjucks
{{apiVersion}}
```#### view.render(template, locals) ⇒
String
Render a nunjucks template**Returns**:
String
- HTML rendered output| Param | Type | Description |
| --- | --- | --- |
| template |String
| View file (.html extension not required) |
| locals |Object
| Variables to be passed to the view |#### view.renderString(string, locals) ⇒
String
Render a string of nunjucks**Returns**:
String
- HTML rendered output| Param | Type | Description |
| --- | --- | --- |
| string |String
| String to be rendered |
| locals |Object
| Variables to be passed to the view |## View Helpers
A number of global methods and contextual helpers are injected into all views.
### Request
All views have access to the current request object, and you can call request methods inside your templates as well.
```nunjucks
The request URL is {% request.url() %}
```Also, there is a direct helper to get the URL.
```nunjucks
The request URL is {% url %}
```### More documentation coming soon.
## License
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fdevanandb%2Fadonis-nunjucks.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fdevanandb%2Fadonis-nunjucks?ref=badge_large)[npm-badge]: https://img.shields.io/npm/v/adonis-nunjucks.svg?maxAge=30
[npm-link]:https://npmjs.com/package/adonis-nunjucks## License
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fdevanandb%2Fadonis-nunjucks.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fdevanandb%2Fadonis-nunjucks?ref=badge_large)