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

Custom partials #1

Closed
waba opened this issue Sep 17, 2010 · 5 comments
Closed

Custom partials #1

waba opened this issue Sep 17, 2010 · 5 comments
Labels

Comments

@waba
Copy link

waba commented Sep 17, 2010

Is there a way I can provide and manipulate my own partials, not using the mechanism to find them in a file on disk? E.g., I'd really like

Template::Mustache->render('{{foo}} {{>bar}}',
                                           { foo => 'a' },
                                           {bar => 'string'})

(or something similar) to produce 'a string'. Thanks - Bart

@pvande
Copy link
Collaborator

pvande commented Sep 17, 2010

I'll look into this when I start pushing cleanup for Template::Mustache in a week or two.

Thanks!

@pvande
Copy link
Collaborator

pvande commented Sep 17, 2010

The closest recommendation I can make at this point is less direct, but functional:

package MustacheView;
use base 'Template::Mustache';
sub partial {
    my ($self, $name) = @_;
    my $partials = { bar => 'string' };
    return $partials->{$name} || $self->SUPER($name);
}

package main;
MustacheView->render('{{foo}} {{>bar}}', { foo => 'a' });

I'm not sure if that's as concise as you'd like, though...

@waba
Copy link
Author

waba commented Sep 17, 2010

Thanks, your solution is certainly helpful ... I'm trying to use some templates that worked with Mustache.js, to work with this module. In Mustache.js, using a partial with name foo also looks up the name foo in the current context, and if found, uses the object found as the context of the partial. By using the same partial under with different names, one can use it in different contexts, within the same template.

@pvande
Copy link
Collaborator

pvande commented Sep 17, 2010

Interesting... I've admittedly been out of the Mustache scene of late, so I'm not entirely sure what "correct" behavior is any more -- and Template::Mustache likely reflects an older standard.

If I'm reading your comment correctly, you're saying that this:

# Current context: { value => 'ten', bar => { value => 10 } }
# bar.partial => '{{value}}'
value is {{<bar}}

would render as "value is 10", and not "value is ten".

My understanding of Mustache (which, again, may be incorrect) is that the latter is actually the correct rendering, though the former can be achieved with:

value is {{#bar}}{{<bar}}{{/bar}}

@waba
Copy link
Author

waba commented Sep 18, 2010

I guess you're right, yours is probably the correct rendering. And that makes the question about accessing partials under different names less important. Thanks.

This issue was closed.
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

2 participants