Skip to content

Commit

Permalink
Partials with parameters fix #350
Browse files Browse the repository at this point in the history
support passing hash to partials fix #357
  • Loading branch information
jknack committed Apr 26, 2015
1 parent 1c1aa1e commit 1fa4bf5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ delimiters

partial
:
START_PARTIAL PATH QID? END
START_PARTIAL PATH QID? hash* END
;

param
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package com.github.jknack.handlebars.internal;

import static org.apache.commons.lang3.StringUtils.isEmpty;
import static org.apache.commons.lang3.StringUtils.join;
import static org.apache.commons.lang3.Validate.notEmpty;

Expand All @@ -25,6 +26,7 @@
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.github.jknack.handlebars.Context;
import com.github.jknack.handlebars.Handlebars;
Expand All @@ -43,7 +45,7 @@
* @author edgar.espina
* @since 0.1.0
*/
class Partial extends BaseTemplate {
class Partial extends HelperResolver {

/**
* The partial path.
Expand Down Expand Up @@ -76,11 +78,14 @@ class Partial extends BaseTemplate {
* @param handlebars The Handlebars object. Required.
* @param path The template path.
* @param context The template context.
* @param hash Template params
*/
public Partial(final Handlebars handlebars, final String path, final String context) {
public Partial(final Handlebars handlebars, final String path, final String context,
final Map<String, Object> hash) {
super(handlebars);
this.path = notEmpty(path, "The path is required.");
this.context = context;
this.hash(hash);
}

@Override
Expand Down Expand Up @@ -120,11 +125,8 @@ public void merge(final Context context, final Writer writer)
}

Template template = handlebars.compile(source);
if (this.context == null || this.context.equals("this")) {
template.apply(context, writer);
} else {
template.apply(Context.newContext(context, context.get(this.context)), writer);
}
String key = isEmpty(this.context) ? "this" : this.context;
template.apply(Context.newContext(context, context.get(key)).data(hash(context)), writer);
} catch (IOException ex) {
String reason = String.format("The partial '%s' could not be found",
loader.resolve(path));
Expand Down Expand Up @@ -219,8 +221,8 @@ private String partialInput(final String input, final String indent) {
@Override
public String text() {
StringBuilder buffer = new StringBuilder(startDelimiter)
.append('>')
.append(path);
.append('>')
.append(path);

if (context != null) {
buffer.append(' ').append(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public Template visitPartial(final PartialContext ctx) {
TerminalNode partialContext = ctx.QID();
String startDelim = ctx.start.getText();
Template partial = new Partial(handlebars, uri,
partialContext != null ? partialContext.getText() : null)
partialContext != null ? partialContext.getText() : null, hash(ctx.hash()))
.startDelimiter(startDelim.substring(0, startDelim.length() - 1))
.endDelimiter(ctx.stop.getText())
.indent(indent)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.jknack.handlebars.i350;

import java.io.IOException;

import org.junit.Test;

import com.github.jknack.handlebars.AbstractTest;
import com.github.jknack.handlebars.Handlebars;

public class Issue350 extends AbstractTest {

@Override
protected void configure(final Handlebars handlebars) {
handlebars.prettyPrint(true);
}

@Test
public void partialWithParameters() throws IOException {
shouldCompileToWithPartials(
"<ul>\n" +
"{{#each dudes}}\n" +
" {{> dude title=../title class=\"list-item\"}}\n" +
"{{/each}}\n" +
"</ul>",
$("title",
"profile",
"dudes",
new Object[]{$("name", "Yehuda", "url", "http://yehuda"),
$("name", "Alan", "url", "http://alan") }),
$("dude", "<li class=\"{{class}}\">\n" +
" {{title}}: <a href=\"{{url}}\">{{name}}</a>\n" +
"</li>\n"),
"<ul>\n" +
" <li class=\"list-item\">\n" +
" profile: <a href=\"http://yehuda\">Yehuda</a>\n" +
" </li>\n" +
" <li class=\"list-item\">\n" +
" profile: <a href=\"http://alan\">Alan</a>\n" +
" </li>\n" +
"</ul>");
}
}

1 comment on commit 1fa4bf5

@gavinlynch
Copy link

Choose a reason for hiding this comment

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

nice 👍

Please sign in to comment.