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

[Question]How to give data on templates you only include? #73

Closed
kud opened this issue Sep 3, 2019 · 1 comment

Comments

@kud
Copy link

commented Sep 3, 2019

Currently I'm doing this:

const PATHS = {
  srcFilesGlob: path.resolve(__dirname, "src/**/*"),
  pagesGlob: path.resolve(__dirname, "src/pages/**/*.njk"),
  partialsGlob: path.resolve(__dirname, "src/pages/**/**(.mjml).partial.njk"),

  rootDir: path.resolve(__dirname, "src"),
  componentsDir: path.resolve(__dirname, "src/components"),
  fontsDir: path.resolve(__dirname, "src/fonts"),
  layoutsDir: path.resolve(__dirname, "src/layouts"),
  pagesDir: path.resolve(__dirname, "src/pages"),
  stylesDir: path.resolve(__dirname, "src/styles"),

  buildDir: path.resolve(__dirname, "__build__"),
  outputDir: path.resolve(__dirname, "dist"),
}

return gulp
    .src([`${PATHS.pagesGlob}`, `!${PATHS.partialsGlob}`])
    .pipe(
      nunjucks({
        data: PATHS,
        path: [
          PATHS.componentsDir,
          PATHS.fontsDir,
          PATHS.layoutsDir,
          PATHS.pagesDir,
          PATHS.stylesDir,
        ],
        envOptions: {
          noCache: true,
          tags: {
            blockStart: "<%",
            blockEnd: "%>",
            variableStart: "<$",
            variableEnd: "$>",
            commentStart: "<#",
            commentEnd: "#>",
          },
        },
      }),
    )
    .pipe(gulp.dest(PATHS.buildDir))

It compiles well the files in pagesGlob but also excludes partialsGlob in order to have only pages as final files, not partials, in buildDir.

In pages, I can do import or include of partials in nunjucks, it works well.

However, as partialsGlob is exclude, data: PATHS, isn't given to those files, which means I can't use variables like componentsDir.

Do you have any idea to make it work?

Thank you.

@kud

This comment has been minimized.

Copy link
Author

commented Sep 3, 2019

I put 'em into env.addGlobal instead of data!

Youuhouu

        manageEnv: (env) => {
          env.addGlobal("componentsDir", PATHS.componentsDir)
          env.addGlobal("layoutsDir", PATHS.layoutsDir)
          env.addGlobal("pagesDir", PATHS.pagesDir)
          env.addGlobal("stylesDir", PATHS.stylesDir)
        },

@kud kud closed this Sep 3, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.