Skip to content

Commit

Permalink
🎨 支持使用自定义模板
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby committed Oct 26, 2018
1 parent 3d9ad78 commit fa46456
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/main/java/cc/ryanc/halo/model/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/cc/ryanc/halo/utils/HaloUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,33 @@ public static List<String> getTplName(String theme) {
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;
}

/**
* 导出为文件
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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;
Expand Down Expand Up @@ -196,14 +197,16 @@ public JsonResult removeGallery(@RequestParam("galleryId") Long galleryId) {
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";
}

Expand Down Expand Up @@ -255,7 +258,9 @@ public JsonResult pushPage(@ModelAttribute Post post, HttpSession session) {
@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";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public String getPage(@PathVariable(value = "postUrl") String postUrl,
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");
}
}
28 changes: 22 additions & 6 deletions src/main/resources/templates/admin/admin_page_md_editor.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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){
Expand Down

0 comments on commit fa46456

Please sign in to comment.