pull/137/head
ruibaby 2019-03-16 16:34:11 +08:00
parent e900016874
commit 450f473fcf
3 changed files with 103 additions and 48 deletions

View File

@ -1,8 +1,19 @@
package cc.ryanc.halo.config;
import cc.ryanc.halo.model.freemarker.method.RandomMethod;
import cc.ryanc.halo.model.freemarker.method.RecentCommentsMethod;
import cc.ryanc.halo.model.freemarker.method.RecentPostsMethod;
import cc.ryanc.halo.model.freemarker.tag.ArticleTagDirective;
import cc.ryanc.halo.model.freemarker.tag.CommonTagDirective;
import cc.ryanc.halo.service.OptionService;
import cc.ryanc.halo.service.UserService;
import freemarker.template.TemplateModelException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
/**
* <pre>
* FreeMarker
@ -15,43 +26,43 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class FreeMarkerAutoConfiguration {
// @Autowired
// private freemarker.template.Configuration configuration;
//
// @Autowired
// private OptionsService optionsService;
//
// @Autowired
// private UserService userService;
//
// @Autowired
// private CommonTagDirective commonTagDirective;
//
// @Autowired
// private ArticleTagDirective articleTagDirective;
//
// @Autowired
// private RandomMethod randomMethod;
//
// @Autowired
// private RecentPostsMethod recentPostsMethod;
//
// @Autowired
// private RecentCommentsMethod recentCommentsMethod;
//
// @PostConstruct
// public void setSharedVariable() {
// try {
// //自定义标签
// configuration.setSharedVariable("commonTag", commonTagDirective);
// configuration.setSharedVariable("articleTag", articleTagDirective);
// configuration.setSharedVariable("options", optionsService.findAllOptions());
@Autowired
private freemarker.template.Configuration configuration;
@Autowired
private OptionService optionsService;
@Autowired
private UserService userService;
@Autowired
private CommonTagDirective commonTagDirective;
@Autowired
private ArticleTagDirective articleTagDirective;
@Autowired
private RandomMethod randomMethod;
@Autowired
private RecentPostsMethod recentPostsMethod;
@Autowired
private RecentCommentsMethod recentCommentsMethod;
@PostConstruct
public void setSharedVariable() {
try {
//自定义标签
configuration.setSharedVariable("commonTag", commonTagDirective);
configuration.setSharedVariable("articleTag", articleTagDirective);
configuration.setSharedVariable("options", optionsService.listOptions());
// configuration.setSharedVariable("user", userService.findUser());
// configuration.setSharedVariable("randomMethod", randomMethod);
// configuration.setSharedVariable("recentPostsMethod", recentPostsMethod);
// configuration.setSharedVariable("recentCommentsMethod", recentCommentsMethod);
// } catch (TemplateModelException e) {
// log.error("Custom tags failed to load{}", e.getMessage());
// }
// }
configuration.setSharedVariable("randomMethod", randomMethod);
configuration.setSharedVariable("recentPostsMethod", recentPostsMethod);
configuration.setSharedVariable("recentCommentsMethod", recentCommentsMethod);
} catch (TemplateModelException e) {
log.error("Custom tags failed to load{}", e.getMessage());
}
}
}

View File

@ -1,10 +1,8 @@
package cc.ryanc.halo.model.freemarker.tag;
import cc.ryanc.halo.service.*;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.*;
import org.springframework.stereotype.Component;
import java.io.IOException;
@ -23,9 +21,55 @@ public class CommonTagDirective implements TemplateDirectiveModel {
private static final String METHOD_KEY = "method";
private final MenuService menuService;
private final CategoryService categoryService;
private final TagService tagService;
private final LinkService linkService;
private final CommentService commentService;
public CommonTagDirective(MenuService menuService,
CategoryService categoryService,
TagService tagService,
LinkService linkService,
CommentService commentService) {
this.menuService = menuService;
this.categoryService = categoryService;
this.tagService = tagService;
this.linkService = linkService;
this.commentService = commentService;
}
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
// TODO Complete comment tag directive.
final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
if (params.containsKey(METHOD_KEY)) {
String method = params.get(METHOD_KEY).toString();
switch (method) {
case "menus":
env.setVariable("menus", builder.build().wrap(menuService.listAll()));
break;
case "categories":
env.setVariable("categories", builder.build().wrap(categoryService.listAll()));
break;
case "tags":
env.setVariable("tags", builder.build().wrap(tagService.listAll()));
break;
case "links":
env.setVariable("links", builder.build().wrap(linkService.listAll()));
break;
case "newComments":
env.setVariable("newComments", builder.build().wrap(commentService.listAll()));
break;
default:
break;
}
}
body.render(env.getOut());
}
}

View File

@ -26,18 +26,18 @@
<ul class="menu">
<#if newComments?size gt 0>
<#assign x=0>
<#list newComments?sort_by("commentDate")?reverse as comment>
<#list newComments?sort_by("createTime")?reverse as comment>
<#assign x = x+1>
<li>
<a data-pjax="true" href="/admin/comments?status=1">
<div class="pull-left">
<img src="//gravatar.loli.net/avatar/${comment.commentAuthorAvatarMd5?default("hash")}?s=256&d=${options.native_comment_avatar?default("mm")}" class="img-circle" alt="User Image">
<img src="//gravatar.loli.net/avatar/${comment.gavatarMd5!'hash'}?s=256&d=${options.native_comment_avatar!'mm'}" class="img-circle" alt="User Image">
</div>
<h4>
${comment.commentAuthor}
<small> <@common.timeline datetime="${comment.commentDate}"?datetime /></small>
${comment.author!}
<small> <@common.timeline datetime="${comment.createTime}"?datetime /></small>
</h4>
<object>${comment.commentContent}</object>
<object>${comment.content!}</object>
</a>
</li>
<#if x==10>