Reputation: 4487
I always create my posts with filenames that begin with "0001-", "0002-", etc. I'd like the default title to strip that off.
In the file themes/Hugo-Octopress/./archetypes/post.md
I changed:
---
title: "{{ replace .TranslationBaseName "-" " " | title }}"
to
---
title: "{{ replace .TranslationBaseName "-" " " | substr 6 | title }}"
I thought that this would pipe the title through "substr 6" which would remove the first 5 chars from the file.
However the error message that I got was this:
Error: Failed to process archetype file "[redacted]/themes/Hugo-Octopress/archetypes/post.md": template: post:2:50: executing "post" at <substr 6>: error calling substr: start argument must be integer
How do I strip off the first 5 chars of a string in a template pipeline?
Upvotes: 1
Views: 2710
Reputation: 466
The solution is to wrap your replace
in parens and give that as a parameter to substr
, like this:
title: "{{ substr (replace .TranslationBaseName "-" " ") 11 | title }}"
I tested it and this work for me.
(source: https://discourse.gohugo.io/t/how-to-trim-and-truncate-a-url/2639/3)
Upvotes: 2
Reputation: 1
As far as I have experienced, Hugo doesn't process any functions in *.md files normally, so you'd have to use "Shortcodes" - see those two for further information:
I'm quite sure though that I read somewhere that these don't work in front matter, i.e. the "title" parameter.
For my posts, which are also numbered, I have resorted to the "slug" parameter:
001-post-title.md
+++
title = "post title"
weight = "-999"
slug = "post title"
image = "fancy.jpg"
+++
Using this parameter, the permalink looks like this in my case:
http://localhost:1313/post/post-title/
I know that this means manual typing and also may be a source of errors, but it's the easiest way around this issue I could find when I wanted a solution. Maybe there's a better answer if you ask the same thing in the Hugo forums.
Upvotes: 0