🎨 支持使用自定义模板

pull/41/head
ruibaby 2018-10-26 16:43:43 +08:00
parent 3d9ad78854
commit fa46456abf
5 changed files with 66 additions and 8 deletions

View File

@ -130,6 +130,11 @@ public class Post implements Serializable {
*/
private Integer allowComment = 0;
/**
*
*/
private String customTpl;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm")
public Date getPostDate() {
return postDate;

View File

@ -219,6 +219,33 @@ public class HaloUtils {
return tpls;
}
/**
*
* page_xxx
*
* @return List
*/
public static List<String> getCustomTpl(String theme) {
List<String> tpls = new ArrayList<>();
try {
File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
//获取主题路径
File themePath = new File(basePath.getAbsolutePath(), "templates/themes/" + theme);
File[] themeFiles = themePath.listFiles();
if (null != themeFiles && themeFiles.length > 0) {
for (File file : themeFiles) {
String[] split = StrUtil.removeSuffix(file.getName(), ".ftl").split("_");
if (split.length == 2 && "page".equals(split[0])) {
tpls.add(StrUtil.removeSuffix(file.getName(), ".ftl"));
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return tpls;
}
/**
*
*

View File

@ -11,6 +11,7 @@ import cc.ryanc.halo.service.GalleryService;
import cc.ryanc.halo.service.LinkService;
import cc.ryanc.halo.service.LogsService;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.utils.HaloUtils;
import cc.ryanc.halo.utils.LocaleMessageUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
@ -196,14 +197,16 @@ public class PageController {
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.delete-success"));
}
/**
*
*
* @param model model
* @return admin/admin_page_md_editor
*/
@GetMapping(value = "/new")
public String newPage() {
public String newPage(Model model) {
List<String> customTpls = HaloUtils.getCustomTpl(HaloConst.OPTIONS.get(BlogPropertiesEnum.THEME.getProp()));
model.addAttribute("customTpls",customTpls);
return "admin/admin_page_md_editor";
}
@ -255,7 +258,9 @@ public class PageController {
@GetMapping(value = "/edit")
public String editPage(@RequestParam("pageId") Long pageId, Model model) {
Optional<Post> post = postService.findByPostId(pageId);
List<String> customTpls = HaloUtils.getCustomTpl(HaloConst.OPTIONS.get(BlogPropertiesEnum.THEME.getProp()));
model.addAttribute("post", post.get());
model.addAttribute("customTpls",customTpls);
return "admin/admin_page_md_editor";
}

View File

@ -103,6 +103,11 @@ public class FrontPageController extends BaseController {
model.addAttribute("commentsCount", comments.size());
model.addAttribute("rainbow", rainbow);
postService.updatePostView(post);
//如果设置了自定义模板,则渲染自定义模板
if(StrUtil.isNotEmpty(post.getCustomTpl())){
return this.render(post.getCustomTpl());
}
return this.render("page");
}
}

View File

@ -69,11 +69,26 @@
</div>
</div>
<div class="box-body">
<label for="allowComment" class="control-label"><@spring.message code='admin.editor.allow-comment' /></label>
<select class="form-control" id="allowComment" name="allowComment">
<option value="1" <#if post?? && post.allowComment?default(1)==1>selected</#if>><@spring.message code='common.select.yes' /></option>
<option value="0" <#if post?? && post.allowComment?default(1)==0>selected</#if>><@spring.message code='common.select.no' /></option>
</select>
<div class="form-group">
<label for="allowComment" class="control-label"><@spring.message code='admin.editor.allow-comment' /></label>
<select class="form-control" id="allowComment" name="allowComment">
<option value="1" <#if post?? && post.allowComment?default(1)==1>selected</#if>><@spring.message code='common.select.yes' /></option>
<option value="0" <#if post?? && post.allowComment?default(1)==0>selected</#if>><@spring.message code='common.select.no' /></option>
</select>
</div>
<div class="form-group">
<label for="customTpl" class="control-label">自定义模板:</label>
<select class="form-control" id="customTpl" name="customTpl">
<#if customTpls?? && customTpls?size gt 0>
<option value="">选择模板</option>
<#list customTpls as tpl>
<option value="${tpl}" <#if post?? && post.customTpl?if_exists == "${tpl}">selected</#if>>${tpl}</option>
</#list>
<#else>
<option value="">无自定义模板</option>
</#if>
</select>
</div>
</div>
<div class="box-footer">
<button onclick="push(1)" class="btn btn-default btn-sm "><@spring.message code='admin.editor.save-draft' /></button>
@ -207,7 +222,8 @@
'postContentMd': simplemde.value(),
'postContent': simplemde.markdown(simplemde.value()),
'postThumbnail': $('#selectImg').attr('src'),
'allowComment' : $('#allowComment').val()
'allowComment' : $('#allowComment').val(),
'customTpl' : $("#customTpl").val()
},
success: function (data) {
if(data.code==1){