HUGO

How to force processing of imported/included file?

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…

{{ $single := readFile "/themes/cloudless-hugo-theme/layouts/_default/single.html" }}
{{ print $single | safeHTML }}

…but this only prints the raw file without translating the Hugo code from single.html, which ends up looking like this:

{{ define "classMain" }} p-single{{ end -}}
{{ define "insideMainArticleBottom" -}}
    {{ with .PublishDate }}
    <div class=m-article__meta>
        <time datetime="{{ . }}">Publisert {{ .Format "02.01.2006" }}</time>
    </div>
    {{ end }}
{{ end -}}

I want Hugo to process the content of fileWithDuplicatedContent.html – is it possible?

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.

I will not even try to understand the “why”, but:

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:

{{ $single := resources.Get "layouts/_default/single.html" }}
{{ $result := $single | resources.ExecuteAsTemplate "single.html" . }}
{{ $result.Content }}

I just typed the above directly here, so totally untested, but the concept should work.

2 Likes

Thanks, this is what I’m looking for!

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.

Ok, I have been wrestling with this for a few hours without getting it to work :laughing:

The default mounts are in place when I run hugo config mounts – for both themes that I use. Output:

[…]
"mounts": [
      {
         "source": "layouts",
         "target": "layouts"
      }
[…]

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:

{{ define "preHTML" }}{{ print "<?php session_start(); ?>" | safeHTML }}{{ end }}
{{ $single := resources.Get "layouts/_default/single.html" }}
{{ $result := $single | resources.ExecuteAsTemplate "single.html" . }}
{{ $result.Content }}

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.

[[module.mounts]]
  source = "themes/theme/layouts"
  target = "assets/layouts"
<!-- themes/theme/layouts/_default/single.html -->
Title: {{ .Title }}
{{ $single := resources.Get "layouts/_default/single.html" }}
{{ $result := $single | resources.ExecuteAsTemplate "single.php" . }}

{{ $result.Content }}
<br>
{{ $result.Permalink }}
<!-- result -->
Title: My New Hugo Site
http://localhost:1313/single.php
<!-- public/single.php -->
Title: My New Hugo Site
1 Like