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

defineRenderer for marko #344

Closed
jpattishall opened this issue Jul 29, 2016 · 3 comments
Closed

defineRenderer for marko #344

jpattishall opened this issue Jul 29, 2016 · 3 comments
Assignees
Labels
type:feature A feature request

Comments

@jpattishall
Copy link

Description

Currently marko-widgets allows to define a UI component's renderer as require('marko-widgets').defineRenderer({...});. I would like to propose a similar, but simpler:

require('marko').defineRenderer({...});

Context

Building our project, we've defined custom taglibs that export the simplest "components" to more complex components. For simple componets (server-rendered only, no state, little to no client behavior), our approach to custom taglibs has been to export in index.js a function that takes an input and then calls template.render.

module.exports = function(input, out) {
    template.render(input, out);
}

Unlike defineRenderer, this does not export a render function similar to defineRenderer. We could set exports.render or use createRenderFunc from raptor-renderer, however I would like to propose a defineRenderer in marko, with similar (but much simpler) life cycle events.

module.exports = require('marko').defineRenderer({
    template: require('./template.marko'),
    getTemplateData: function (input) {
        input.time = Date.now();
        return input;
    }
});

The added benefit (and why this beats exports.render or createRenderFunc) is that if the simple component takes on a more, complex role, adding -widgets + any extra lifecycle event methods is straightforward and painless.

Thanks!

@yomed
Copy link
Contributor

yomed commented Jul 29, 2016

👍 I always thought this API difference between marko and marko-widgets was a bit strange.

@pcanella
Copy link
Contributor

👍 totally agree here.

@patrick-steele-idem
Copy link
Contributor

Actual usage will be the following:

var defineRenderer = require('marko/defineRenderer');

module.exports = defineRenderer({
    template: require('./template.marko'),
    getTemplateData: function(input) {
        var firstName = input.firstName;
        var lastName = input.lastName;

        return {
            fullName: firstName + ' ' + lastName
        };
    }
})

NOTE: defineRenderer should be required as a sub-module (require('marko/defineRenderer')). This was done to avoid adding any extra weight to the main marko runtime.

patrick-steele-idem added a commit that referenced this issue Aug 3, 2016
Fixes #344 - defineRenderer for marko
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature A feature request
Projects
None yet
Development

No branches or pull requests

4 participants