All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.jknack.handlebars.js.HandlebarsJs Maven / Gradle / Ivy

There is a newer version: 4.4.0
Show newest version
/**
 * Copyright (c) 2012-2013 Edgar Espina
 *
 * This file is part of Handlebars.java.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.jknack.handlebars.js;

import static org.apache.commons.lang3.Validate.notNull;

import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.internal.js.RhinoHandlebars;

/**
 * The main motivation of {@link HandlebarsJs} is the ability of reuse JavaScript helpers in the
 * server and the client.
 *
 * @author edgar.espina
 * @since 1.1.0
 */
public abstract class HandlebarsJs {

  /**
   * The handlebars instance. Required.
   */
  protected final Handlebars handlebars;

  /**
   * Creates a new {@link Handlebars} object.
   *
   * @param handlebars The {@link Handlebars} instance. Required.
   */
  public HandlebarsJs(final Handlebars handlebars) {
    this.handlebars = notNull(handlebars, "The handlebars is required.");
  }

  /**
   * 

* Register helpers from a JavaScript source. *

*

* A JavaScript source file looks like: *

* *
   *  Handlebars.registerHelper('hey', function (context) {
   *    return 'Hi ' + context.name;
   *  });
   *  ...
   *  Handlebars.registerHelper('hey', function (context, options) {
   *    return 'Hi ' + context.name + options.hash['x'];
   *  });
   *  ...
   *  Handlebars.registerHelper('hey', function (context, p1, p2, options) {
   *    return 'Hi ' + context.name + p1 + p2 + options.hash['x'];
   *  });
   *  ...
   * 
* * To keep your helpers reusable between server and client avoid DOM manipulation. * * @param filename The file name (just for debugging purpose). Required. * @param source The JavaScript source. Required. * @throws Exception If the JavaScript helpers can't be registered. */ public abstract void registerHelpers(String filename, String source) throws Exception; /** * Creates a {@link HandlebarsJs} object. * * @param handlebars The handlebars object. Required. * @return A new {@link HandlebarsJs} object. */ public static HandlebarsJs create(final Handlebars handlebars) { return new RhinoHandlebars(handlebars); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy