Skip to content

RFC: helpers/dynamicComponent.js modification to support path literal as string input #396

Description

@34638a

Gday @jairo-bc ,

Hope you've been well mate, before I code up a PR later this month I was hoping to grab some thoughts on the helpers/dynamicComponent.js function in regards to possibly expanding the implementation to support a path literal.
https://github.com/bigcommerce/paper-handlebars/blob/master/helpers/dynamicComponent.js

Currently the implementation is fine no doubt but I was wondering if there would be any pushback in expanding the function to not necessarily require a partial context element only if there is a completed path to a partial.

In theory the implementation would be as follows, (untested):

'use strict';

const Path = require('path');

const factory = globals => {
    return function(path) {

        // -- Move Guard to later --
        // if (!this['partial']) {
        //     return;
        // }

        // prevent access to __proto__
        // or any hidden object properties
        let no_prototype_values_literal_path = path.replace('__', '');


        // We don't want a slash as a prefix
        if (no_prototype_values_literal_path[0] === '/') {
            no_prototype_values_literal_path = no_prototype_values_literal_path.slice(1); // update to slice()
        }

        // -- Guard here instead --
        if (!this['partial']) {
            // Add New logic to check if the path is a resolved partial and return if valid
            if (globals.handlebars.partials[no_prototype_values_literal_path] && Object.hasOwnProperty.call(globals.handlebars.partials, no_prototype_values_literal_path)) {
                return globals.handlebars.partials[no_prototype_values_literal_path](this);
            }
            
            // bail not a valid partial path literal.
            return;
        }

        // Normal behaviour as per current implementation
        
        const path = Path.join(no_prototype_values_literal_path, this['partial']);
        if (globals.handlebars.partials[path] && Object.hasOwnProperty.call(globals.handlebars.partials, path)) {
            return globals.handlebars.partials[path](this);
        }
    };
};

module.exports = [{
    name: 'dynamicComponent',
    factory: factory,
}];

I can open a full PR and create the proper implementation later this / next month if it's worth pursuing.

Cheers,

  • Jordan, Online Workwear.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions