Loading thymeleaf template from Database

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Loading thymeleaf template from Database

Ruud
Hi,

Is there a way to load the templates from Database instead of having them in the web inf folder?

Thanks,
Karthick.
Reply | Threaded
Open this post in threaded view
|

Re: Loading thymeleaf template from Database

danielfernandez
Administrator
There are no out-of-the-box artifacts for reading templates from a DB, but you can create your own by following these steps:

1st. Create an implementation of IResourceResolver [ http://www.thymeleaf.org/apidocs/thymeleaf/2.0.13/org/thymeleaf/resourceresolver/IResourceResolver.html ] that is able to load files from your database (can be templates, but could also be other things you might want to store at your DB like i18n files...).

2nd. Create an implementation of ITemplateResolver [ http://www.thymeleaf.org/apidocs/thymeleaf/2.0.13/org/thymeleaf/templateresolver/ITemplateResolver.html ] that uses this "resource resolver" you created for reading templates and, once your template is read, assigns it several metainformation like template mode, cache capabilities, etc. I recommend you to use "AbstractTemplateResolver" or maybe even "TemplateResolver" instead as a base for creating your extension.

Then simply add your now template resolver to your template engine and you are done :-)

As a plus, note that your ITemplateResolver implementation can return for each template an ITemplateResolutionValidity instance (see http://www.thymeleaf.org/apidocs/thymeleaf/2.0.13/org/thymeleaf/templateresolver/ITemplateResolutionValidity.html ), which might allow you to implement some kind of intelligent cache invalidation policy able to avoid queries to database for retrieving templates for as long as they don't change in database (you could check for changes only every x minutes, for example), heavily improving your performance...

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: Loading thymeleaf template from Database

Ruud
Thank you!!! Its working :):):)
Reply | Threaded
Open this post in threaded view
|

Re: Loading thymeleaf template from Database

Kilian
Ruud, I came across this post while searching for this. Would you mind sharing your code?
Reply | Threaded
Open this post in threaded view
|

Re: Loading thymeleaf template from Database

michael_wb
This post was updated on .
I found this thread while looking for a way to pull template content out of our database.  I'm posting an updated version of our code that works with Thymeleaf 3.0.5 for the next person who comes along.  Hope it helps.

import java.util.Map;
import org.thymeleaf.IEngineConfiguration;
import org.thymeleaf.templateresolver.StringTemplateResolver;
import org.thymeleaf.templateresource.ITemplateResource;

import com.google.common.collect.Sets;

public class ThymeleafDatabaseResourceResolver extends StringTemplateResolver {
	private final static String PREFIX = "";

	@Autowired ThymeleafTemplateDao thymeleaftemplateDao;

	public ThymeleafDatabaseResourceResolver() {
		setResolvablePatterns(Sets.newHashSet(PREFIX + "*"));
	}

	@Override
	protected ITemplateResource computeTemplateResource(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String, Object> templateResolutionAttributes) {

                // ThymeleafTemplate is our internal object that contains the content.  
               // You should change this to match you're set up.

                ThymeleafTemplateDao thymeleaftemplateDao = ApplicationContextProvider.getApplicationContext().getBean(ThymeleafTemplateDao.class);
		ThymeleafTemplate thymeleafTemplate = thymeleaftemplateDao.findByTemplateName(template);  
		if (thymeleafTemplate != null) {
			return super.computeTemplateResource(configuration, ownerTemplate, thymeleafTemplate.getContent(), templateResolutionAttributes);
		}
		return null;
	}

}