
com.mitchellbosecke.pebble.spring.extension.function.HrefFunction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pebble-spring3 Show documentation
Show all versions of pebble-spring3 Show documentation
Pebble Integration with Spring 3
The newest version!
package com.mitchellbosecke.pebble.spring.extension.function;
import com.mitchellbosecke.pebble.extension.Function;
import com.mitchellbosecke.pebble.spring.util.ViewUtils;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Pebble function which adds the context path to the given url
*
* @author Eric Bussieres
*/
public class HrefFunction implements Function {
public static final String FUNCTION_NAME = "href";
protected static final String PARAM_URL = "url";
protected List argumentNames;
private String contextPath;
/**
* Constructor
*/
public HrefFunction() {
this.argumentNames = new ArrayList<>();
this.argumentNames.add(PARAM_URL);
}
/**
* {@inheritDoc}
*
* @see com.mitchellbosecke.pebble.extension.Function#execute(java.util.Map)
*/
@Override
public Object execute(Map args) {
StringBuffer result = new StringBuffer();
result.append(this.getContextPath());
this.addUrlParameter(args, result);
return result.toString();
}
private void addUrlParameter(Map args, StringBuffer result) {
String url = (String) args.get(PARAM_URL);
if (StringUtils.hasText(url)) {
result.append(url);
}
}
private String getContextPath() {
if (this.contextPath == null) {
this.contextPath = ViewUtils.getRequest().getContextPath();
}
return this.contextPath;
}
/**
* {@inheritDoc}
*
* @see com.mitchellbosecke.pebble.extension.NamedArguments#getArgumentNames()
*/
@Override
public List getArgumentNames() {
return this.argumentNames;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy