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

Add support for parameters on inline partials/blocks #358

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/dust.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,13 @@ Chunk.prototype.notexists = function(elem, context, bodies) {
return this;
};

Chunk.prototype.block = function(elem, context, bodies) {
Chunk.prototype.block = function(elem, context, bodies, params) {
var body = bodies.block;

if (params) {
context = context.push(params);
}

if (elem) {
body = elem;
}
Expand Down
52 changes: 52 additions & 0 deletions test/jasmine-test/spec/coreTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,58 @@ var coreTests = [
context: { "val1" : "title", "val2" : "A", "obj" : { "name" : ["A", "B"] } },
expected: "AAA",
message: "should test blocks with dynamic key values as arrays"
},
{
name: "blocks with inline parameters",
source: [ '{<form}',
' <form action="{url}">',
' {+formBody/}',
' </form>',
'{/form}',
'',
'{+form url=dynamically_generated_url/}'].join("\n"),
context: { dynamically_generated_url: 'http://google.com/search'},
expected: '<form action="http://google.com/search"></form>',
message: "should test blocks with inline parameters"
},
{
name: "blocks with inline parameters that reference objects",
source: [ '{<formAction}',
' <form action="{url.value}">',
' {+formBody/}',
' </form>',
'{/formAction}',
'',
'{+formAction url=dynamically_generated_url/}'].join("\n"),
context: { "dynamically_generated_url": { "value": "http://google.com/search" } },
expected: '<form action="http://google.com/search"></form>',
message: "should test blocks with inline parameters"
},
{
name: "blocks with missing inline parameters",
source: [ '{<formAction}',
' <form action="{url}">',
' {+formBody/}',
' </form>',
'{/formAction}',
'',
'{+formAction url=refDoesntExist/}'].join("\n"),
context: {},
expected: '<form action=""></form>',
message: "should test blocks with inline parameters"
Copy link
Contributor

Choose a reason for hiding this comment

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

Add 2 unit tests.

One where the ref doesn't exist in the context, but you pass a static string for the param value.

The second where you reference include another template that has the blocks, and then you call that block in your source. You can include previously defined tests as partials by their name.

Copy link
Author

Choose a reason for hiding this comment

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

Ran in to a little snag w/ the second unit test, but, updated w/ something that I think get's close to what you're asking for... mind taking a look and letting me know?

Copy link
Contributor

Choose a reason for hiding this comment

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

So the problem may be how blocks and inline partials work today. @jimmyhchan and I were playing around with them, and they really just made no sense. However, I think your original PR is useful, so let's stick with that, and then revisit this issue more deeply later on.

Copy link
Author

Choose a reason for hiding this comment

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

Sounds like a plan. I'll revert the partial_with_blocks_and_overridable_params stuff, but, keep the blocks with a static string as an inline parameter unit test since I think it still makes sense...

},
{
name: "blocks with a static string as an inline parameter",
source: [ '{<formAction}',
' <form action="{url}">',
' {+formBody/}',
' </form>',
'{/formAction}',
'',
'{+formAction url="http://google.com/search"/}'].join("\n"),
context: {},
expected: '<form action="http://google.com/search"></form>',
message: "should test blocks with inline parameters that are static strings"
}
]
},
Expand Down