Search in sources :

Example 1 with StringTemplate

use of org.summerb.utils.stringtemplate.api.StringTemplate in project summerb by skarpushin.

the class ArticleRendererImpl method renderArticle.

@Override
public RenderedArticle renderArticle(String key, Locale locale) {
    Preconditions.checkArgument(StringUtils.hasText(key));
    Preconditions.checkArgument(locale != null);
    try {
        Article article = articleService.findArticleByKeyAndLocale(key, locale);
        if (article == null) {
            throw new RuntimeException("Article not found: " + key + ", locale: " + locale);
        }
        RenderedArticle renderedArticle = buildRenderedArticleTemplate(article);
        ArticleRenderingContext articleRenderingContext = new ArticleRenderingContext(locale, renderedArticle, articleService, articleAbsoluteUrlBuilder);
        StringTemplate annotationTemplate = stringTemplateCompiler.compile(article.getAnnotation());
        renderedArticle.setAnnotation(annotationTemplate.applyTo(articleRenderingContext));
        StringTemplate contentTemplate = stringTemplateCompiler.compile(article.getContent());
        renderedArticle.setContent(contentTemplate.applyTo(articleRenderingContext));
        return renderedArticle;
    } catch (Throwable t) {
        throw new RuntimeException("Failed to render article", t);
    }
}
Also used : RenderedArticle(org.summerb.microservices.articles.api.dto.consuming.RenderedArticle) Article(org.summerb.microservices.articles.api.dto.Article) StringTemplate(org.summerb.utils.stringtemplate.api.StringTemplate) RenderedArticle(org.summerb.microservices.articles.api.dto.consuming.RenderedArticle)

Example 2 with StringTemplate

use of org.summerb.utils.stringtemplate.api.StringTemplate in project summerb by skarpushin.

the class StringTemplateCompilerSpElImplTest method testCompile.

/**
 * Run the StringTemplate compile(String) method test
 */
@Test
public void testCompile() {
    String template = "Hello ${name}! I hope you are fine. Now you have ${amount} money!";
    StringTemplate stringTemplate = fixture.compile(template);
    String result = stringTemplate.applyTo(new DomainObjectExample("Bob", 1234));
    assertEquals("Hello Bob! I hope you are fine. Now you have 1234 money!", result);
}
Also used : StringTemplate(org.summerb.utils.stringtemplate.api.StringTemplate) Test(org.junit.Test)

Aggregations

StringTemplate (org.summerb.utils.stringtemplate.api.StringTemplate)2 Test (org.junit.Test)1 Article (org.summerb.microservices.articles.api.dto.Article)1 RenderedArticle (org.summerb.microservices.articles.api.dto.consuming.RenderedArticle)1