In Hugo, there may be situations where you need to retrieve the current file name without the “.md” extension. This can be useful when you want to display or manipulate the file name in your templates. In this blog post, we will explore a simple template snippet that allows you to achieve this.

To obtain the current file name without the “.md” extension, you can use the following template code:

{{ trim .File.LogicalName ".md" }}

Let’s break down this code:

  1. The dot (".") represents the current context in Hugo’s template engine.
  2. The File represents the file being rendered.
  3. The LogicalName provides the logical name of the file being rendered, including the file extension. For example, if the current file is “mypage.md”, the LogicalName will return “mypage.md”.
  4. The trim function trims the specified suffix (".md" in this case) from the given string.

By using this template code, you can retrieve the current file name without the “.md” extension, allowing you to manipulate or display it as needed in your Hugo templates.

Tags: Hugo templates, file name, trim, .md extension