Java Code Examples for com.github.jknack.handlebars.Helper

The following examples show how to use com.github.jknack.handlebars.Helper. These examples are extracted from open source projects. 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 Project: bootstraped-multi-test-results-report   Author: web-innovate   File: Helpers.java    License: 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 #2
Source Project: tutorials   Author: eugenp   File: CustomHelperUnitTest.java    License: 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 #3
Source Project: arcusplatform   Author: arcus-smart-home   File: HandlebarsTemplateService.java    License: 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 #4
Source Project: arcusplatform   Author: arcus-smart-home   File: HandleBarsTemplateServiceTest.java    License: 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 #5
Source Project: bootstraped-multi-test-results-report   Author: web-innovate   File: Helpers.java    License: 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 #6
Source Project: bootstraped-multi-test-results-report   Author: web-innovate   File: Helpers.java    License: 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 #7
Source Project: bootstraped-multi-test-results-report   Author: web-innovate   File: Helpers.java    License: 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 #8
Source Project: bootstraped-multi-test-results-report   Author: web-innovate   File: Helpers.java    License: 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 #9
Source Project: bootstraped-multi-test-results-report   Author: web-innovate   File: Helpers.java    License: 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 #10
Source Project: bootstraped-multi-test-results-report   Author: web-innovate   File: Helpers.java    License: 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 #11
Source Project: bootstraped-multi-test-results-report   Author: web-innovate   File: Helpers.java    License: 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 #12
Source Project: flow   Author: vaadin   File: VaadinConnectTsGenerator.java    License: 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 #13
Source Project: sakai   Author: sakaiproject   File: PASystemImpl.java    License: 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 #14
Source Project: template-benchmark   Author: mbosecke   File: Handlebars.java    License: 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 #15
Source Project: sakai   Author: sakaiproject   File: PASystemImpl.java    License: 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 #16
Source Project: dew   Author: gudaoxuri   File: HandlebarsHelper.java    License: 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 #17
Source Project: dew   Author: gudaoxuri   File: HandlebarsHelper.java    License: 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 #18
Source Project: StubbornJava   Author: StubbornJava   File: Templating.java    License: 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 #19
Source Project: zerocode   Author: authorjapps   File: RestEndPointMocker.java    License: 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 #20
Source Project: StubbornJava   Author: StubbornJava   File: Templating.java    License: 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 #21
Source Project: flow   Author: vaadin   File: VaadinConnectTsGenerator.java    License: 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 #22
Source Project: flow   Author: vaadin   File: VaadinConnectTsGenerator.java    License: 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 #23
Source Project: flow   Author: vaadin   File: VaadinConnectTsGenerator.java    License: 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 #24
Source Project: incubator-pinot   Author: apache   File: HandlebarsHelperBundle.java    License: 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 Project: incubator-pinot   Author: apache   File: HandlebarsHelperBundle.java    License: 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);
}