Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added loader that works from memory #617

Merged
merged 3 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/src/orchid/resources/changelog/v3_2_0.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ version: '3.2.0'
---

- Add support for spring framework 6 and spring-boot 3 (#630)
- Bump minimum supported java version to 17 in pebble-spring6 and pebble-spring-boot-starter in order to work with spring (#630)
- Bump minimum supported java version to 17 in pebble-spring6 and pebble-spring-boot-starter in order to work with
spring (#630)
- Add a memory loader that supports inheritance and doesn't require a filesystem. This is useful for applications
that retrieve templates from a database for example (#617).
- **BREAKING CHANGE**: Change default suffix to `.peb` instead of `.pebble` in spring boot autoconfiguration (#553)
- **BREAKING CHANGE**: Rename method `getInstance` to `createInstance` in `BinaryOperator` interface (#521)
- **BREAKING CHANGE**: Rename package from `com.mitchellbosecke` to `io.pebbletemplates` (#635)
2 changes: 2 additions & 0 deletions docs/src/orchid/resources/wiki/guide/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ application server but is not enabled by default.
- `Servlet5Loader`: Same as `ServletLoader`, but for Jakarta Servlet 5.0 or newer.
- `StringLoader`: Considers the name of the template to be the contents of the template.
- `DelegatingLoader`: Delegates responsibility to a collection of children loaders.
- `MemoryLoader`: Loader that supports inheritance and doesn't require a filesystem. This is useful for applications
that retrieve templates from a database for example.

If you do not provide a custom Loader, Pebble will use an instance of the `DelegatingLoader` by default.
This delegating loader will use a `ClasspathLoader` and a `FileLoader` behind the scenes to find your templates.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package io.pebbletemplates.pebble.loader;

import io.pebbletemplates.pebble.error.LoaderException;

import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

public class MemoryLoader implements Loader<String> {
private final List<TemplateDefinition> templateDefinitions = new ArrayList<>();

@Override
public Reader getReader(String templateName) {
String content = "";
for (TemplateDefinition templateDefinition : this.templateDefinitions) {
if (templateDefinition.templateName.equals(templateName)) {
content = templateDefinition.content;
break;
}
}

if (content.isEmpty()) {
throw new LoaderException(null, "Could not find template \"" + templateName + "\"");
}

return new StringReader(content);
}

public void addTemplate(String templateName, String content) {
if (templateName == null) {
throw new IllegalArgumentException("templateName cannot be null");
}
if (content == null) {
throw new IllegalArgumentException("content cannot be null");
}
this.templateDefinitions.add(new TemplateDefinition(templateName, content));
}

@Override
public void setSuffix(String suffix) {
}

@Override
public void setPrefix(String prefix) {
}

@Override
public void setCharset(String charset) {
}

@Override
public String resolveRelativePath(String relativePath, String anchorPath) {
return relativePath; // hierarchy is flat
}

@Override
public String createCacheKey(String templateName) {
return templateName;
}

@Override
public boolean resourceExists(String templateName) {
for (TemplateDefinition templateDefinition : this.templateDefinitions) {
if (templateDefinition.templateName.equals(templateName)) {
return true;
}
}
return false;
}

private static class TemplateDefinition {
public final String templateName;
public final String content;

public TemplateDefinition(String templateName,
String content) {
this.templateName = templateName;
this.content = content;
}
}
}
44 changes: 32 additions & 12 deletions pebble/src/test/java/io/pebbletemplates/pebble/LoaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,11 @@

import io.pebbletemplates.pebble.error.LoaderException;
import io.pebbletemplates.pebble.error.PebbleException;
import io.pebbletemplates.pebble.loader.ClasspathLoader;
import io.pebbletemplates.pebble.loader.DelegatingLoader;
import io.pebbletemplates.pebble.loader.FileLoader;
import io.pebbletemplates.pebble.loader.Loader;
import io.pebbletemplates.pebble.loader.StringLoader;
import io.pebbletemplates.pebble.loader.*;
import io.pebbletemplates.pebble.template.PebbleTemplate;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
Expand Down Expand Up @@ -108,6 +98,36 @@ void testDelegatingLoader() throws PebbleException, IOException {
assertEquals("LOADER ONE", writer.toString());
}

@Test
void testMemoryLoader() throws PebbleException, IOException {
MemoryLoader loader = new MemoryLoader();
PebbleEngine pebble = new PebbleEngine.Builder().loader(loader).strictVariables(false).build();

loader.addTemplate("home.html", "{% extends \"layout.html\" %}{% block title %} Home {% endblock %}"
+ "{% block content %}"
+ "<h1> Home </h1>"
+ "<p> Welcome to my home page. My name is {{ name }}.</p>"
+ "{% endblock %}");
loader.addTemplate("layout.html", "<html>"
+ "<head>"
+ "<title>Hello Pebble</title>"
+ "</head>"
+ "<body>"
+ "{% block content %}{% endblock %}"
+ "</body>"
+ "</html>");

PebbleTemplate template = pebble.getTemplate("home.html");

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

Writer writer = new StringWriter();
template.evaluate(writer, context);

assertEquals("<html><head><title>Hello Pebble</title></head><body><h1> Home </h1><p> Welcome to my home page. My name is Bob.</p></body></html>", writer.toString());
}

@Test
void testGetLiteralTemplate() throws IOException {
PebbleEngine engine = new PebbleEngine.Builder().build();
Expand Down