yeojz / metalsmith-react-templates

A metalsmith plugin to render files using React / Preact / JSX based templates.
MIT License
92 stars 17 forks source link

Add an option to preserve props for use in downstream metalsmith tasks #28

Closed pumpikano closed 8 years ago

pumpikano commented 8 years ago

Adds an option to pass along the props object provided to the template with the preserveProps option. This makes it much easier to use the props downstream without essentially reconstructing it.

My current use case is to store the props object in the script bundle via webpack and use it to mount the template component in the client.

yeojz commented 8 years ago

@pumpikano

All items in the props are available on the file object. Wouldn't this be just duplicating data into the metalsmith file object?

pumpikano commented 8 years ago

@yeojz

Yes, that is true, but currently the only way that I can see of getting the props used in the plugin is to use preserve: true and then later reconstruct the props object (recreating filename and metadata keys) and recreate content = files[file].rawContent.toString(). It is possible, but indirect. Did I miss something?

But now I'm thinking that this isn't the best either, and perhaps providing the option for the props object to be constructed before the plugin is the best. Perhaps:

.use(reactTemplate({
    isStatic: false,
    directory: 'layouts',
    noConflict: false,
    baseFile: 'base.html',
    propsKey: 'props'
})

where propsKey names the key on the file object to be used as props, defaulting to the current scheme when undefined. This gives explicit control in a simpler way I think.

yeojz commented 8 years ago

@pumpikano That sounds like a better solution and gives much better flexibility.