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

Fix context.templateName for directly loaded templates being used as partials #368

Merged
merged 5 commits into from
Nov 28, 2013

Conversation

prashn64
Copy link
Contributor

Also change context.templateName to context.__templateName, since it's strange to not be allowed to use templateName in your json model.

The other way of doing this is to set it on the ctx object inside the compiled dust body_0. If that is preferred, I'll change this PR.

…mplateNames on directly loaded templates used as partials
@@ -705,6 +705,7 @@ function getGlobal(){
var partialChunk;
if (typeof elem === 'function') {
partialChunk = this.capture(elem, partialContext, function(name, chunk) {
partialContext.__templateName = partialContext.__templateName || name;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the meat of the PR.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought name in this case was typically out: as in, the captured output from calling elem. Should that be used as the __templateName?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1
Looking at the captureOutput implementation it's parameters are out and chunk. How does this fix the issues?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add a few unit tests to look into the issue deeper.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the elem passed to this partial function is always only in reference to the name of the partiall, so this.capture will invoke the callback with out as the name, which is always actually the template name. I looked through very complex templates to confirm this, and tried to make a unit test to somewhat emulate that.

@smfoote
Copy link
Contributor

smfoote commented Nov 21, 2013

Since the improved release processed mentioned in #361 has not yet been implemented, please run make dust and bump the version.

@@ -219,7 +219,7 @@ function getGlobal(){
this.stack = stack;
this.global = global;
this.blocks = blocks;
this.templateName = templateName;
this.__templateName = templateName;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC we've used templateName and __templateName__. Perhaps we should avoid a third option? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, decided to go with adding a getTemplateName function to Context, so we can go through as many name changes as we want.

@jimmyhchan
Copy link
Contributor

-1 for making people reference a dunderVariable. Suggest adding a context.templateName() method which returns you the current template's name. We should be able to move __template as needed even hiding it in the stack without having to break a very useful API

@prashn64
Copy link
Contributor Author

@jimmyhchan completely agreed, since we've already gone through a lot of variations, so I added that function to the most recent commit.

@@ -270,7 +270,7 @@ function getGlobal(){
var ctx = this.stack,
i = 1,
value, first, len, ctxThis;
dust.log('Searching for reference [{' + down.join('.') + '}] in template [' + this.templateName + ']', DEBUG);
dust.log('Searching for reference [{' + down.join('.') + '}] in template [' + this.__template_name__ + ']', DEBUG);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't all of these use the getTemplateName method?

@brikis98
Copy link

LGTM, but the code should probably use getTemplateName internally as well.

this.stack = stack;
this.global = global;
this.blocks = blocks;
this.__template_name__ = templateName;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to have this __?

this.templateName and prototype.getTemplateName seem good enough.

like stack, global and blocks this is an internally stored item which is consistent internally. Using context.* directly is not documented. Only the context.* methods are documented.

I'm not a fan of the inconsistency. Perhaps having _stack, _global, '_blocks` but that's a back compat issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. As we discussed, I'll revert template_name to templateName

@kate2753
Copy link
Contributor

Looks good to me. Thanks for additional unit tests!

jimmyhchan added a commit that referenced this pull request Nov 28, 2013
Add context.getTemplateName. This method now correctly returns the template name even for directly loaded templates being used as partials. For end users please use this api for getting the template name instead of ctx.templatename
@jimmyhchan jimmyhchan merged commit 5daf0f1 into linkedin:master Nov 28, 2013
@rragan
Copy link
Contributor

rragan commented Dec 17, 2013

I’m updating the Wiki to cover 2.2.x versions. At 2.0.x we added {templateName} to global and then moved it to context. Now that seems to be gone. I guess this is a backward incompatible change. I see the getTemplateName method on Context but how might a user template wanting to display the template name do this now without dropping down into JS?

@kate2753
Copy link
Contributor

Since templateName variable is no longer on global or part of head\tail of context I don't think user template can access templateName.

Issue #385 created to discuss this

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

Successfully merging this pull request may close these issues.

6 participants