dustjs icon indicating copy to clipboard operation
dustjs copied to clipboard

There's no way to override the context value from an inline parameter.

Open SteffanDonal opened this issue 9 years ago • 5 comments

I've recently run into a case where I want to override some of my context to control the appearance of a partial.

The partial has a simple reference: {theme}, and I had attempted overriding it by adding the inline parameter to the partial include: {> mypartial theme="sky"}.

I can see why the decision was made to override this value from the context; It facilitates setting up default values when the context doesn't have the necessary data.

Can we consider adding the ability to allow the inline parameter to override the context? I had two approaches in mind:

  1. Add a decorator/extension to the inline parameter definition to say "I want this value to override the context."
  2. Add a filter/decorator to the reference inside the partial to say "I want to use the inline parameter value if it exists, then the context."

Thoughts?

SteffanDonal avatar Feb 26 '16 11:02 SteffanDonal

    if (!dust.isEmptyObject(params)) {
      partialContext = partialContext.clone();
      head = partialContext.pop();
      partialContext = partialContext.push(params)
                                     .push(head);
    }

Partial context gets inserted behind the context head. @jimmyhchan what's the legacy reason for this again?

Can you use a different name for the param that won't be shadowed by your context? themeOverride or something, and then check for themeOverride before using theme?

sethkinast avatar Mar 03 '16 19:03 sethkinast

https://github.com/linkedin/dustjs/issues/313 here we go.

I think we should probably just change this in Dust@3

sethkinast avatar Mar 03 '16 19:03 sethkinast

That was my initial resolution, just naming it different, however the resulting dust is ugly IMO.

Edit: Apologies for the open/close, misclicked.

SteffanDonal avatar Mar 03 '16 19:03 SteffanDonal

OK, more insight. I haven't read this part of the code in a bit.

The code for sections looks like this:

Push section params onto the stack If iterating over an array, grab the nth element of the array and push it onto the stack Render the body once Repeat

So partials were updated to use the same logic as sections, but I think this is just an oversight and inline params should clobber context params of the same name.

Definitely a major change though so we'd have to roll it up along with the other bc-break changes.

sethkinast avatar Mar 03 '16 20:03 sethkinast

However that prevents {.} from working properly because of traversal structure. Complex...

sethkinast avatar Mar 03 '16 20:03 sethkinast