Tips for efficiently applying a layout to just one page?

Much of the time I only need to apply a specific layout to a single page, for example a REST API doc page or a Login page. Thus far I’ve been doing that in a way that feels kind of janky to me, and I was hoping to fiind a better way. Here’s my approach in a few steps:

  1. Add a pagetype param to that page:
---
"title": "Login"
"pagetype": "login"
---
  1. Create a special partial for that page (in layouts/partials/login.html):
<main>
  <h1>Login</h1>
  <!-- logic specific only to the Login page -->
</main>
  1. Create an if/else logic in _default/single.html that renders based on page type:
<body>
  {{if eq .Params.pagetype "login" }}
    {{partial "login.html" .}}
  {{end}}
  {{if eq .Params.pagetype "other" }}
    {{partial "other.html" .}}
  {{end}}
</body>

This approach has been working without hiccups thus far, but it seems a bit inelegant. Tips would be greatly appreciated!

Maybe I am missing something, but why don’t you assign a new type to the login page and then create its own specific layout?

Types can be assigned in two ways:

  1. Directory structure. For example everything in content/post is of type post. Content in content/login/ is of type login.
  2. Through the type parameter in front matter which overrides number 1. For example: type: login. In this case the content file can be anywhere in the content directory.

Then this page will use the template in layouts/login/single.html.

If you want to give the page a specific URL then you can specify the URL in the front matter too. url: "/login/" will put it at http://basurl/login/.

Here’s something I wrote a couple of weeks ago from my answer to another question on this forum about creating an archive page. Maybe that will help: http://parsiya.net/blog/2016-02-14-archive-page-in-hugo/.

1 Like

What works, and I was a little bit surprised by this and it broke when upgrading Hugo to Go 1.6:

Put your layout+content with no front matter into /content/login.html.

There are some limitations to this (no Page context), but for a simple login page that should work.

1 Like