Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
Simple template language that does not emit syntax errors

I'm looking for a simple template engine that does not emit syntax errors. What I mean by that is that every template by definition should have a valid syntax. If a variable or code construct can't be resolved it should simply print out the broken code instead of aborting rendering.

The reason I want to do that is because I want to use them in a context where power users can adjust their own templates but it is very difficult for me to communicate errors back to them.

Right now I'm using a bit of advanced search and replace for tokens which works great but doesn't support loops and if statements. I would like to have a simple template language that allows for very simple if and loop statements.

Do you know of any template engine that can do that in Java?

Example A

Template:

My template <h1>{title}</h1>
<p>{message}</p>
<ul>
{foreach items as item}
<li>{item.value}</li>
{/foreach}
</ul> 

Data:

{
    "title": "Hello World",
    "items": [
        {
            "value": "foo"
        },
        {
            "value": "bar"
        }
    ]
}

Result:

My template <h1>Hello World</h1>
<p>{message}</p>
<ul>
    <li>foo</li>
    <li>bar</li>
</ul> 

Example B

Template:

My template <h1>{title}</h1>
<p>{message}</p>
<ul>
{foreach items
<li>{item.value</li>
{/foreach}
</ul> 

Data:

{
    "title": "Hello World",
    "message": "Test",
    "items": [
        {
            "value": "foo"
        },
        {
            "value": "bar"
        }
    ]
}

Result:

My template <h1>Hello World</h1>
<p>Test</p>
<ul>
{foreach items
<li>{item.value</li>
{/foreach}
</ul>

Edit:

I actually wrote my own simple template engine and it worked. I choose a xml like syntax where if statements and loops where always named. For example {for_cars} & {/for_cars} and {if_loggedIn} & {/if_loggedIn} which made it easier to repair errors. But in the end I opted to go for a simpler design where I simply split the template into multiple smaller templates and implementing the loops and if statements around the templates in code. The templates now only support variables but no logic. That way users have fewer options to go wrong which I think makes it a way more robust user design.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The Apache Velocity Template engine can handle Example A - one or more parameters not specified when template is syntactically correct. The template will look like below in velocity:

My template <h1>${title}</h1>
<p>${message}</p>
<ul>
#foreach( $item in $items )
<li>${item.value}</li>
#end
</ul>

And the output will be like below:

My template <h1>Hello World</h1>
<p>${message}</p>
<ul>
<li>foo</li>
<li>bar</li>
</ul>

But it wasn't able to parse the template when I removed the closing parenthesis from the foreach line. And I do agree with Konrad that, while a templating framework can be used to handle advanced templating, there might not be any framework that can selectively evaluate only the valid parts of a template and even if there is, communicating the errors back to the users might be the right thing to do

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda