com.github.jknack.handlebars.Helper Java Examples

The following examples show how to use com.github.jknack.handlebars.Helper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomHelperUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenHelperIsCreated_ThenCanRegister() throws IOException {
    Handlebars handlebars = new Handlebars(templateLoader);
    handlebars.registerHelper("isBusy", new Helper<Person>() {
        @Override
        public Object apply(Person context, Options options) throws IOException {
            String busyString = context.isBusy() ? "busy" : "available";
            return context.getName() + " - " + busyString;
        }
    });
    Template template = handlebars.compile("person");
    Person person = getPerson("Baeldung");

    String templateString = template.apply(person);

    assertThat(templateString).isEqualTo("Baeldung - busy");
}
 
Example #2
Source File: Helpers.java    From bootstraped-multi-test-results-report with MIT License 6 votes vote down vote up
private Helper<Long> dateHelper() {
    return new Helper<Long>() {
        public CharSequence apply(Long arg0, Options arg1) throws IOException {
            PeriodFormatter formatter = new PeriodFormatterBuilder()
                .appendDays()
                .appendSuffix(" d : ")
                .appendHours()
                .appendSuffix(" h : ")
                .appendMinutes()
                .appendSuffix(" m : ")
                .appendSeconds()
                .appendSuffix(" s : ")
                .appendMillis()
                .appendSuffix(" ms")
                .toFormatter();
            return formatter.print(new Period((arg0 * 1) / 1000000));
        }
    };
}
 
Example #3
Source File: Helpers.java    From bootstraped-multi-test-results-report with MIT License 5 votes vote down vote up
private Helper<Object> nowHelper() {
    return new Helper<Object>() {
        @Override
        public CharSequence apply(Object context, Options options) throws IOException {
            Calendar cal = Calendar.getInstance();
            Date date = cal.getTime();
            String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").format(date);
            TimeZone tz = cal.getTimeZone();
            return now + " " + tz.getID();
        }
    };
}
 
Example #4
Source File: PASystemImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
private Handlebars loadHandleBars(final I18n i18n) {
    Handlebars handlebars = new Handlebars()
            .with(cache);

    handlebars.registerHelper("t", new Helper<Object>() {
        @Override
        public CharSequence apply(final Object context, final Options options) {
            String key = options.param(0);
            return i18n.t(key);
        }
    });

    return handlebars;
}
 
Example #5
Source File: Handlebars.java    From template-benchmark with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Setup
public void setup() throws IOException {
  template = new com.github.jknack.handlebars.Handlebars(new ClassPathTemplateLoader("/", ".html"))
          .registerHelper("minus", new Helper<Stock>() {
            @Override
            public CharSequence apply(final Stock stock, final Options options)
                throws IOException {
              return stock.getChange() < 0 ? new SafeString("class=\"minus\"") : null;
            }
          }).compile("templates/stocks.hbs");
  this.context = getContext();
}
 
Example #6
Source File: PASystemImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
private Handlebars loadHandleBars(final I18n i18n) {
    Handlebars handlebars = new Handlebars()
            .with(cache);

    handlebars.registerHelper("t", new Helper<Object>() {
        @Override
        public CharSequence apply(final Object context, final Options options) {
            String key = options.param(0);
            return i18n.t(key);
        }
    });

    return handlebars;
}
 
Example #7
Source File: VaadinConnectTsGenerator.java    From flow with Apache License 2.0 5 votes vote down vote up
private Helper<String> getMultipleLinesHelper() {
    return (context, options) -> {
        Options.Buffer buffer = options.buffer();
        String[] lines = context.split("\n");
        Context parent = options.context;
        Template fn = options.fn;
        for (String line : lines) {
            buffer.append(options.apply(fn, parent.combine("@line", line)));
        }
        return buffer;
    };
}
 
Example #8
Source File: Helpers.java    From bootstraped-multi-test-results-report with MIT License 5 votes vote down vote up
private Helper<List<Row>> doTableHelper() {
    return new Helper<List<Row>>() {
        @Override
        public CharSequence apply(List<Row> rows, Options arg1) throws IOException {
            String tableContent = "<table  class='table table-condensed table-hover'>";
            int indexRow = 0;
            for (Row row : rows) {
                indexRow++;
                if (indexRow == 1) {
                    tableContent += "<thead><tr>";
                } else if (indexRow == 2) {
                    tableContent += "<tbody><tr>";
                } else {
                    tableContent += "<tr>";
                }
                for (String cell : row.getCells()) {
                    if (indexRow == 1) {
                        tableContent += "<th>" + cell + "</th>";
                    } else {
                        tableContent += "<td>" + cell + "</td>";
                    }
                }
                if (indexRow == 1) {
                    tableContent += "</tr></thead>";
                } else {
                    tableContent += "</tr>";
                }
            }
            tableContent += "</tbody></table>";
            return tableContent;
        }
    };
}
 
Example #9
Source File: Helpers.java    From bootstraped-multi-test-results-report with MIT License 5 votes vote down vote up
private Helper<List<StepRow>> doTableHelperForStep() {
    return new Helper<List<StepRow>>() {
        @Override
        public CharSequence apply(List<StepRow> rows, Options arg1) throws IOException {
            String tableContent = "<table  class='table table-condensed table-hover'>";
            int indexRow = 0;
            for (StepRow row : rows) {
                indexRow++;
                if (indexRow == 1) {
                    tableContent += "<thead><tr>";
                } else if (indexRow == 2) {
                    tableContent += "<tbody><tr>";
                } else {
                    tableContent += "<tr>";
                }
                for (String cell : row.getCells()) {
                    if (indexRow == 1) {
                        tableContent += "<th>" + cell + "</th>";
                    } else {
                        tableContent += "<td>" + cell + "</td>";
                    }
                }
                if (indexRow == 1) {
                    tableContent += "</tr></thead>";
                } else {
                    tableContent += "</tr>";
                }
            }
            tableContent += "</tbody></table>";
            return tableContent;
        }
    };
}
 
Example #10
Source File: HandlebarsTemplateService.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
public void registerHelpers(Map<String,Helper<? extends Object>>helpers) {
	if(!helpers.isEmpty()){
		helpers.entrySet().stream().forEach(e->
			handlebars.registerHelper(e.getKey(),e.getValue())
		);
	}
}
 
Example #11
Source File: Helpers.java    From bootstraped-multi-test-results-report with MIT License 5 votes vote down vote up
private Helper<String> isCollapsedHelper() {
    return new Helper<String>() {
        @Override
        public CharSequence apply(String arg0, Options arg1) throws IOException {
            return checkState(
                arg0.toLowerCase(),
                Constants.COLLAPSE_IN,
                Constants.COLLAPSE,
                Constants.COLLAPSE_IN,
                Constants.COLLAPSE_IN);
        }
    };
}
 
Example #12
Source File: Helpers.java    From bootstraped-multi-test-results-report with MIT License 5 votes vote down vote up
private Helper<String> resolveTitleHelper() {
    return new Helper<String>() {
        @Override
        public CharSequence apply(String arg0, Options arg1) throws IOException {
            return checkState(
                arg0.toLowerCase(),
                Constants.THIS_STEP_HAS_BEEN_SKIPPED,
                Constants.THIS_STEP_HAS_PASSED,
                Constants.THIS_STEP_HAS_FAILED,
                Constants.THIS_STEP_HAS_NOT_BEEN_DEFINED);
        }
    };
}
 
Example #13
Source File: Helpers.java    From bootstraped-multi-test-results-report with MIT License 5 votes vote down vote up
private Helper<String> resultColorHelper() {
    return new Helper<String>() {
        @Override
        public CharSequence apply(String arg0, Options arg1) throws IOException {
            return checkState(
                arg0.toLowerCase(),
                Constants.INFO,
                Constants.SUCCESS,
                Constants.DANGER,
                Constants.WARNING);
        }
    };
}
 
Example #14
Source File: Helpers.java    From bootstraped-multi-test-results-report with MIT License 5 votes vote down vote up
private Helper<String> embeddingHelper() {
    return new Helper<String>() {
        @Override
        public CharSequence apply(String arg0, Options arg1) throws IOException {
            String toReturn;
            String id = UUID.randomUUID().toString();
            int index = arg1.param(1);
            if (arg1.param(0).toString().contains("image")) {
                toReturn =
                    "<button 'type='button'"
                        + "class='btn btn-primary'"
                        + "data-toggle='modal' "
                        + "data-target='#" + id + "'>"
                        + "  Screenshot " + ++index + ""
                        + "</button>";
                toReturn +=
                    "<div id='" + id + "'"
                        + "class='modal fade'"
                        + "tabindex='-1'"
                        + "role='dialog'"
                        + "aria-labelledby='myModalLabel'"
                        + "aria-hidden='true'>"
                        + "  <div style='width:90%;height:90%;'"
                        + "  class='modal-dialog'>"
                        + "    <div class='modal-content'>"
                        + "      <div class='modal-body'>"
                        + "        <img "
                        + "        src='data:image/png;base64," + arg0 + "'"
                        + "        class='img-responsive'>"
                        + "      </div>"
                        + "    </div>"
                        + "  </div>"
                        + "</div>";
            } else {
                toReturn = "<pre>" + arg0 + "</pre>";
            }
            return toReturn;
        }
    };
}
 
Example #15
Source File: HandleBarsTemplateServiceTest.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Test
public void testInjectCustomHelper() {
	HashMap<String, Helper<? extends Object>> helpers = new HashMap<String, Helper<? extends Object>>();
	helpers.put("testhelper",(Object a, Options o)->{return "hello";});
	service.registerHelpers(helpers);
	String template = service.render("test-customhelper", context);
	assertEquals("hello", template);		
}
 
Example #16
Source File: Templating.java    From StubbornJava with MIT License 4 votes vote down vote up
public <T> Builder withHelper(String helperName, Helper<T> helper) {
    log.debug("using template helper {}" , helperName);
    handlebars.registerHelper(helperName, helper);
    return this;
}
 
Example #17
Source File: RestEndPointMocker.java    From zerocode with Apache License 2.0 4 votes vote down vote up
private static Map<String, Helper> getWiremockHelpers() {
    Map<String, Helper> helperMap = new HashedMap();
    helperMap.put("localdatetime", new HandlebarsLocalDateHelper());
    return helperMap;
}
 
Example #18
Source File: VaadinConnectTsGenerator.java    From flow with Apache License 2.0 4 votes vote down vote up
private Helper<String> getClassNameFromImportsHelper() {
    return (className, options) -> getSimpleNameFromImports(className,
            (List<Map<String, String>>) options.param(0));
}
 
Example #19
Source File: VaadinConnectTsGenerator.java    From flow with Apache License 2.0 4 votes vote down vote up
private Helper<CodegenProperty> getModelTypeHelper() {
    return (prop, options) -> getModelType(prop,
            (List<Map<String, String>>) options.param(0));
}
 
Example #20
Source File: VaadinConnectTsGenerator.java    From flow with Apache License 2.0 4 votes vote down vote up
private Helper<CodegenProperty> getModelConstructorHelper() {
    return (prop, options) -> getModelConstructor(prop,
            (List<Map<String, String>>) options.param(0));
}
 
Example #21
Source File: Templating.java    From StubbornJava with MIT License 4 votes vote down vote up
public <T> Builder withHelper(String helperName, Helper<T> helper) {
    log.debug("using template helper {}" , helperName);
    handlebars.registerHelper(helperName, helper);
    return this;
}
 
Example #22
Source File: HandlebarsHelper.java    From dew with Apache License 2.0 4 votes vote down vote up
public static void registerFormatPackage(Handlebars handlebars) {
    handlebars.registerHelper("formatPackage",
            (Helper<String>) (s, options) -> NameHelper.formatPackage(s));
}
 
Example #23
Source File: HandlebarsHelper.java    From dew with Apache License 2.0 4 votes vote down vote up
public static void registerFormatClassName(Handlebars handlebars) {
    handlebars.registerHelper("formatClassName",
            (Helper<String>) (s, options) -> NameHelper.formatClassName(s));
}
 
Example #24
Source File: HandlebarsHelperBundle.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
/**
 * {@link com.github.jknack.handlebars.Handlebars#registerHelperMissing(com.github.jknack.handlebars.Helper)}
 */
public static <H> void registerHelperMissing(Helper<H> helper) {
    HandlebarsViewRenderer.HANDLEBARS.registerHelperMissing(helper);
}
 
Example #25
Source File: HandlebarsHelperBundle.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
/**
 * {@link com.github.jknack.handlebars.Handlebars#registerHelper(String, com.github.jknack.handlebars.Helper)}
 */
public static <H> void registerHelper(String name, Helper<H> helper) {
    HandlebarsViewRenderer.HANDLEBARS.registerHelper(name, helper);
}