Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add supports for provide theme templates in plugin class path #4862

Merged
merged 5 commits into from
Nov 30, 2023

Conversation

guqing
Copy link
Member

@guqing guqing commented Nov 16, 2023

What type of PR is this?

/kind feature
/milestone 2.11.x
/area core

What this PR does / why we need it:

插件支持在 templates 目录提供默认模板

由于模板解释时只有模板名称无法区分该从哪里解析模板,默认是从主题,但插件提供的模板需要从插件的 classpath 下使用插件的 classloader 加载,所以需要特殊标记模板来让模板解析知道从哪个插件加载,规则为 plugin:{pluginName}:{templateName}
如在 fake-plugin 插件下的模板 layout.html 中定义一个 fragment 片段

<!DOCTYPE html>
<html data-theme="light" lang="en" th:fragment="html (content)">
<head>
<title>Demo layout</title>
</head>
<body>
<div>Hello layout</div>
</body>

那么在其他模板使用 fragment 时需要在 th:replace 的模板名称前加上前缀

<!DOCTYPE html>
<html th:replace="plugin:fake-plugin:modules/layout :: html(content = ~{::content})"
      xmlns:th="https://www.thymeleaf.org">
<th:block th:fragment="content">
 <div>This is content</div>
</th:block>
</html>

Which issue(s) this PR fixes:

Fixes #4858

Does this PR introduce a user-facing change?

插件支持在 templates 目录提供默认模板

@f2c-ci-robot f2c-ci-robot bot added kind/feature Categorizes issue or PR as related to a new feature. release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Nov 16, 2023
@f2c-ci-robot f2c-ci-robot bot added this to the 2.11.x milestone Nov 16, 2023
@f2c-ci-robot f2c-ci-robot bot added the area/core Issues or PRs related to the Halo Core label Nov 16, 2023
@f2c-ci-robot f2c-ci-robot bot requested review from LIlGG and minliacom November 16, 2023 04:20
Copy link

codecov bot commented Nov 17, 2023

Codecov Report

Attention: 36 lines in your changes are missing coverage. Please review.

Comparison is base (457e059) 56.88% compared to head (1e8d594) 56.85%.
Report is 38 commits behind head on main.

Files Patch % Lines
...heme/engine/PluginClassloaderTemplateResolver.java 58.13% 17 Missing and 1 partial ⚠️
...un/halo/app/theme/DefaultTemplateNameResolver.java 0.00% 15 Missing ⚠️
.../halo/app/plugin/PluginApplicationInitializer.java 0.00% 2 Missing ⚠️
...java/run/halo/app/theme/TemplateEngineManager.java 90.90% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #4862      +/-   ##
============================================
- Coverage     56.88%   56.85%   -0.03%     
- Complexity     2975     2984       +9     
============================================
  Files           514      516       +2     
  Lines         16912    16984      +72     
  Branches       1273     1280       +7     
============================================
+ Hits           9621     9657      +36     
- Misses         6750     6784      +34     
- Partials        541      543       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@JohnNiang JohnNiang modified the milestones: 2.11.x, 2.11.0 Nov 24, 2023
@guqing
Copy link
Member Author

guqing commented Nov 24, 2023

/ping @halo-dev/sig-halo

Copy link
Member

@ruibaby ruibaby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@f2c-ci-robot f2c-ci-robot bot added the lgtm Indicates that a PR is ready to be merged. label Nov 24, 2023
@JohnNiang
Copy link
Member

Hi @guqing ,如果多个插件中都提供了模板 halo.html,访问页面 /halo 的模板选择策略是怎么样的呢?

@guqing
Copy link
Member Author

guqing commented Nov 30, 2023

Hi @guqing ,如果多个插件中都提供了模板 halo.html,访问页面 /halo 的模板选择策略是怎么样的呢?

插件只会访问到插件自己的模板,因为使用的时候要求加上自己插件名如 ServerResponse.ok().render('plugin:fake-plugin:halo.html', Map.of()); 如果不加前缀访问的会是主题的模板,以下是一个可能的使用示例:
image

它表示优先使用主题的 doc.html 模板,如果模板不存在就会返回 plugin:docsme:doc 作为模板名然后会使用到插件的 classpath:/templates/doc.html

https://github.com/halo-dev/halo/pull/4862/files#diff-b01d609e06755f8f0fd8f83857b0fac7964c881659306d707ee6145d9ba12d06R30

如果说多个插件都定义了路由 /halo 那么路由就冲突了要看路由会访问到哪个插件,而如果路由没有冲突的情况下使用哪个插件的路由就是用哪个插件的模板

@ruibaby
Copy link
Member

ruibaby commented Nov 30, 2023

Hi @guqing ,如果多个插件中都提供了模板 halo.html,访问页面 /halo 的模板选择策略是怎么样的呢?

模板是插件只能访问自己的模板,但如果多个插件定义了相同的路由那就是路由冲突的问题了。

@JohnNiang
Copy link
Member

明白了。PR 描述中确实没有看出来具体该怎么用。后续可能也需要完善一下插件开发文档。

Copy link
Member

@JohnNiang JohnNiang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve

Copy link

f2c-ci-robot bot commented Nov 30, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: JohnNiang

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@f2c-ci-robot f2c-ci-robot bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 30, 2023
@JohnNiang JohnNiang merged commit f659a32 into halo-dev:main Nov 30, 2023
4 checks passed
@guqing guqing deleted the feature/plugin-template branch November 30, 2023 03:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/core Issues or PRs related to the Halo Core kind/feature Categorizes issue or PR as related to a new feature. lgtm Indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

期望插件可以支持在 templates 提供默认模板
3 participants