foundation / panini

A super simple flat file generator.
Other
592 stars 104 forks source link

Custom Global Panini Helper Variables #51

Closed EHackettTriCore closed 7 years ago

EHackettTriCore commented 8 years ago

I'm not sure if this is related to Site-aware data or not. What i'd like to do is define a site wide variable/s that get's passed down to custom helpers during the build process.

pipe(panini({
      root: 'src/pages',
      layouts: 'src/layouts',
      partials: 'src/partials',
      helpers: 'src/helpers',
      foo: 'bar' <--- Some custom variable
    }))

Example I want a custom helper that either outputs a variable or filler text for emails (variable for app generated emails from server, filler text for previewing during development process). My build process has a build for development/production variable that the helper needs to have access too.

It would be helpful if Panini expected an object or array for custom site wide data that get's define during the build process. Then yargs could be used to set the variables.

EHackettTriCore commented 8 years ago

I think this wold be relatively easy to update render.js to expect something like 'globals' or 'customs' and pass them down the line. Pull Request

chrisbloom7 commented 8 years ago

I'm looking for similar functionality. I'd like to be able to specify whether a particular flag was specified on the command line.

ghost commented 8 years ago

Did anyone got it running? When i just use the lines @EHackettTriCore put into the render.js and tried to set foo: 'bar', panini does not recognize it.

Where is my fault? Some example maybe @EHackettTriCore ? Is it even possible to access a custom variable/value (production: true) from within panini?

{{#if production}} output something {{else}} output something different {{/if}}

chrisbloom7 commented 8 years ago

@dmargreiter-rewe After applying @EHackettTriCore's patch, your config would need to look like this:

pipe(panini({
  root: 'src/pages',
  layouts: 'src/layouts',
  partials: 'src/partials',
  helpers: 'src/helpers',
  globals: { // <--- Some custom variable
    foo: 'bar'
  }
}))

Now you should have access to foo as data.root.globals.foo or whatever your path is at the point you want to reference it.

ghost commented 8 years ago

@chrisbloom7 Thanks for that. Already found out myself. Cheers.

gakimball commented 7 years ago

Hey there! I know this issue is pretty old. I'm looking over your pull request right now, and this feature seems to already be covered by Panini's data feature.

In your PR, a globals key can be added to the Handlebars data. However, by adding a JSON/YAML/JS file with the name globals, you can get the same result. Files added to your project's data/ folder are made available to all pages, partials, and layouts. The filename is used as the base variable name.

@dmargreiter-rewe, to your question, this should be possible in Panini 1.4, which was deployed yesterday. You can now use a Node module as a data source, making something like this possible:

// src/data/globals.js
module.exports = {
  production: process.env.NODE_ENV === 'production',
};

Then in your Handlebars template you can do:

{{#if globals.production}}
  <p>Production!</p>
{{else}}
  <p>Not production.</p>
{{/if}}
gakimball commented 7 years ago

Closing this for now as a non-issue, but if y'alls have any questions let me know :)

nicholaskang commented 7 years ago

@gakimball in your example of what to include in your Handlebars template, you need to remove the # before else.

gakimball commented 7 years ago

@nicholaskang Good catch, thank you :)

GGurbanov commented 4 years ago

SOLUTION

As @gakimball said, in order to pass custom variables to your templates/pages you have to add

// src/data/globals.js

module.exports = {
  foo: 'bar',
};

and then consume it like

// src/pages/basic.html

<h1>This is the passed variable - {{globals.foo}}</h1>

BUT you have to add data: 'src/data' to gulpfile.babel.js

// gulpfile.babel.js

function pages() {
  return gulp.src(['src/pages/**/*.html', '!src/pages/archive/**/*.html'])
    .pipe(panini({
      root: 'src/pages',
      layouts: 'src/layouts',
      partials: 'src/partials',
      helpers: 'src/helpers',
      data: 'src/data'
    }))
    .pipe(inky())
    .pipe(gulp.dest('dist'));
}