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

Cannot overwrite pre-existing key/value in preRender #9

Open
dtothefp opened this issue Dec 30, 2015 · 4 comments
Open

Cannot overwrite pre-existing key/value in preRender #9

dtothefp opened this issue Dec 30, 2015 · 4 comments

Comments

@dtothefp
Copy link

Demo repo here: https://github.com/dtothefp/assemble-yfm-nunj

"version": "0.8.0"
Essentially, overwritting pre-existing data on the file.data context does not seem to work in preRender hook

app.preRender(/\.(?:hbs|md|html)$/, (file, next) => {
  console.log('Append Pre-Render Data Before Merge', file.data);

  file.data = Object.assign({}, file.data, {
    title: 'From Pre-Render',
    custom_stuff: 'Custom stuff from Pre-Render'
  });

  console.log('Append Pre-Render Data After Merge', file.data);
  next();
});

if app.cache.data.title or file.data.title exists then the above doesn't work. I solve this by doing merge in onLoad

app.onLoad(/\.(?:hbs|md|html)$/, (file, next) => {
  matter.parse(file, (err, file) => {
    if (err) return next(err);

    _.assign(file.data, {title: 'blahhhh'});

    next(null, file);
  });
});

or by making a stream plugin

app.task('build', () => {
  return app.src('./templates/pages/**/*.html')
    .pipe(through.obj(function(file, enc, cb) {
      file.data = {title: 'blaahhhhh'}
      this.push(file);
      cb();
    }))
    .pipe(app.renderFile())
    .pipe(app.dest('dist'))
    .on('error', (err) => {
      console.error('Error [assemble]: build');
    })
    .on('data', (file) => {
      console.log('data', file.path);
    })
    .on('end', () => {
      console.log('ended');
    });
});
@assemblebot
Copy link

@dtothefp Thanks for the issue! If you're reporting a bug, please be sure to include:

  • The version of assemble you are using.
  • Your assemblefile.js (This can be in a gist)
  • The commandline output. (Screenshot or gist is fine)
  • What you expected to happen instead.

@jonschlinkert
Copy link
Member

@dtothefp inside node_modules in assemble-render-file try commenting out the code that creates the ctx object, then just pass locals to render. See if that works. If so we can update that lib. (was just talking to @doowb, he mentioned this)

@dtothefp
Copy link
Author

@jonschlinkert when I do that looks like locals is sometimes undefined which causes an error

        // create the context to pass to templates
        //var ctx = utils.merge({}, app.cache.data, locals, file.data);
        //ctx.engine = engine || ctx.engine;

        // render the file
        app.render(file, locals, function(err, res) { //...
/Users/davidfox-powell/dev/prerender-demo/node_modules/assemble-core/node_modules/templates/lib/utils/proto.js:105
  var engine = locals.engine || view.engine || opts.engine;
                     ^

TypeError: Cannot read property 'engine' of undefined

@jonschlinkert
Copy link
Member

you probably need to do something like if (!locals) locals = {}

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

3 participants