I’m trying to include /layouts/_default/single.html into another file (basically “duplicating” it) and have it processed by Hugo. Yes, this is an edge case, but I am curious. How can I force Hugo to process the imported content? This is an example of the code I’m trying to use in fileWithDuplicatedContent.html…
The print function outputs strings. So with {{ print $single | safeHTML }} you are simply creating a long string.
Not sure whether you could achieve what you’re looking for.
But instead of chasing Easter eggs in November, why don’t you simply set the type of the content file in question to single so that said template is picked up by Hugo?
My reason for wanting to achieve this is a bit complicated, but I’ll try to explain as succinctly as possible.
A couple of pages on this site uses a few lines of PHP and for this to work with Hugo, I’ll have to duplicate the content of i.e. baseof.html and single.html into baseof.php and single.php. The content is exactly the same, only there is added a line of PHP to the first line on both files. I have tried to just rename the .html to .php, but that does not jive with Hugo for these files, so I have had to do it this way, with the help of Customized Output Formats
I would like to just call in the html file into the PHP file “as is” and have Hugo process it, so I do not have to manually copy and paste the content of the html file to the PHP file every time I make adjustments to the html.
First, instead of readFile with that horrible absolute path I would recommend using resources.Get and friends. It’s important to understand that it is possible to mount your templates to both layouts and, say, assets. So if you mount the theme’s layouts to, say, assets/layouts:
You mention mounting the layout folder/files as an asset, but I can’t seem to get this to work. From what I gather, this is done in the first line of your example code.
When I try this, it seems to be looking in assets/layouts/_default/ – and returns <nil> (hugo throws an error). What I want is to fetch the original template files from the layouts/_default/-folder directly.
Back to my project; In the first line I define a block with a tiny dose of php (yeah, I know) and then I attempt to “replicate” the code from the baseof.html:
Hugo throws this error: execute of template failed: template: _default/single.php:3:33: executing "_default/single.php" at <resources.ExecuteAsTemplate>: error calling ExecuteAsTemplate: type <nil> not supported in Resource transformations
The problem seems to be that $single is empty for some reason.
I’m trying to make a baseof.php and single.php that is identical to the html-templates, only with a tiny php thrown into the mix at the top.
I tried changing the suffix to .php (resources.ExecuteAsTemplate "single.php"), as this is what I want to end up with, but it made no difference.