001package io.jstach.opt.spring.example;
002
003import java.util.List;
004
005import org.springframework.context.annotation.Bean;
006import org.springframework.context.annotation.Configuration;
007import org.springframework.core.env.Environment;
008
009import io.jstach.jstachio.JStachio;
010import io.jstach.jstachio.Template;
011import io.jstach.jstachio.spi.JStachioExtension;
012import io.jstach.opt.jmustache.JMustacheRenderer;
013import io.jstach.opt.spring.SpringJStachio;
014import io.jstach.opt.spring.SpringJStachioExtension;
015
016/**
017 * Configures JStachio Spring style.
018 */
019@Configuration
020public class SpringTemplateConfig {
021
022        /**
023         * Do nothing constructor to placate jdk 18 javadoc
024         */
025        public SpringTemplateConfig() {
026        }
027
028        /**
029         * Creates a services based on spring objects.
030         * @param environment used for config
031         * @param templates found templates via component scanning
032         * @return the services
033         */
034        @Bean
035        public SpringJStachioExtension jstachioService(@SuppressWarnings("exports") Environment environment,
036                        List<Template<?>> templates) {
037                return new SpringJStachioExtension(environment, templates);
038        }
039
040        /**
041         * Creates jstachio from found plugins
042         * @param services plugins
043         * @return spring version fo jstachio
044         */
045        @Bean
046        public SpringJStachio jstachio(List<JStachioExtension> services) {
047                var js = new SpringJStachio(services);
048                // We need this for the view mixins.
049                JStachio.setStaticJStachio(() -> js);
050                return js;
051        }
052
053        /**
054         * The JMustache plugin to render templates while editing in development mode.
055         * @return jmustache plugin
056         */
057        @Bean
058        public JStachioExtension jmustache() {
059                return new JMustacheRenderer();
060        }
061
062}