absURL doesn't work with the variable

What version of Hugo are you using ( hugo version )?

v0.60.1

Does this issue reproduce with the latest release?

yes

Problem

the absURL function works well like this:

<a href="{{"test" | absURL }}">test</a>

but doesn’t work when used with variable like this:

<a href="{{.testLink | absURL }}">test</a>

How is .testlink declared? Typically template variables begin with the dollar sign $

.testlink would only make sense within the context of a given Data file.

You need to post more info. Also see the Requesting Help guidelines.

I declared .testLink in config.toml file. If I don’t use absURL, just use the .testLink only. Such as:

// define in config.toml
testLink = "http://test.com/testLink"
// used in template
<a href="{{.testLink}}">test</a>

it works well. But when I use with absURL, it doesn’t, such as:

// define in config.toml
baseurl = "http://test.com"
testLink = "testLink"
// used in template
<a href="{{.testLink | absURL}}">test</a>

And it also works when I don’t use the variable:

// define in config.toml
baseurl = "http://test.com"

// used in template
<a href="{{"testLink" | absURL}}">test</a>

It works well and link will be rendered as http://test.com/testLink

These are what I can describe for my question. It seems that the variable here don’t need to use with sign $. Thanks.

I am sorry but what you posted above does not make sense to me and I cannot see how you are able to call the .testlink parameter in a template.

As far as I know, custom user parameters in the config are typically entered within the .Site.Params container, otherwise hugolib will not be able to evaluate them.

Furthermore one needs to use the proper context when trying to access .Site.Params from the templates

You need to share your project for people in the forum to see what you may be doing wrong.

My time is limited and I cannot look into this further.

1 Like

I’m really sorry for my confusing sample. Here is my real project link which I really use .testLink

Due to I want to use github pages to show the site, my baseURL set is a little special like this.

What’s the problem is that it don’t render the link expected with the baseURL set to https://nianiajr.github.io/nebula-website/ in config.toml.

Sorry again for your time wasted. It’s ok you ignore the sample here. Thanks really.

Right. There is a language barrier here. By writing that absURL does not work you meant that the menu links throw a 404. Also the misunderstanding was due to the fact that the whole context was not shown.

In your config the baseURL points to a subdirectory.
However your menu links begin with a forward slash / and as a result these links point to the host root.
To fix this issue you need to remove the forward slash / from the beginning of the links.
Also if you notice any missing assets on your project you should do the same.

1 Like

Thanks for your clear analysis. I get it finally! :+1: