From 2ef2de7d50c7ca2167794ead6c97760d769cafaa Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Fri, 20 Dec 2019 17:38:39 +0100 Subject: [PATCH] Adjustments for Freemarker, especially template laoding --- ccm-bundle-devel/pom.xml | 5 ++ .../themes/freemarker/ccm-cms/pages.ftl | 27 +++++---- .../FreemarkerConfigurationProvider.java | 35 ++++++++--- .../freemarker/ccm-core/user-banner.ftl | 27 ++++++++- ccm-theme-ftllibs-devel/.gitignore | 1 + ccm-theme-ftllibs-devel/pom.xml | 52 +++++++++++++++++ .../themes/ftllibs-devel/page.html.ftl | 58 +++++++++++++++++++ .../resources/themes/ftllibs-devel/theme.json | 6 ++ pom.xml | 1 + 9 files changed, 191 insertions(+), 21 deletions(-) create mode 100644 ccm-theme-ftllibs-devel/.gitignore create mode 100644 ccm-theme-ftllibs-devel/pom.xml create mode 100644 ccm-theme-ftllibs-devel/src/main/resources/themes/ftllibs-devel/page.html.ftl create mode 100644 ccm-theme-ftllibs-devel/src/main/resources/themes/ftllibs-devel/theme.json diff --git a/ccm-bundle-devel/pom.xml b/ccm-bundle-devel/pom.xml index 3c9be3ae7..15c5c2b18 100644 --- a/ccm-bundle-devel/pom.xml +++ b/ccm-bundle-devel/pom.xml @@ -38,6 +38,11 @@ ccm-theme-foundry ${project.parent.version} + + org.librecms + ccm-theme-ftllibs-devel + ${project.parent.version} + net.sf.saxon diff --git a/ccm-cms/src/main/resources/themes/freemarker/ccm-cms/pages.ftl b/ccm-cms/src/main/resources/themes/freemarker/ccm-cms/pages.ftl index 7269754ac..0965b694e 100644 --- a/ccm-cms/src/main/resources/themes/freemarker/ccm-cms/pages.ftl +++ b/ccm-cms/src/main/resources/themes/freemarker/ccm-cms/pages.ftl @@ -86,23 +86,25 @@ Get the URL of the root category of the navigation with the provided id. @param navigationId The ID of the navigation system to use. + @param containerId Container of the category menu @return The URL of the root category of the navigation system with the provided ID. --> -<#function getNavigationRootUrl navigationId="categoryMenu"> - <#return [navigationId].url> +<#function getNavigationRootUrl navigationId="categoryMenu" containerId="container"> + <#return [container][navigationId].url> <#--doc Get title of the navigation with the provided id. @param navigationId The ID of the navigation system to use. + @param containerId Container of the category menu @return The title of the navigation. --> -<#function getNavigationTitle navigationId="categoryMenu"> - <#return [navigationId].categoryTitle> +<#function getNavigationTitle navigationId="categoryMenu" containerId="container"> + <#return [containerId][navigationId].categoryTitle> <#--doc @@ -110,11 +112,12 @@ provided ID. If no id is provided 'categoryNav' is used. @param hierarchyId The ID of the category hierachy to use. + @param containerId Container of the category hierarchy @return The first level of categories in the hierarchy. --> -<#function getCategoryHierarchy hierarchyId="categoryNav"> - <#return [hierarchyId].subCategories> +<#function getCategoryHierarchy hierarchyId="categoryNav" containerId="container"> + <#return [containerId][hierarchyId].subCategories> <#--doc @@ -129,22 +132,24 @@ <#--doc - Gets the subcategories of the category with the provided id. + Not longer supported @param categoryId The ID of the category to use. - @return The sub categories of the category with the provided ID. + @return Nothing --> <#function getSubCategoriesOfCategoryWithId categoryId> - <#return model["/bebop:page/nav:categoryMenu//nav:category[@id=${categoryId}]/nav:category"]> + <#return ""> <#--doc Gets the greeting/index item of the current navigation page. The returned model can be processed with usual functions for processing content items. + @param containerId Container of the index item. + @return The model of the index item. --> -<#function getGreetingItem path="greetingItem"> - <#return eval(path)> +<#function getGreetingItem containerId="container"> + <#return [container].greetingItem> \ No newline at end of file diff --git a/ccm-core/src/main/java/org/libreccm/theming/freemarker/FreemarkerConfigurationProvider.java b/ccm-core/src/main/java/org/libreccm/theming/freemarker/FreemarkerConfigurationProvider.java index 7864023bf..e7d5f573c 100644 --- a/ccm-core/src/main/java/org/libreccm/theming/freemarker/FreemarkerConfigurationProvider.java +++ b/ccm-core/src/main/java/org/libreccm/theming/freemarker/FreemarkerConfigurationProvider.java @@ -18,8 +18,10 @@ */ package org.libreccm.theming.freemarker; +import freemarker.cache.ClassTemplateLoader; +import freemarker.cache.MultiTemplateLoader; import freemarker.cache.TemplateLoader; -import freemarker.cache.TemplateLookupStrategy; +import freemarker.cache.WebappTemplateLoader; import freemarker.template.Configuration; import freemarker.template.TemplateExceptionHandler; import org.libreccm.core.UnexpectedErrorException; @@ -32,9 +34,11 @@ import java.io.InputStreamReader; import java.io.Reader; import java.util.HashMap; import java.util.Map; +import java.util.Optional; import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject; +import javax.servlet.ServletContext; /** * @@ -43,6 +47,9 @@ import javax.inject.Inject; @ApplicationScoped class FreemarkerConfigurationProvider { + @Inject + private ServletContext servletContext; + @Inject private Themes themes; @@ -64,7 +71,18 @@ class FreemarkerConfigurationProvider { configuration.setLogTemplateExceptions(false); configuration.setWrapUncheckedExceptions(false); configuration.setLocalizedLookup(false); - configuration.setTemplateLoader(new CcmTemplateLoader(forTheme)); + + configuration.setTemplateLoader( + new MultiTemplateLoader(new TemplateLoader[]{ + // For for files from themes + new CcmTemplateLoader(forTheme), + // Loader for MacroLibs provided by CCM modules + new WebappTemplateLoader( + servletContext, "/themes/freemarker" + ), + new ClassTemplateLoader(getClass(), "/themes/freemarker") + }) + ); configurations.put(forTheme, configuration); @@ -83,13 +101,12 @@ class FreemarkerConfigurationProvider { @Override public Object findTemplateSource(final String name) throws IOException { - return themes - .getFileFromTheme(fromTheme, name) - .orElseThrow(() -> new UnexpectedErrorException(String - .format("Failed to open Freemarker Template \"%s\" from " - + "theme \"%s\".", - name, - fromTheme.getName()))); + final Optional source = themes.getFileFromTheme(fromTheme, name); + if (source.isPresent()) { + return source.get(); + } else { + return null; + } } @Override diff --git a/ccm-core/src/main/resources/themes/freemarker/ccm-core/user-banner.ftl b/ccm-core/src/main/resources/themes/freemarker/ccm-core/user-banner.ftl index d0064a6d6..2bde37871 100644 --- a/ccm-core/src/main/resources/themes/freemarker/ccm-core/user-banner.ftl +++ b/ccm-core/src/main/resources/themes/freemarker/ccm-core/user-banner.ftl @@ -72,22 +72,47 @@ Retrieves the link to the login form. Only available if the current user is not logged in. + @depcrecated Use getLoginUrl() instead + @return The link to the login form. --> <#function getLoginLink> - <#return currentUser.loginLink> + <#return getLoginUrl()> + + +<#--doc + Retrieves the URL of the login form. Only available if the current user + is not logged in. + + @return The link to the login form. +--> +<#function getLoginUrl> + <#return currentUser.loginUrl> <#--doc Retrieves the link for logging out. Only available if the current user is logged in. + @depcreated Use getLogoutUrl instead + @return The link for logging out. --> <#function getLogoutLink> + <#return getLogoutUrl()> + + +<#--doc + Retrieves the URL for logging out. Only available if the current user + is logged in. + + @return The link for logging out. +--> +<#function getLogoutUrl> <#return currentUser.logoutUrl> + <#--doc Retrieves the screen name (user name) of the current user. Only available if the current user is logged in. diff --git a/ccm-theme-ftllibs-devel/.gitignore b/ccm-theme-ftllibs-devel/.gitignore new file mode 100644 index 000000000..eb5a316cb --- /dev/null +++ b/ccm-theme-ftllibs-devel/.gitignore @@ -0,0 +1 @@ +target diff --git a/ccm-theme-ftllibs-devel/pom.xml b/ccm-theme-ftllibs-devel/pom.xml new file mode 100644 index 000000000..43a94e4b9 --- /dev/null +++ b/ccm-theme-ftllibs-devel/pom.xml @@ -0,0 +1,52 @@ + + + + 4.0.0 + + + libreccm-parent + org.libreccm + 7.0.0-SNAPSHOT + + + + UTF-8 + ${maven.build.timestamp} + yyyy-MM-dd'T'HH:mm:ss'Z'Z + + + org.librecms + ccm-theme-ftllibs-devel + + FTL Libs Devel theme + + + + Lesser GPL 2.1 + http://www.gnu.org/licenses/old-licenses/lgpl-2.1 + + + + + + + org.libreccm + ccm-core + ${project.parent.version} + + + + org.librecms + ccm-cms + ${project.parent.version} + + + + + + + + + + + \ No newline at end of file diff --git a/ccm-theme-ftllibs-devel/src/main/resources/themes/ftllibs-devel/page.html.ftl b/ccm-theme-ftllibs-devel/src/main/resources/themes/ftllibs-devel/page.html.ftl new file mode 100644 index 000000000..833e9515e --- /dev/null +++ b/ccm-theme-ftllibs-devel/src/main/resources/themes/ftllibs-devel/page.html.ftl @@ -0,0 +1,58 @@ +<#import "/ccm-core/user-banner.ftl" as UserBanner> + + + + + FTL Libs Devel + + +

FTL Libs Test

+

user-banner.ftl

+
+
+
UserBanner.getGreeting
+
${UserBanner.getGreeting()}
+
+
+
UserBanner.isLoggedIn
+
${UserBanner.isLoggedIn()?c}
+
+
+
UserBanner.isAuthenticated
+
${UserBanner.isAuthenticated()?c}
+
+
+
UserBanner.isNotLoggedIn
+
${UserBanner.isNotLoggedIn()?c}
+
+
+
UserBanner.isNotAuthenticated
+
${UserBanner.isNotAuthenticated()?c}
+
+
+
UserBanner.getChangePasswordUrl
+
${UserBanner.getChangePasswordUrl()}
+
+
+
UserBanner.getLoginLink
+
${UserBanner.getLoginLink()}
+
+
+
UserBanner.getLogoutLink
+
${UserBanner.getLogoutLink()}
+
+
+
UserBanner.getScreenName
+
${UserBanner.getScreenName()}
+
+
+
UserBanner.getUserGivenName
+
${UserBanner.getUserGivenName()}
+
+
+
UserBanner.getUserFamilyName
+
${UserBanner.getUserFamilyName()}
+
+
+ + \ No newline at end of file diff --git a/ccm-theme-ftllibs-devel/src/main/resources/themes/ftllibs-devel/theme.json b/ccm-theme-ftllibs-devel/src/main/resources/themes/ftllibs-devel/theme.json new file mode 100644 index 000000000..78d6ce268 --- /dev/null +++ b/ccm-theme-ftllibs-devel/src/main/resources/themes/ftllibs-devel/theme.json @@ -0,0 +1,6 @@ +{ + "name": "ftllibs-devel", + "type": "freemarker", + + "default-template": "page.html.ftl" +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index f1de65322..0f0f1cc32 100644 --- a/pom.xml +++ b/pom.xml @@ -79,6 +79,7 @@ ccm-theme-foundry + ccm-theme-ftllibs-devel ccm-bundle-devel