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

Programatically call taglibs #14

Closed
peterdotjs opened this issue Jan 9, 2015 · 3 comments
Closed

Programatically call taglibs #14

peterdotjs opened this issue Jan 9, 2015 · 3 comments

Comments

@peterdotjs
Copy link

Is it possible to programmatically call taglibs?

Currently we can do the following when including template fragments:

<include template="src/modules/${module.alias}.marko"/>

I can include a different template based on the value of "module.alias".

I'd like to be able to have the same logic with taglibs. Something like this:

<var name="path" value="data.type" />
<app-test-${path} attr1="${data.attr1}" attr2="${data.attr2}" attr2="${data.attr2}"/>

Currently I can do the following:

<if test="data.type === "a">
    <app-test-a attr1="${data.attr1}" attr2="${data.attr2}" attr2="${data.attr2}"/>
</if>
<else-if test="data.type === "b">
    <app-test-b attr1="${data.attr1}" attr2="${data.attr2}" attr2="${data.attr2}"/>
</else-if>
<else>
    <app-test-c attr1="${data.attr1}" attr2="${data.attr2}" attr2="${data.attr2}"/>
</else>

This would work but I'd like to avoid having to copy over the same parameter code.

@patrick-steele-idem
Copy link
Contributor

Custom tags are resolved to a "renderer" module at compile-time so it is not possible to support something like <app-test-${path} ...>. However, a custom tag is mapped directly to a renderer and you can invoke the renderer directly to have the HTML be written to the output stream. One option is to create a custom tag that delegates rendering to the appropriate renderer based on some internal logic. Something like the following:

src/components/app-test/renderer.js:

var rendererA = require('./variant-A/renderer');
var rendererB = require('./variant-B/renderer');
var rendererC = require('./variant-C/renderer');

module.exports = function render(input, out) {
    if (input.type === 'A') {
        rendererA(input, out);
    } else if (input.type === 'B') {
        rendererB(input, out);
    } else if (input.type === 'C') {
        rendererC(input, out);
    }
}

This nice thing about that approach is that to the outside world there is only a single tag, but internally the custom tag renderer can delegate to the appropriate implementation.

Let us know if that works or doesn't work for you.

@patrick-steele-idem
Copy link
Contributor

ping.

Can you please let me know if the proposed solution meets your needs so that I can close this issue or keep it open? Thanks.

@peterdotjs
Copy link
Author

This will work. Thanks.

patrick-steele-idem pushed a commit that referenced this issue Nov 2, 2016
…tialization even if document is not ready
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

2 participants