Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nunjucks custom filter is not working #380

Closed
2 tasks done
ashwin31 opened this issue Aug 4, 2023 · 1 comment
Closed
2 tasks done

nunjucks custom filter is not working #380

ashwin31 opened this issue Aug 4, 2023 · 1 comment

Comments

@ashwin31
Copy link

ashwin31 commented Aug 4, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.21.0

Plugin version

3.2.4

Node.js version

18.13.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

20.04

Description

i tried to implement custom filter for nunjucks templates and it resulted in error.

Steps to Reproduce

import path from 'path'
import AutoLoad from '@fastify/autoload'
import { fileURLToPath } from 'url'
import nunjucks from 'nunjucks';

import moment from 'moment';

var env = new nunjucks.Environment();

// const renderer = nunjucks.configure(join(__dirname, 'views'));
const templatesPath = path.join(process.cwd(), 'views'); // Path to your templates folder
const renderer = nunjucks.configure(templatesPath);


env.addFilter('dateformat', function(date) {
  return moment(date).format('MMMM Do YYYY, h:mm:ss a');
});

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

// Pass --options via CLI arguments in command to enable these options.
export const options = {}

export default async function (fastify, opts) {
  // Place here your custom code!

  // Do not touch the following lines
  
  // This loads all plugins defined in plugins
  // those should be support plugins that are reused
  // through your application
  fastify.register(AutoLoad, {
    dir: path.join(__dirname, 'plugins'),
    options: Object.assign({}, opts)
  })

  // This loads all plugins defined in routes
  // define your routes in one of these
  fastify.register(AutoLoad, {
    dir: path.join(__dirname, 'routes'),
    options: Object.assign({}, opts)
  })

  // Registering a view engine for Nunjucks
fastify.register(import("@fastify/view"), {
  engine: {
    nunjucks: nunjucks
  },
  includeViewExtension: true,
  templates: templatesPath
});


}

Expected Behavior

No response

@ashwin31
Copy link
Author

ashwin31 commented Aug 4, 2023

tried this too but no use

const renderer = nunjucks.configure(templatesPath);

renderer.addFilter('dateformat', function(date) {
  return moment(date).format('MMMM Do YYYY, h:mm:ss a');
});

@ashwin31 ashwin31 closed this as completed Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant