Piping Replace and SliceStr

Hi all,

Is it possible to use replace and slicestr in a pipe statement? Per the pipe docs, the single piped value “becomes the last parameter of the next pipeline.”

For example in an archtype I may like to modify the title from the filename of “2012_01_03_post_name.md” to “Post Name”. Ideally I would be able to do this:

title: "{{ .Name | slicestr 11 | replace  "_" "  " | title }}"

but due to those functions accepting the input as the first argument rather than the last, I would have to do something like:

title: "{{ .Name | replaceRE "^([0-9_]{11})([a-zA-Z_])" "$2" | replaceRE  "_" " " | title }}"

Am I missing something? Are there pipable versions of replace and slicestr out there? Or am I limited to just using them in the first pipe command?

Thanks!

grellyd

3 Likes

I don’t think you’re missing something. I also ran into the same ‘problem’ that replace cannot be piped.

I’d also love to have a pipable version of those string functions since that would make working with strings in Hugo easier. :slight_smile:

1 Like

Thanks for confirming I’m not crazy.

Do you happen to know if there is a Hugo syntax roadmap? A quick glance around didn’t reveal any planning documents. Having more (or alternative) pipable functions in the template language would be nice.

As an aside, the replaceRE equivalent doesn’t have to be that complicated:

{{ "2012_01_03_post_name" | replaceRE "^.{11}" "" | humanize | title }}

(Above outputs "Post Name".)

slicestr does not check if the first 11 chars are numbers. So why would you need to do that in replaceRE. Also humanize would suit better here, I believe, than manually replacing underscores.

1 Like

… or another way:

title: "{{ slicestr .Name 11 | humanize | title }}"
2 Likes

Thanks kaushalmodi! You’re right it doesn’t need to be so complex. I was playing around with capturing syntax for another task and got pulled in! Thanks for the cleaner version. I hadn’t found humanize yet either