Skip to content

Instantly share code, notes, and snippets.

View ndemengel's full-sized avatar

Nicolas Grisey Demengel ndemengel

View GitHub Profile
@ndemengel
ndemengel / test-bundle.js
Created November 20, 2016 16:25
Webpack: JS entry point importing Handlebars template (result of https://gist.github.com/ndemengel/3bc106eac2c6a8d095ce0bbdab55420c)
// skipped: 17 lines to define webpackBootstrap and __webpack_require__...
webpackBootstrap({
0: function(module, exports, __webpack_require__) {
var helloTemplate = __webpack_require__(371);
console.log(helloTemplate({name: 'John'}));
},
188: function(module, exports) { module.exports = Handlebars; },
371: function(module, exports, __webpack_require__) {
@ndemengel
ndemengel / handlebars-taglib-example.jsp
Created November 20, 2016 16:26
Handlebars taglib example
<%@ taglib prefix="hbs" uri="http://www.hopwork.com/jsp/jstl/handlebars" %>
<%-- preferred form: all needed data are computed outside of the view and put into ${headerData} --%>
<hbs:render template="common/header" context="${headerData}"/>
<%-- sometimes, it's still useful to tweak the view independently of the data --%>
<hbs:render template="some/other/template">
<hbs:contextProperty name="someCondition" value="true"/>
<hbs:contextProperty name="someConfigProperty" value="SOME_CONFIG_VALUE"/>
<hbs:contextProperty name="data" value="${someData}"/>
@ndemengel
ndemengel / ContextProperty.java
Last active November 20, 2016 21:14
JSP tag to render Handlebars templates (Java code, Spring config and TLD here: https://gist.github.com/ndemengel/2d0ce37ad326570cecfc2daccaae2267)
import lombok.Value;
@Value
public class ContextProperty {
String name;
Object value;
}
@ndemengel
ndemengel / ConcatHelper.java
Last active November 20, 2016 21:36
Handlebars helper "concat" in JavaScript and Java
import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.Options;
import java.io.IOException;
import java.util.StringJoiner;
public class ConcatHelper implements Helper<Object> {
@Override
public Object apply(Object firstObjectToConcat, Options options) throws IOException {
StringJoiner joiner = new StringJoiner("");
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.cache.ConcurrentMapTemplateCache;
import com.github.jknack.handlebars.helper.*;
import com.github.jknack.handlebars.io.*;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import javax.inject.Inject;
import java.util.*;
@ndemengel
ndemengel / HelloController.java
Created November 20, 2016 21:28
Sample Spring controller using Handlebars
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping(path = "/hello", method = RequestMethod.GET)
public ModelAndView sayHelloTo(@RequestParam String name) {
return new ModelAndView("hello", "name", name);
}
@ndemengel
ndemengel / RecomposedThrowable.java
Last active January 30, 2017 10:04
RecomposedThrowable, for when you want to test a very specific error handling case but all you have is a stack trace from your production logs
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.util.Arrays.stream;
import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableList;
import static java.util.Optional.ofNullable;
@ndemengel
ndemengel / 1 EmailUpdated.java
Last active April 24, 2017 12:13
Sample event used with our Spring Boot/RabbitMQ solution
import lombok.Value;
// This code only works if compiling the code using javac's -parameters option (Java 8 only).
// With this option, Jackons can use the all-args constructor of our type.
// See https://gist.github.com/ndemengel/72f362bc31afe5fcaa0499af8f269651 for a more general solution.
@Value
public class EmailUpdated implements Event {
public static final String EXCHANGE_NAME = "email.updated.exchange";
import lombok.*;
// The aim here is to have a type that looks like a value type, while still
// allowing Jackson to unmarshal our type from JSON.
// See https://gist.github.com/ndemengel/77a8ffbb929bc55e3d87809820c1c5a4 for a more elegant solution.
@AllArgsConstructor @Getter
@EqualsAndHashCode @ToString
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class EmailUpdated implements Event<EmailUpdated> {
@ndemengel
ndemengel / 1 EmailUpdated.java
Last active April 30, 2017 18:14
Spring/Rabbit boilerplate
import lombok.Data;
@Data
public final class EmailUpdated {
private String accountId;
private String oldEmail;
private String newEmail;
public EmailUpdated(String accountId, String oldEmail, String newEmail) {
this.accountId = accountId;