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

View File

@ -1,10 +1,8 @@
package cc.ryanc.halo.model.freemarker.tag; package cc.ryanc.halo.model.freemarker.tag;
import cc.ryanc.halo.service.*;
import freemarker.core.Environment; import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody; import freemarker.template.*;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.IOException; import java.io.IOException;
@ -23,9 +21,55 @@ public class CommonTagDirective implements TemplateDirectiveModel {
private static final String METHOD_KEY = "method"; 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 @Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { 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"> <ul class="menu">
<#if newComments?size gt 0> <#if newComments?size gt 0>
<#assign x=0> <#assign x=0>
<#list newComments?sort_by("commentDate")?reverse as comment> <#list newComments?sort_by("createTime")?reverse as comment>
<#assign x = x+1> <#assign x = x+1>
<li> <li>
<a data-pjax="true" href="/admin/comments?status=1"> <a data-pjax="true" href="/admin/comments?status=1">
<div class="pull-left"> <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> </div>
<h4> <h4>
${comment.commentAuthor} ${comment.author!}
<small> <@common.timeline datetime="${comment.commentDate}"?datetime /></small> <small> <@common.timeline datetime="${comment.createTime}"?datetime /></small>
</h4> </h4>
<object>${comment.commentContent}</object> <object>${comment.content!}</object>
</a> </a>
</li> </li>
<#if x==10> <#if x==10>