hexojs / hexojs/hexo

Request for loading custom layout

Open
#5,146 2 comments 5 reactions 0 assignees View on GitHub
feature-request
Dominant language
TypeScript
Stars
41.8k
Forks
41
PR merge metrics
No merged PRs in 30d

Description

## Check List

Please check followings before submitting a new feature request.

- [x] I have already read [Docs page](https://hexo.io/docs/)
- [x] I have already searched existing issues

## Feature Request

Happy new year, 2023 here (kinda late tho).
This has been talked about back then in 2017 in #2645, but that didn't solve the essential problem.

I think that Hexo should support to load layout automatically from a custom `/layout` diretory without having to modify `theme/*/layout/layout.ejs`.
This fundamentally means that themes can be decoupled with the actual content of the site.

Let me show you what I mean:
Suppose (I'm doing this irl really) that I'm building a website *not only* containing blog articles, but all other kinds of content, say ideas or song lists, and I want to organize them under different pages, and to have a nav bar linking to all these pages.
This could be done by applying categories on posts though, but in my case I want to have different sub-index page for each category. Auto generated category pages won't work, so essentially they have to use different layouts.

Since the nav bar is written in a theme, and the actual content types would be indefinite, I left a configuring option in `_config.yml` to let the theme EJS know what should be presented in the nav bar (see [the actual codes](https://github.com/WangNianyi2001/WangNianyi2001.github.io/blob/master/themes/nianyi/layout/partial/header.ejs)).

After this, I *should be* able to move all the sub-index layout files *out* from the theme directory, to `/layout` under the root directory. But when I actually do this, I noticed that all them pages with a custom layout had gone empty.

This means that custom layouts outside a theme can't be loaded, which further means that such content system can not be decoupled with its theme. Tracing back the old issue I realized this is certainly a design problem of Hexo.

## Others

When I first tried to implement such thing, I tried simply setting all individual entries' layouts (for example if I want a new blog article I'd create a new but with `layout: blog`), and then I can iterate through them in `blogs.ejs` via `<% for(const blog of site.blogs._data) %>`.
Then I (very sadly) found out that no pages other than those with layout set to `post`/`page` can be access in the `site` variable.

This should be fixed, probably by adding a `.layouts` member to `site`.

P.S. If you actually visit my [blog site](https://wangnianyi2001.github.io/) you'll see that I've got it equipped with multilingual support. I personally think that my solution is more elegant than using `i18n`. I hope to hear some advice of this from you!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.