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

Partial parameters #410

Closed
tomalex0 opened this issue Jan 17, 2013 · 7 comments
Closed

Partial parameters #410

tomalex0 opened this issue Jan 17, 2013 · 7 comments
Labels
Milestone

Comments

@tomalex0
Copy link

I'm trying to pass parameter from partials and based on that value i wan to do some condition check. This is sample implementation i'm trying to do.

Header Template, header.html

{{#if isleft}} {{> leftbtn}} {{/if}} {{title}} {{#if isright}} {{> rightbtn}} {{/if}}
Handlebars.registerPartial("header",header.html);
Handlebars.registerPartial("leftbtn", "<div>Left</div>");
Handlebars.registerPartial("rightbtn",'<div>Right</div>');

First Approach Is this possible?

  
 newpage1  {{> header {isleft:1,isright : 1} }}
 newpage2  {{> header }}  

Page1 Template, page1.html

 {{> header }}  
 <div>Some Content</div>

Second Approach This worked fine for me

var newpage1  = Handlebars.compile('page1.html')( {isleft:1,isright : 1}) shows both left and right
var newpage2  = Handlebars.compile('page1.html')()  does not shows left and right

Please let me know if first approach is possible and which one is better.

@piercemoore
Copy link

I see where your train of thought is going. My understanding of partials leads me to believe that the partials take their data from the parent context that called them, but I could be wrong. But simple conditionals remove the need to do all that craziness.

Page.html

var page1 = {
    title : "User Profile",
    isLeft : false,
    isRight : false,
    user : {
        name : "Pierce Moore",
        friends : [
            { name : "Nancy", age : 28 },
            { name : "Bobby", age : 25 }
        ]
    }
};

var page2 = {
    title : "Administrator",
    isLeft : true,
    isRight : true,
    user : {
        name : "Pierce Moore",
        friends : [
            { name : "Nancy", age : 28 },
            { name : "Bobby", age : 25 }
        ]
    }
};

$(function() {
    var tpl = Handlebars.compile( templateSelector );
    $("#page1").html(tpl(page1));
    $("#page2").html(tpl(page2));
});

Template.hb

{{#if isLeft}}
    <!-- Left side content -->
{{/if}}

{{#if user}}
    <p>Name: {{user.name}}</p>
    <p>Friends</p>
    <ul>
        {{#each user.friends}}
            <li>Name: {{name}}, age: {{age}}</li>
        {{/each}}
    </ul>
{{/if}}

{{#if isRight}}
    <!-- Right side content -->
{{/if}}

@kpdecker
Copy link
Collaborator

This is going to have the same resolution as #182. My plan is to add support for this syntax {{> foo bar=bat}}.

@jfbrennan
Copy link

kpdecker when will {{> foo bar=bat}} be released? need that feature bad!

@kpdecker
Copy link
Collaborator

This will land in 2.0. I hope that there will be a prerelease of 2.0 in the
next few weeks, followed by a formal release if no one has major issues
with the upgrade.

On Mon, Jan 27, 2014 at 1:58 PM, Jordan [email protected] wrote:

kpdecker when will {{> foo bar=bat}} be released? need that feature bad!


Reply to this email directly or view it on GitHubhttps://github.com//issues/410#issuecomment-33414217
.

@nathagey
Copy link

I'm not sure if your illustration {{> foo bar=bat}} made it into the release, and if it covers what I'm trying to do. I want to pass a configuration object along with a context object.

{{> myPartial this ../conf}}

Is this possible? Or would there be a way to merge the two objects to make a single context?

@nathagey
Copy link

Using a helper worked for this. #182 (comment)

@kpdecker
Copy link
Collaborator

@nathagey you need to be on the 2.x codeline for the native support. That's in alpha right now and not available on all channels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants