Skip to content

Commit

Permalink
Added first filter and join filter
Browse files Browse the repository at this point in the history
  • Loading branch information
mbosecke committed Feb 10, 2015
1 parent 0e978ec commit 3de0cdb
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public Map<String, Filter> getFilters() {
filters.put("date", new DateFilter());
filters.put("default", new DefaultFilter());
filters.put("first", new FirstFilter());
filters.put("join", new JoinFilter());
filters.put("lower", new LowerFilter());
filters.put("numberformat", new NumberFormatFilter());
filters.put("sort", new SortFilter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
******************************************************************************/
package com.mitchellbosecke.pebble.extension.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand All @@ -25,9 +24,7 @@ public class FirstFilter implements Filter {

@Override
public List<String> getArgumentNames() {
List<String> names = new ArrayList<>();
names.add("collection");
return names;
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*******************************************************************************
* This file is part of Pebble.
*
* Copyright (c) 2014 by Mitchell Bösecke
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
******************************************************************************/
package com.mitchellbosecke.pebble.extension.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.mitchellbosecke.pebble.extension.Filter;

/**
* Concatenates all entries of a collection, optionally glued together
* with a particular character such as a comma.
*
* @author mbosecke
*
*/
public class JoinFilter implements Filter {

@Override
public List<String> getArgumentNames() {
List<String> names = new ArrayList<>();
names.add("glue");
return names;
}

@Override
public Object apply(Object input, Map<String, Object> args) {
if (input == null) {
return null;
}

@SuppressWarnings("unchecked")
Collection<Object> inputCollection = (Collection<Object>) input;

StringBuilder builder = new StringBuilder();

String glue = null;
if(args.containsKey("glue")){
glue = (String)args.get("glue");
}

boolean isFirst = true;
for(Object entry : inputCollection){

if(!isFirst && glue != null){
builder.append(glue);
}
builder.append(entry);

isFirst = false;
}
return builder.toString();
}
}
78 changes: 78 additions & 0 deletions src/test/java/com/mitchellbosecke/pebble/CoreFiltersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,84 @@ public void testFirstWithStringInput() throws PebbleException, IOException {
template.evaluate(writer, context);
assertEquals("A", writer.toString());
}

@Test
public void testJoin() throws PebbleException, IOException {
Loader loader = new StringLoader();
PebbleEngine pebble = new PebbleEngine(loader);

PebbleTemplate template = pebble
.getTemplate("{{ names | join(',') }}");

List<String> names = new ArrayList<>();
names.add("Alex");
names.add("Joe");
names.add("Bob");

Map<String, Object> context = new HashMap<>();
context.put("names", names);

Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals("Alex,Joe,Bob", writer.toString());
}


@Test
public void testJoinWithoutGlue() throws PebbleException, IOException {
Loader loader = new StringLoader();
PebbleEngine pebble = new PebbleEngine(loader);

PebbleTemplate template = pebble
.getTemplate("{{ names | join }}");

List<String> names = new ArrayList<>();
names.add("Alex");
names.add("Joe");
names.add("Bob");

Map<String, Object> context = new HashMap<>();
context.put("names", names);

Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals("AlexJoeBob", writer.toString());
}


@Test
public void testJoinWithNumbers() throws PebbleException, IOException {
Loader loader = new StringLoader();
PebbleEngine pebble = new PebbleEngine(loader);

PebbleTemplate template = pebble
.getTemplate("{{ numbers | join(',') }}");

List<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);

Map<String, Object> context = new HashMap<>();
context.put("numbers", numbers);

Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals("1,2,3", writer.toString());
}

@Test
public void testJoinWithNullInput() throws PebbleException, IOException {
Loader loader = new StringLoader();
PebbleEngine pebble = new PebbleEngine(loader);

PebbleTemplate template = pebble
.getTemplate("{{ null | join(',') }}");

Writer writer = new StringWriter();
template.evaluate(writer);
assertEquals("", writer.toString());
}

public class User {

Expand Down

0 comments on commit 3de0cdb

Please sign in to comment.