Skip to content
This repository was archived by the owner on Oct 6, 2023. It is now read-only.
This repository was archived by the owner on Oct 6, 2023. It is now read-only.

Pass in context to lambda functions? #13

@dswitzer

Description

@dswitzer

Right now the lambda function only retrieve the template code as an argument.

However, I started thinking that it would make some sense to pass in a reference to the "context" item as an argument as well, so that you could potentially cache the results of the function call by replacing the context on-the-fly.

This obviously doesn't always make sense, but I have some situations where I have some variables I want to do as a lambda because the lookup expense is costly and they're not always used. By passing in a reference to the context item, I could change the context to point to the rendered string (instead of the function.)

Does anyone else think it's worth adding?

Here's a little proof of concept showing the basic idea would work:

// function to show off lambda
function cacheTest(context, item){
    var results = createUUID();
    // here we cache the results in the original context object
    context[item] = results;
    return results;
}

// a sample context object
context = {test=cacheTest};

for( i=1; i <= 10; i=i+1 ){
    // this logic is handled by the get() method
    if( isCustomFunction(context.test) ){
        x = context.test(context, "test");
    } else {
        x = context.test;
    }
    // show the results
    writeOutput("<div>#i# = #x#</div>");
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions