Skip to content

Commit

Permalink
use node, cms and mod namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
thmarx committed Nov 16, 2024
1 parent c778cfa commit 6c68c78
Show file tree
Hide file tree
Showing 23 changed files with 131 additions and 58 deletions.
2 changes: 2 additions & 0 deletions cms-api/src/main/java/com/condation/cms/api/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public static class ContentTypes {

public static final String DEFAULT_CACHE_ENGINE = "local";
public static final boolean DEFAULT_CONTENT_CACHE_ENABLED = false;

public static final String DEFAULT_MODULE_NAMESPACE = "mod";

public static class Taxonomy {
public static final String DEFAULT_TEMPLATE = "taxonomy.html";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.condation.cms.api.extensions;

import java.util.Map;

import com.condation.cms.api.Constants;

/*-
* #%L
* cms-api
Expand All @@ -26,6 +30,21 @@

public abstract class TemplateModelExtendingExtensionPoint extends AbstractExtensionPoint{

/**
* deprecated: use @TemplateModelExtendingExtensionPoint.getModel instead
* @param model
*/
@Deprecated(since = "7.3.0", forRemoval = true)
public abstract void extendModel (TemplateEngine.Model model);


public Map<String, Object> getModel () {
TemplateEngine.Model model = new TemplateEngine.Model(null, null);
extendModel(model);
return model.values;
}

public String getNamespace () {
return Constants.DEFAULT_MODULE_NAMESPACE;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.condation.cms.api.extensions;

import java.util.Map;

import com.condation.cms.api.Constants;

/*-
* #%L
* cms-api
Expand Down Expand Up @@ -35,4 +39,13 @@ public abstract class TemplateModelExtendingExtentionPoint extends AbstractExten

public abstract void extendModel (TemplateEngine.Model model);

public Map<String, Object> getModel () {
TemplateEngine.Model model = new TemplateEngine.Model(null, null);
extendModel(model);
return model.values;
}

public String getNamespace () {
return Constants.DEFAULT_MODULE_NAMESPACE;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.condation.cms.content;

import com.condation.cms.api.Constants;
/*-
* #%L
* cms-content
Expand Down Expand Up @@ -146,8 +147,8 @@ public String render(final ReadOnlyFile contentFile, final RequestContext contex
model.values.put("meta", new MapAccess(meta));
model.values.put("sections", sections);

namespace.add("page", "meta", new MapAccess(meta));
namespace.add("page", "sections", sections);
namespace.add("node", "meta", new MapAccess(meta));
namespace.add("node", "sections", sections);

ShortCodeTemplateFunction shortCodeFunction = createShortCodeFunction(context);
model.values.put("shortCodes", shortCodeFunction);
Expand All @@ -169,9 +170,11 @@ public String render(final ReadOnlyFile contentFile, final RequestContext contex
model.values.put("theme", context.get(RenderContext.class).theme());
model.values.put("site", siteProperties);
model.values.put("mediaService", context.get(SiteMediaServiceFeature.class).mediaService());
namespace.add("cms", "mediaService", context.get(SiteMediaServiceFeature.class).mediaService());

model.values.put("taxonomies", context.get(InjectorFeature.class).injector().getInstance(TaxonomyFunction.class));

namespace.add("cms", "taxonomies", context.get(InjectorFeature.class).injector().getInstance(TaxonomyFunction.class));

var theme = context.get(RenderContext.class).theme();
if (theme.empty()) {
model.values.put("messages", context.get(InjectorFeature.class).injector().getInstance(MessageSource.class));
Expand All @@ -180,8 +183,10 @@ public String render(final ReadOnlyFile contentFile, final RequestContext contex
}

model.values.put("hooks", context.get(HookSystemFeature.class).hookSystem());
namespace.add("cms", "hooks", context.get(HookSystemFeature.class).hookSystem());

model.values.put("links", new LinkFunction(context));
namespace.add("cms", "links", new LinkFunction(context));

model.values.put("PREVIEW_MODE", isPreview(context));
model.values.put("DEV_MODE", isDevMode(context));
Expand All @@ -193,16 +198,18 @@ public String render(final ReadOnlyFile contentFile, final RequestContext contex

context.get(TemplateHooks.class).getTemplateSupplier().getRegisterTemplateSupplier().forEach(service -> {
model.values.put(service.name(), service.supplier());
namespace.add(Constants.DEFAULT_MODULE_NAMESPACE, service.name(), service.supplier());
});
context.get(TemplateHooks.class).getTemplateFunctions().getRegisterTemplateFunctions().forEach(service -> {
model.values.put(service.name(), service.function());
namespace.add(Constants.DEFAULT_MODULE_NAMESPACE, service.name(), service.function());
});

extendModel(model);
extendModel(model, namespace);

String content = renderContent(rawContent, context, model);
model.values.put("content", content);
namespace.add("page", "content", content);
namespace.add("node", "content", content);

model.values.putAll(namespace.getNamespaces());

Expand Down Expand Up @@ -244,9 +251,27 @@ private boolean isDevMode(final RequestContext context) {
return context.has(IsDevModeFeature.class);
}

private void extendModel(final TemplateEngine.Model model) {
moduleManager.extensions(TemplateModelExtendingExtentionPoint.class).forEach(extensionPoint -> extensionPoint.extendModel(model));
moduleManager.extensions(TemplateModelExtendingExtensionPoint.class).forEach(extensionPoint -> extensionPoint.extendModel(model));
private void extendModel(final TemplateEngine.Model model, Namespace namespace) {
moduleManager.extensions(TemplateModelExtendingExtentionPoint.class).forEach(extensionPoint -> {
var modModel = extensionPoint.getModel();
// deprecated: module extensions on root will be remove in 8.0.0
model.values.putAll(modModel);
modModel.entrySet().forEach(entry -> namespace.add(
extensionPoint.getNamespace(),
entry.getKey(),
entry.getValue()
));
});
moduleManager.extensions(TemplateModelExtendingExtensionPoint.class).forEach(extensionPoint -> {
var modModel = extensionPoint.getModel();
// deprecated: module extensions on root will be remove in 8.0.0
model.values.putAll(modModel);
modModel.entrySet().forEach(entry -> namespace.add(
extensionPoint.getNamespace(),
entry.getKey(),
entry.getValue()
));
});
}

@Override
Expand Down
22 changes: 22 additions & 0 deletions cms-content/src/main/java/com/condation/cms/content/Namespace.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
package com.condation.cms.content;

/*-
* #%L
* cms-content
* %%
* Copyright (C) 2023 - 2024 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%
*/

import java.util.HashMap;
import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.condation.cms.modules.example;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.condation.cms.api.extensions.TemplateModelExtendingExtensionPoint;
Expand Down Expand Up @@ -40,7 +41,11 @@ public class ExampleTemplateModelExtendingExtensionEndPoint extends TemplateMode

@Override
public void extendModel(TemplateEngine.Model model) {
model.values.put("searcher", new Searcher());
}

@Override
public Map<String, Object> getModel() {
return Map.of("searcher", new Searcher());
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions test-server/hosts/features/site-dev.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[modules]
active = [ "example-module" ]
3 changes: 0 additions & 3 deletions test-server/hosts/features/site-dev.yaml

This file was deleted.

15 changes: 15 additions & 0 deletions test-server/hosts/features/site.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
id = "feature-site"
hostname = [ "localhost2" ]
baseurl = "http://localhost2:1010"
language = "en"
test = "Hallo"
theme = "test"

[cache]
content = true

[query.index]
mode = "PERSISTENT"

[modules]
active = [ "forms-module" ]
32 changes: 0 additions & 32 deletions test-server/hosts/features/site.yaml

This file was deleted.

5 changes: 5 additions & 0 deletions test-server/hosts/features/templates/start.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ <h3>Call ShortCode from template</h3>
[(${shortCodes.call('hello', #{'name': 'CondationCMS'})})]
</div>

<div class="container">
<h3>Test mod-Namespace</h3>
<p th:text="${mod.searcher.search('query').total}"></p>
</div>

<th:block th:replace="libs/fragments.html :: footer"></th:block>

</body>
Expand Down
2 changes: 1 addition & 1 deletion test-server/themes/demo/templates/libs/fragments.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

<th:block th:fragment="header">
<title th:text="${page.meta.title}"></title>
<title th:text="${node.meta.title}"></title>
<link rel="canonical" th:href="${site.get('baseurl')} + ${requestContext.uri}" />
<link rel="shortcut icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1">
Expand Down
2 changes: 1 addition & 1 deletion test-server/themes/demo/templates/start.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<body class="centered">

<div id="content">
<div class="container" th:utext="${content}"></div>
<div class="container" th:utext="${node.content}"></div>
</div>

</body>
Expand Down
2 changes: 1 addition & 1 deletion test-server/themes/parent/templates/blog-entry.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<div class="row">
<h2 th:utext="${title}"></h2>
<p th:utext="${content}"></p>
<p th:utext="${node.content}"></p>
</div>

<div class="row"
Expand Down
2 changes: 1 addition & 1 deletion test-server/themes/parent/templates/blog.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<h1 th:text="${title}"></h1>
</div>

<div class="row" th:utext="${content}"></div>
<div class="row" th:utext="${node.content}"></div>
<div class="row"
th:with='page = ${nodeList.from("/blog/*").sort("publish_date").reverse(true).page(pageNumber).size(1).list()}'>
<th:block th:each="entry : ${page.items}" data-test="e">
Expand Down
2 changes: 1 addition & 1 deletion test-server/themes/parent/templates/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<nav th:replace="libs/fragments.html :: navigation"></nav>

<div id="content">
<div class="container" th:utext="${content}"></div>
<div class="container" th:utext="${node.content}"></div>

<div class="container" th:if="${sections.containsKey('part')}">
<th:block th:each="part : ${sections.get('part')}">
Expand Down
4 changes: 2 additions & 2 deletions test-server/themes/parent/templates/content.part.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="card mb-3" xmlns:th="http://www.thymeleaf.org">
<div class="card-body">
<h5 class="card-title" th:text="${meta.title}"></h5>
<p class="card-text" th:utext="${content}"></p>
<h5 class="card-title" th:text="${node.meta.title}"></h5>
<p class="card-text" th:utext="${node.content}"></p>
<!--a href="#" class="card-link">readmore</a-->
</div>
</div>
2 changes: 1 addition & 1 deletion test-server/themes/parent/templates/error.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</th:block>
</head>

<body th:utext="${content}">
<body th:utext="${node.content}">
</body>

</html>
2 changes: 1 addition & 1 deletion test-server/themes/parent/templates/libs/fragments.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

<th:block th:fragment="header">
<title th:text="${meta.title}"></title>
<title th:text="${node.meta.title}"></title>
<link rel="canonical" th:href="${site.get('baseurl')} + ${requestContext.uri}" />
<link rel="shortcut icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
Expand Down
2 changes: 1 addition & 1 deletion test-server/themes/parent/templates/navigation.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</div>

<div id="content">
<div class="container" th:utext="${content}"></div>
<div class="container" th:utext="${node.content}"></div>
</div>

<th:block th:replace="libs/fragments.html :: footer"></th:block>
Expand Down
2 changes: 1 addition & 1 deletion test-server/themes/parent/templates/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<nav th:replace="libs/fragments.html :: navigation"></nav>

<div class="container" th:utext="${content}"></div>
<div class="container" th:utext="${node.content}"></div>

<div class="container">
<div th:if="USERNAME != null">
Expand Down
2 changes: 1 addition & 1 deletion test-server/themes/parent/templates/start.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<nav th:replace="libs/fragments.html :: navigation"></nav>
<div id="content">
<div class="container" th:utext="${content}"></div>
<div class="container" th:utext="${node.content}"></div>
</div>
<th:block th:replace="libs/fragments.html :: footer"></th:block>

Expand Down
2 changes: 1 addition & 1 deletion test-server/themes/test/templates/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<h3>Overridden template from parent</h3>
</div>

<div class="container" th:utext="${content}"></div>
<div class="container" th:utext="${node.content}"></div>

<div class="container" th:if="${sections.containsKey('part')}">
<th:block th:each="part : ${sections.get('part')}">
Expand Down

0 comments on commit 6c68c78

Please sign in to comment.