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

Used inexisten variable in example #46

Open
yhojann-cl opened this issue Nov 8, 2024 · 0 comments
Open

Used inexisten variable in example #46

yhojann-cl opened this issue Nov 8, 2024 · 0 comments

Comments

@yhojann-cl
Copy link

yhojann-cl commented Nov 8, 2024

In https://www.thymeleaf.org/doc/articles/sayhelloextendingthymeleaf5minutes.html says as example:

Using the hello dialect
Using our new dialect is very easy. This being a Spring MVC application, we just have to add it to our templateEngine bean during configuration.

@Bean
public SpringTemplateEngine templateEngine(){
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setEnableSpringELCompiler(true);
    templateEngine.setTemplateResolver(templateResolver());
    templateEngine.addDialect(new HelloDialect());
    return templateEngine;
}

But templateResolver is not defined and it doesn't say how to make it or what it does.

I try reutilize the default templateEngine adding the custom dialect but can not load the SpringTemplateEngine after the bean config or using a autowired annotation.

I solve this recteating all templateResolver again but it is not defined in the documentation example:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.spring6.SpringTemplateEngine;
import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.templatemode.TemplateMode;
import com.example.demo.dialects.MarkdownDialect;

@Configuration
public class ThymeleafConfig {

    @Bean
    public SpringResourceTemplateResolver templateResolver() { // For fix
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setPrefix("classpath:/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setCacheable(false);
        return templateResolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {

        // Template engine for thymeleaf
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();

        // Template engine settings
        templateEngine.setEnableSpringELCompiler(true);
        templateEngine.setTemplateResolver(this.templateResolver());

        // Add custom dialects
        templateEngine.addDialect(new MarkdownDialect());

        // Return modified template engine
        return templateEngine;
    }
}
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

No branches or pull requests

1 participant