从Freemarker中的相对链接获得绝对链接

问题描述 投票:3回答:1

我正在使用Magnolia-CMS创建网站。现在,我正在实现一个博客页面。在每个博客页面上,都有几个共享按钮。现在,我正在忙于实现twittershare按钮。在这里,我将使用推特卡。为此,我需要在metatag中提供图像的URL。主要问题:我像这样检索图像:$ {damfn.getAssetLink(content.blogImage)}。这只会返回我资源的相对路径。有没有一种快速的方法(在freemarker中),可以将tis转换为绝对链接?

非常感谢!

html freemarker magnolia
1个回答
0
投票

通常您在magnolia.properties中定义magnolia.default.base.url

然后您可以使用Components.getComponent(ServerConfiguration.class).getDefaultBaseUrl()进行检索

现在您必须将服务安装到freemarker中。您可以通过在启动时将安装程序任务添加到渲染器中来实现。您可以在模块版本处理程序中执行此操作。在那里,您覆盖了getStartupTasks(...),如下所示:

@Override
protected List<Task> getStartupTasks(InstallContext installContext) {
    final List<Task> tasks = new ArrayList<>();
    tasks.add(new InstallRendererContextAttributeTask("rendering", "freemarker", "serverConf", ServerConfiguration.class.getName()));
    tasks.add(new InstallRendererContextAttributeTask("site", "site", "serverConf", ServerConfiguration.class.getName()));
    return tasks;
}

现在您可以调用freemarker:

"${serverConf.defaultBaseUrl}/${ctx.contextPath}/${damfn.getAssetLink(content.blogImage)}"

检查斜线是否为必需,并确保在您的木兰配置中正确设置了defaultBaseUrl(“ / server / ...”)

edit:在freemarker ${Request}中调用当前请求应该会更容易,因此它可能类似于“ $ {Request.domain} / $ {ctx.contextPath} / $ {damfn.getAssetLink(content.blogImage) }”,而无需将serverConfiguration注入渲染器

© www.soinside.com 2019 - 2024. All rights reserved.