Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public class TemplateConfiguration {
@Getter
private boolean devMode = false;

public TemplateConfiguration (final boolean devMode) {
this.devMode = devMode;
}

public boolean hasTags () {
return !registeredTags.isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public class TemplateEngineFactory {

private TemplateConfiguration configuration;

private TemplateEngineFactory() {
configuration = new TemplateConfiguration();
private TemplateEngineFactory(boolean devMode) {
configuration = new TemplateConfiguration(devMode);
}

public static TemplateEngineFactory newInstance(TemplateLoader templateLoader) {
var factory = new TemplateEngineFactory();
public static TemplateEngineFactory newInstance(TemplateLoader templateLoader, boolean devMode) {
var factory = new TemplateEngineFactory(devMode);

factory.configuration.setTemplateLoader(templateLoader);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,4 @@
public interface TemplateLoader {

String load (String template);

default void invalidate () {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,5 @@ public String load(String template) {
}
throw new TemplateNotFoundException("template %s not found".formatted(template));
}

@Override
public void invalidate() {
this.templateLoaders.forEach(TemplateLoader::invalidate);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,12 @@ public class FileTemplateLoader implements TemplateLoader {

private final Path basePath;

private final ICache<String, String> templateCache;

public FileTemplateLoader(Path basePath, ICache<String, String> templateCache) {
public FileTemplateLoader(Path basePath) {
this.basePath = basePath;

this.templateCache = templateCache;
}

@Override
public String load(String template) {
try {
return templateCache.get(template, key -> {
return loadFromDisk(key);
});
} catch (Exception e) {
throw new TemplateNotFoundException(e.getMessage());
}
}

private String loadFromDisk(String template) {
public String load(String template) {
try {
var path = basePath.resolve(template);

Expand All @@ -79,11 +65,4 @@ private String loadFromDisk(String template) {
throw new TemplateNotFoundException("Failed to load template: " + template, e);
}
}

@Override
public void invalidate() {
this.templateCache.invalidate();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,25 @@ public static CMSModuleTemplateEngine create(final DB db, final Theme theme, Cac

return engine;
}

private ICache<String, String> createTemplateCache (String name) {
if (devMode) {
return this.cacheManager.get(name, new CacheManager.CacheConfig(1000l, Duration.ofMinutes(1)));
} else {
return this.cacheManager.get(name, new CacheManager.CacheConfig(1000l, Duration.ofMinutes(10)));
}
}

private void initTemplateEngine() {

var loaders = new ArrayList<TemplateLoader>();
loaders.add(new FileTemplateLoader(db.getFileSystem().resolve("templates/"), createTemplateCache("templates/site")));
loaders.add(new FileTemplateLoader(db.getFileSystem().resolve("templates/")));

if (!theme.empty()) {
var themeLoader = new FileTemplateLoader(theme.templatesPath(), createTemplateCache("templates/theme"));
var themeLoader = new FileTemplateLoader(theme.templatesPath());
loaders.add(themeLoader);

if (theme.getParentTheme() != null) {
var parentLoader = new FileTemplateLoader(theme.getParentTheme().templatesPath(), createTemplateCache("templates/parent"));
var parentLoader = new FileTemplateLoader(theme.getParentTheme().templatesPath());
loaders.add(parentLoader);
}
}

CompositeTemplateLoader templateLoader = new CompositeTemplateLoader(loaders);

templateEngine = TemplateEngineFactory.newInstance(templateLoader)
templateEngine = TemplateEngineFactory.newInstance(templateLoader, devMode)
.cache(cacheManager.get("templates", new CacheManager.CacheConfig(100l, Duration.ofMinutes(1))))
.defaultFilters()
.defaultTags()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public boolean isDevMode () {
@BeforeAll
public void setup () {
SUT = TemplateEngineFactory
.newInstance(getLoader())
.newInstance(getLoader(), true)
.cache(cacheProvider.getCache("templates", new CacheManager.CacheConfig(100l, Duration.ofSeconds(60))))
.defaultFilters()
.defaultTags()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class TagTemplateFunctionTest extends ContentBaseTest {
public static void setup() {
templateLoader = new StringTemplateLoader();
engine = TemplateEngineFactory
.newInstance(templateLoader)
.newInstance(templateLoader, true)
.defaultFilters()
.defaultTags()
.devMode(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class ParserStabilityTest {

@BeforeAll
public static void setup() {
var config = new TemplateConfiguration();
var config = new TemplateConfiguration(true);
config.registerTag(new com.condation.cms.templates.tags.IfTag());
config.registerTag(new com.condation.cms.templates.tags.EndIfTag());
parser = new Parser(config, jexl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class ParserTest {

@BeforeAll
public static void setup() {
var config = new TemplateConfiguration();
var config = new TemplateConfiguration(true);
parser = new Parser(config, jexl);
lexer = new Lexer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class RendererStabilityTest {

@BeforeAll
public static void setup() {
var config = new TemplateConfiguration();
var config = new TemplateConfiguration(true);
config.registerTag(new com.condation.cms.templates.tags.IfTag());
config.registerTag(new com.condation.cms.templates.tags.EndIfTag());
engine = new CMSTemplateEngine(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.condation.cms.api.ServerContext;
import com.condation.cms.api.cache.CacheManager;
import com.condation.cms.api.cache.CacheProvider;
import com.condation.cms.templates.CMSTemplateEngine;
Expand All @@ -41,7 +42,7 @@ public class TemplateEngine {
public TemplateEngine(CacheManager cacheManager) {

templateEngine = TemplateEngineFactory
.newInstance(new ClasspathTemplateLoader("manager"))
.newInstance(new ClasspathTemplateLoader("manager"), ServerContext.IS_DEV)
.cache(cacheManager.get("ui/templates", new CacheManager.CacheConfig(100l, Duration.ofSeconds(60))))
.defaultFilters()
.defaultTags()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void testSomeMethod() {
);

Assertions.assertThatCode(() -> {
templateEngine.render("index.html", Map.of("actionFactory", new ActionFactory(hookSystem, moduleManager, new User("test", "asdasdfasdf", new String[]{"manager"}))));
templateEngine.render("test.html", Map.of("actionFactory", new ActionFactory(hookSystem, moduleManager, new User("test", "asdasdfasdf", new String[]{"manager"}))));
}).doesNotThrowAnyException();
}

Expand Down
33 changes: 33 additions & 0 deletions modules/ui-module/src/test/resources/manager/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<!--
#%L
ui-module
%%
Copyright (C) 2023 - 2025 CondationCMS
%%
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public
License along with this program. If not, see
<http://www.gnu.org/licenses/gpl-3.0.html>.
#L%
-->

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>TODO write content</div>
</body>
</html>