.Page.File.Dir : Problem with output

Helle everyone,

I’m using {{ .Page.File.Dir }} in my template
It renders as ./dirA%5dirB%5 instead of ./dirA/dirB/.

How to change %5 to /?
What am I doing wrong?

Thank you very much and many greetings
ruphus 42

I couldn’t reproduce it, if you’re using it as an element attribute value, safeHTMLAttr may help, for example.

<p {{ printf `data-dir="%s"` .Page.File.Dir | safeHTMLAttr }}></p>

Thank you @razon , it doesn’t help.

Cloud you please provide a reproducible code.

Really?

Are you sure it’s not:

It renders as ./dirA%5CdirB%5C

Hello @jmooring , thanks for your eye.
You are right, it renders as ./dirA%5CdirB%5C.

So the question is: How to change %5c to / ?
What am I doing wrong?

You’re on a Windows system, so the OS path separators are backslashes (\). The hex representation is 5C.

If you are trying to use .File.Dir to build a URL or something, pass it through path.Clean.

Thank you @jmooring, it doesn’t help.
Is there another way to get the URL?

Please post a link to the public repository for your project.

See Requesting Help.

Let us see your code

Include a link to the source code repository of your project, because we really need the context of seeing your templates and partials to be able to help you. It is trivial to do a quick git clone on your repo, then run hugo server in your project, to help you out. On the other hand, recreating your code from screenshots, or sort of guessing at it, is not.

If you can’t share your repository for whatever reason, consider creating a dummy repo that you can share, which reproduces the problem you’re experiencing.

Thank you again for your good will @jmooring.
I had a typo.
Your answer solved my problem by using

{{ path.Clean .Page.File.Dir }}

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.