Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Issue #8 Support cleaner way to pass arguments to partials #13

Merged
merged 5 commits into from

3 participants

@jimmyhchan
Owner

Please see the pull request. Adding parameters was pretty straightforward but it's odd what context to give it.

Currently only sections create new contexts. When you add parameters in sections, Dust puts the parameters context on top, followed by the new context. That way, the current context inside a section becomes the new context and the parameters context become the first head of the tail.

I'm mimicking this behavior in the partials parameters. To keep it so that partials do not create a new context (that is, current context should not change but parameters need to be available) I push the parameter context between the current head and the first head of the tail.

@rragan
Owner

Wouldn't it be more regular with the rest of the language constructs to have the partial method take chunk, context, blocks, params since you are adding params? It seems like there could well be useful partials that could consume blocks a part of what they do.

@vybs
Collaborator

Woot! Thanks a lot Jimmy!

Some more test cases I may have in mind, will reply to this soon!

@vybs
Collaborator

@rragan, a partial within itself can have any conten, block etc,

Can you elaborate with an example use case?

@rragan
Owner

We have a custom JSP tag that I would conisder as a partial with a block. It is call cdiv for "conditionaL DIV". If it has no body text it generates nothing. If it has a body it wraps it in the div. We use this in layouts where the user may or may not supply parts of the layouts.

@vybs
Collaborator

We decided to use @ tags in such scenarios. We do have similar cases, @ module

   /** 
    @author Hans Brough 
    * Used to standardize HTML containers. 
    * @method module
    * @param {Object} params a configuration object created from attributes set in the template - see below for details. 
   */
  dust.helpers.module = function(chunk, context, bodies, params){
    var hasHdr,hdrTag,id,modClass,modType,title;
     if( params ){
        // init
         hasHdr = (params.hasHdr == undefined || params.hasHdr.toLowerCase() === 'true');
         .....
        //starting container tags
        chunk.write("<div class='module mod-" + modType + modClass +"' id='" + id + "'>");
        if( hasHdr ){
          chunk.write("<div class='header'><" + hdrTag + ">" + title + "</" + hdrTag + "></div>");
        }
         chunk.write("<div class='content'>");

          // body itself can be a partial
         chunk.render( bodies.block, context);

         // closing containe tags
        chunk.write("</div></div>");
      }
     return chunk;
   };
@vybs
Collaborator

@jimmyhchan , i dont see a option to merge this in?

@jimmyhchan
Owner
@vybs
Collaborator

It says this for me:

This pull request cannot be automatically merged.

@jimmyhchan
Owner

I need to get my stuff synced with the current head... let me get that fixed.

@vybs
Collaborator

No rush.

We found a good use case to use this feature.

The fs embed will will be partial and take parameters.

fs_embed url="{uri}" template="{template}"

So i'd like to get this in via git svn this week.

@jimmyhchan
Owner

@vybs I've updated my fork. The pull request is now clean.

@vybs vybs commented on the diff
test/jasmine-test/spec/examples.js
@@ -301,6 +301,19 @@ var dustExamples = [
message: "should test partial context"
},
{
+ name: "partial_param",
+ source: '{>replace name=n count="{c}"/}',
+ context: { n: "Mick", c: 30 },
+ expected: "Hello Mick! You have 30 new messages.",
+ message: "should test partial params"
+ },
+ {name: "partial_array_param",
+ source: '{#n}{>replace name=. count="30"/}{@sep} {/sep}{/n}',
+ context: { n: ["Mick", "Tom", "Bob"] },
+ expected: "Hello Mick! You have 30 new messages. Hello Tom! You have 30 new messages. Hello Bob! You have 30 new messages.",
+ message: "should test partial params using an array"
+ },
@vybs Collaborator
vybs added a note

lgtm.

Few more unit tests

  1. if there is context override on the partial

partial:ctx name="joe"

and ctx also has name ( the expected behaviour ?) can we add a unit test for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@vybs vybs commented on the diff
test/jasmine-test/spec/examples.js
@@ -301,6 +301,19 @@ var dustExamples = [
message: "should test partial context"
},
{
+ name: "partial_param",
@vybs Collaborator
vybs added a note

partial:ctx

also please add unit tests to support name=name ( which means it will not walk up the tree and look for name in the ctx only )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@vybs vybs merged commit a82d9f8 into linkedin:master
@vybs vybs commented on the diff
test/jasmine-test/spec/examples.js
@@ -301,6 +301,19 @@ var dustExamples = [
message: "should test partial context"
},
{
+ name: "partial_param",
+ source: '{>replace name=n count="{c}"/}',
+ context: { n: "Mick", c: 30 },
+ expected: "Hello Mick! You have 30 new messages.",
+ message: "should test partial params"
+ },
+ {name: "partial_array_param",
+ source: '{#n}{>replace name=. count="30"/}{@sep} {/sep}{/n}',
@vybs Collaborator
vybs added a note

@jimmyhchan

i forget this

{>"tl/apps/demo/embed/base_embed" test="FOOOO"/}
{>"tl/apps/demo/embed/base_embed" test="BARRR"/}

{>"tl/apps/demo/embed/base_embed" test=foo/}
{>"tl/apps/demo/embed/base_embed" test=bar/}

the above output for test is always the value for "bar" in the JSON

is this expected?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 24, 2012
  1. @jimmyhchan

    add params to partials

    jimmyhchan authored
  2. @jimmyhchan

    typo in jasmine test

    jimmyhchan authored
Commits on Apr 25, 2012
  1. @jimmyhchan
Commits on May 3, 2012
  1. @jimmyhchan
Commits on May 9, 2012
  1. @jimmyhchan

    merging from upstream

    jimmyhchan authored
This page is out of date. Refresh to see the latest.
View
3  lib/compiler.js
@@ -247,7 +247,8 @@ dust.nodes = {
partial: function(context, node) {
return ".partial("
+ dust.compileNode(context, node[1])
- + "," + dust.compileNode(context, node[2]) + ")";
+ + "," + dust.compileNode(context, node[2])
+ + "," + dust.compileNode(context, node[3]) + ")";
},
context: function(context, node) {
View
12 lib/dust.js
@@ -404,7 +404,17 @@ Chunk.prototype.block = function(elem, context, bodies) {
return this;
};
-Chunk.prototype.partial = function(elem, context) {
+Chunk.prototype.partial = function(elem, context, params) {
+ var ctx = context.stack, tempHead = ctx.head;
+ if (params){
+ //put the params context second to match what section does. {.} matches the current context without parameters
+ //remove head
+ context = context.rebase(ctx.tail);
+ //put params on
+ context = context.push(params);
+ //reattach the head
+ context = context.push(tempHead);
+ }
if (typeof elem === "function") {
return this.capture(elem, context, function(name, chunk) {
dust.load(name, chunk, context).end();
View
32 lib/parser.js
@@ -808,7 +808,7 @@ var parser = (function(){
return cachedResult.result;
}
- var result0, result1, result2, result3, result4, result5;
+ var result0, result1, result2, result3, result4, result5, result6;
var pos0, pos1, pos2;
reportFailures++;
@@ -840,19 +840,25 @@ var parser = (function(){
if (result2 !== null) {
result3 = parse_context();
if (result3 !== null) {
- if (input.charCodeAt(pos) === 47) {
- result4 = "/";
- pos++;
- } else {
- result4 = null;
- if (reportFailures === 0) {
- matchFailed("\"/\"");
- }
- }
+ result4 = parse_params();
if (result4 !== null) {
- result5 = parse_rd();
+ if (input.charCodeAt(pos) === 47) {
+ result5 = "/";
+ pos++;
+ } else {
+ result5 = null;
+ if (reportFailures === 0) {
+ matchFailed("\"/\"");
+ }
+ }
if (result5 !== null) {
- result0 = [result0, result1, result2, result3, result4, result5];
+ result6 = parse_rd();
+ if (result6 !== null) {
+ result0 = [result0, result1, result2, result3, result4, result5, result6];
+ } else {
+ result0 = null;
+ pos = pos1;
+ }
} else {
result0 = null;
pos = pos1;
@@ -878,7 +884,7 @@ var parser = (function(){
pos = pos1;
}
if (result0 !== null) {
- result0 = (function(offset, n, c) { return ["partial", n, c] })(pos0, result0[2], result0[3]);
+ result0 = (function(offset, n, c, p) { return ["partial", n, c, p] })(pos0, result0[2], result0[3], result0[4]);
}
if (result0 === null) {
pos = pos0;
View
6 src/dust.pegjs
@@ -45,8 +45,8 @@ reference "reference"
{ return ["reference", n, f] }
partial "partial"
- = ld ">" n:(k:key {return ["literal", k]} / inline) c:context "/" rd
- { return ["partial", n, c] }
+ = ld ">" n:(k:key {return ["literal", k]} / inline) c:context p:params "/" rd
+ { return ["partial", n, c, p] }
filters "filters"
= f:("|" n:key {return n})*
@@ -114,4 +114,4 @@ eol
/ "\u2029"
ws
- = [\t\v\f \u00A0\uFEFF]
+ = [\t\v\f \u00A0\uFEFF]
View
14 test/examples.js
@@ -257,6 +257,18 @@ exports.dustExamples = [
expected: "Hello Mick! You have 30 new messages."
},
{
+ name: "partial_param",
+ source: '{>replace name=n count="{c}"/}',
+ context: { n: "Mick", c: 30 },
+ expected: "Hello Mick! You have 30 new messages."
+ },
+ {
+ name: "partial_array_param",
+ source: '{#n}{>replace name=. count="30"/}{@sep} {/sep}{/n}',
+ context: { n: ["Mick", "Tom", "Bob"] },
+ expected: "Hello Mick! You have 30 new messages. Hello Tom! You have 30 new messages. Hello Bob! You have 30 new messages."
+ },
+ {
name: "base_template",
source: "Start{~n}\n" +
"{+title}\n" +
@@ -315,4 +327,4 @@ exports.dustExamples = [
}
];
-})(typeof exports !== "undefined" ? exports : window);
+})(typeof exports !== "undefined" ? exports : window);
View
15 test/jasmine-test/spec/examples.js
@@ -301,6 +301,19 @@ var dustExamples = [
message: "should test partial context"
},
{
+ name: "partial_param",
@vybs Collaborator
vybs added a note

partial:ctx

also please add unit tests to support name=name ( which means it will not walk up the tree and look for name in the ctx only )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ source: '{>replace name=n count="{c}"/}',
+ context: { n: "Mick", c: 30 },
+ expected: "Hello Mick! You have 30 new messages.",
+ message: "should test partial params"
+ },
+ {name: "partial_array_param",
+ source: '{#n}{>replace name=. count="30"/}{@sep} {/sep}{/n}',
@vybs Collaborator
vybs added a note

@jimmyhchan

i forget this

{>"tl/apps/demo/embed/base_embed" test="FOOOO"/}
{>"tl/apps/demo/embed/base_embed" test="BARRR"/}

{>"tl/apps/demo/embed/base_embed" test=foo/}
{>"tl/apps/demo/embed/base_embed" test=bar/}

the above output for test is always the value for "bar" in the JSON

is this expected?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ context: { n: ["Mick", "Tom", "Bob"] },
+ expected: "Hello Mick! You have 30 new messages. Hello Tom! You have 30 new messages. Hello Bob! You have 30 new messages.",
+ message: "should test partial params using an array"
+ },
@vybs Collaborator
vybs added a note

lgtm.

Few more unit tests

  1. if there is context override on the partial

partial:ctx name="joe"

and ctx also has name ( the expected behaviour ?) can we add a unit test for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {
name: "base_template",
source: "Start{~n}\n" +
"{+title}\n" +
@@ -403,4 +416,4 @@ if (typeof module !== "undefined" && typeof require !== "undefined") {
module.exports = dustExamples; // We're on node.js
} else {
window.dustExamples = dustExamples; // We're on the browser
-}
+}
Something went wrong with that request. Please try again.