-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the problem of not using error template in theme (#3166)
#### What type of PR is this? /kind bug /area core #### What this PR does / why we need it: Currently, if there is no `error.html` error template in theme, but there is a `404.html` error template, this will not work correctly. We always get rendering result from global error template `error.html`. This PR mainly provides a `ThemeTemplateAvailabilityProvider` to check if the template is available in theme instead of in globally predefined templates. #### Which issue(s) this PR fixes: Fixes #3062 #### Special notes for your reviewer: 1. Download and install any theme 2. Check the theme folder 3. Check folder `templates/error` 4. Try to remove `templates/error/error.html` template file 5. Create `templates/error/404.html` 6. Request a page which does not exist 7. See the result #### Does this PR introduce a user-facing change? ```release-note 解决主题自定义错误模板不生效的问题。 ```
- Loading branch information
Showing
8 changed files
with
130 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/run/halo/app/theme/engine/DefaultThemeTemplateAvailabilityProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package run.halo.app.theme.engine; | ||
|
||
import java.nio.file.Files; | ||
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties; | ||
import org.springframework.stereotype.Component; | ||
import run.halo.app.theme.ThemeContext; | ||
|
||
@Component | ||
public class DefaultThemeTemplateAvailabilityProvider implements ThemeTemplateAvailabilityProvider { | ||
|
||
private final ThymeleafProperties thymeleafProperties; | ||
|
||
public DefaultThemeTemplateAvailabilityProvider(ThymeleafProperties thymeleafProperties) { | ||
this.thymeleafProperties = thymeleafProperties; | ||
} | ||
|
||
@Override | ||
public boolean isTemplateAvailable(ThemeContext themeContext, String viewName) { | ||
var suffix = thymeleafProperties.getSuffix(); | ||
// Currently, we only support Path here. | ||
var path = themeContext.getPath().resolve("templates").resolve(viewName + suffix); | ||
return Files.exists(path); | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/run/halo/app/theme/engine/ThemeTemplateAvailabilityProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package run.halo.app.theme.engine; | ||
|
||
import run.halo.app.theme.ThemeContext; | ||
|
||
public interface ThemeTemplateAvailabilityProvider { | ||
|
||
boolean isTemplateAvailable(ThemeContext themeContext, String viewName); | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
src/test/java/run/halo/app/theme/engine/DefaultThemeTemplateAvailabilityProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package run.halo.app.theme.engine; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.Path; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties; | ||
import org.springframework.util.ResourceUtils; | ||
import run.halo.app.theme.ThemeContext; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class DefaultThemeTemplateAvailabilityProviderTest { | ||
|
||
@InjectMocks | ||
DefaultThemeTemplateAvailabilityProvider provider; | ||
|
||
@Mock | ||
ThymeleafProperties thymeleafProperties; | ||
|
||
@Test | ||
void templateAvailableTest() throws FileNotFoundException, URISyntaxException { | ||
var themeUrl = ResourceUtils.getURL("classpath:themes/default"); | ||
var themePath = Path.of(themeUrl.toURI()); | ||
|
||
when(thymeleafProperties.getSuffix()).thenReturn(".html"); | ||
var themeContext = ThemeContext.builder() | ||
.name("default") | ||
.path(themePath) | ||
.build(); | ||
boolean templateAvailable = provider.isTemplateAvailable(themeContext, "fake"); | ||
assertFalse(templateAvailable); | ||
|
||
templateAvailable = provider.isTemplateAvailable(themeContext, "index"); | ||
assertTrue(templateAvailable); | ||
|
||
templateAvailable = provider.isTemplateAvailable(themeContext, "timezone"); | ||
assertTrue(templateAvailable); | ||
} | ||
} |