I want to concatenate $filenameBeginning and $filenameEnding (with no spaces) to build the filename and then call that filename with an inline style. (So my 3 categories each have different background images in the header, based upon the page parameter.)
Here is what I’d like: 3 images in my assets/img folder named: cat1-background-image.jpg categ2-background-image.jpg category3-background-image.jpg
The goal is to make one variable that can be called with a {{ $imgURL := .Resources.GetMatch $combinedFilename }} <img src="{{$imgURL.RelPermalink}}"}}
(I realize this is simply calling it as an image and not a background-image using an inline style).
So I can make a variable like this:
{{ $filenameEnding := "-background-image.jpg" }}
The front matter of each page will contain either:
category: “cat1”
category: “categ2”
or
category: "category3
So I should be able to make this variable:
{{ $filenameBeginning := .Page.Params.Category }}
But I can’t figure out how to put it all together (get $combinedFilename from concatenating $filenameBeginning and $filenameEnding)
When I try to start Hugo server, I get this error: execute of template failed: template: machine/single.html:12:93: executing "main" at <$assetImage.RelPermalink>: nil pointer evaluating resource.Resource.RelPermalink
(You probably don’t need the printf or parenthesis but I know for sure that works, so I’ll stick with it until I remember to test warnf’s own formatting spec abilities)
Thank you!!
After working through a few errors I was able to get the site to serve and build.
However, my entire breadcrumb (top of page) and background image are not formatted now (or background image loading). But I am able to build the site and upload via FTP so I can view source on the generated files.
REALLY appreciate the kind assistance!
It’s been a long (10 hr) day here, so I will get back at it tomorrow.
then you’ll at least have your breadcrumb / top bar back in the case Hugo can’t find the image. It obviously won’t have an image in that case, but it should unbreak the layout.
EDIT: Oh, and I guess you figured out the with should be if because it changes the context (.) and .TranslationKey would not work as I wrote above.
Thanks again. You correctly anticipated the lack of the breadcrumbs div not being written in the ELSE. Also, I had NOT figured out that the with should be an if - so appreciate that catch also.
So with those changes everything looked right, except I was still not getting the background image inline style written (The ELSE was being written instead). So I went back and did a printf $variableName after each variable assignment. All was good up until I asked it to:
printf $assetImage
That returned this error message:
render of "page" failed: "D:\Hugo\HugoSites\01\themes\half-fast\layouts\machine\single.html:14:10": execute of template failed at <$assetImage>: wrong type for value; expected string; got resource.Resource render of "page" failed: wrong type for value; expected string; got resource.Resource failed to render pages: wrong type for value; expected string; got resource.Resource
That’s a new one for me so Googling that now, but happy to hear from anyone who knows the fix for this. If it makes a difference, the pages using this are ALL kind: "machine" (defined in Front Matter). THX!
Thanks for hanging in there with me on this!
I’m afraid I don’t know what you are referring to as the “console”. However, I removed the warnf and just made it printf on the visible page (since it is rendering with the else code).
So here is what my code looks like now. (Noob attempting to recast the string to a resource for the first time):
It should perhaps be noted that the index page in question is found at:
/content/machine/stairboss_pro/index.md (no directory named “page” if that is what it is saying).
Also that initial ‘page’ in page/machine/stairboss_pro/index just means that it’s a page.
And the console is where you were seeing:
execute of template failed: template: machine/single.html:12:93: executing "main" at <$assetImage.RelPermalink>: nil pointer evaluating resource.Resource.RelPermalink
when you did hugo server way back at the top. Perhaps I should have said on command line or in the terminal.
And what this
%!s(<nil>)
tells us is that $assetImage has no value (is nil) but we already knew that because we are in the else which can only happen when there is no value for assetImage
OK thank you. But I only see the console if there is an error and the page cannot be served or rendered.
It IS serving and rendering… only with (as you noted) the {{else}} html.
So we are making progress, but the directory in /assets/ is not being put on the front of the path.
The image resides at: themes/half-fast/assets/img/Stairs-breadcrumbs-bg.jpg
But, as an aside, I want to clarify resources.FromString.
That creates a ‘resource object’ (that does the same thing as .Resources.GetMatch) only it creates it from a the string you provide instead of from a file. So it’s actually not what you want. It also writes the resources to the build cache at the path your specify.
So what the docs say is:
resources.FromString which takes two arguments, the given string and the resource target path.
which mean that this:
resources.FromString $combinedFilename $directory
tells Hugo create a resource from the literal string (not the file at the location, but the actual characters in the filename) with the filename $directory (in /public of course, but only in cache unless you .Permalink it or similar). You were then assigning the resource (not a string with a path) to $fullPathAndFilenameRecast.
I don’t think that is quite what you were looking for.
That’s great (and the easier way that I suspected existed)!
We’re getting the full path now, so the problem seems to be in the .Resources.GetMatch
I may have a changed variable name in here (from before) but the page serves and renders (but still no background-image in the div) - getting the {{ else }} html:
So THAT SHOULD work. But something is not working in the GetMatch. (I’ve also tried .Resources.Get and using.Page.Resources.GetMatch and .Page.Resources.Get). No joy on any of them (but page still renders and give me the proper bgimage in the debugging printf.
An image with that exact name DOES exist in /themes/themeName/assets/img
That was it!
First I just tried changing the case of the “R” to .resources.Get (wasn’t reading your post carefully). But that gave me a different error message:
<.resources.Get>: resources is an unexported field of struct type page.Page