Skip to content

added loader that works from memory#617

Merged
ebussieres merged 3 commits into
PebbleTemplates:masterfrom
acole76:master
Oct 28, 2022
Merged

added loader that works from memory#617
ebussieres merged 3 commits into
PebbleTemplates:masterfrom
acole76:master

Conversation

@acole76

@acole76 acole76 commented Feb 17, 2022

Copy link
Copy Markdown
Contributor

This attempts to solve the issue with StringLoader where inheritance is not supported. This loader uses a flat hierarchy.

MemoryLoader memLoader = new MemoryLoader();
memLoader.addFile("home.html", "{% extends \"layout.html\" %}{% block title %} Home {% endblock %}\r\n"
			+ "\r\n"
			+ "{% block content %}\r\n"
			+ "	<h1> Home </h1>\r\n"
			+ "	<p> Welcome to my home page. My name is {{ name }}.</p>\r\n"
			+ "{% endblock %}");
memLoader.addFile("layout.html", "<!DOCTYPE html>\r\n"
		+ "<html>\r\n"
		+ "    <head>\r\n"
		+ "        <title>Hello Pebble</title>\r\n"
		+ "    </head>\r\n"
		+ "    <body>\r\n"
		+ "        {% block content %}{% endblock %}\r\n"
		+ "    </body>\r\n"
		+ "</html>");
PebbleEngine engine = new PebbleEngine.Builder().loader(memLoader).build();
PebbleTemplate compiledTemplate = engine.getTemplate("home.html");
Map<String, Object> context = new HashMap<String, Object>();
context.put("name", "Mitchell");

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

String output = writer.toString();
System.out.println(output)
```;

@ebussieres ebussieres added this to the 3.2.0 milestone Oct 28, 2022
@ebussieres ebussieres merged commit 97a6f70 into PebbleTemplates:master Oct 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants