Render string as template

I’m relatively new to Hugo so apologies if I’m using the vocabulary wrong.

Is there a way to take a string and render it to the page as a template using a supplied context? Let’s say I have a YAML file that is structured like the following:

  entry_title: "{{ .name }} - {{ .city }}, {{ state }}"
  entry_type: "school"
  - name: "University of Arizona"
    city: "Tucson"
    state: "AZ"

I want to be able to do something like this:

{{ $struct := .struct }}
{{ range (index . $struct.entry_type) }}
    <p class="entry title">{{ $struct.entry_title | renderString . }}</p>
{{ end }}

I have a bunch of data that needs to be laid out similarly but have pieces of it composed differently that I’d like to dynamically define via YAML. Perhaps I’m approaching this the wrong way though.

I would do it differently.

But first, we have:

  • .RenderString (which renders the given string, including shortcodes, in the context and format of the Page (typically Markdown)
  • resources.ExecuteAsTemplate parses and executes the given string with the given context.

You could use the last one for you purpose, but I suspect you will be much more happy by

  • Keeping the data in front matter
  • Do all layout work in the template (e.g. single.html)

Note that it’s not uncommon to have some formatting in the front matter “data”, e.g. Markdown, so

  - name: "University of **Arizona**"

Would in template be used as:

{{ | .RenderString }}

I did try to use this but was unable to make it work. As far as I can tell it wants to create a resource file and needs a target path.

I’m also not sure if markdown will achieve my goals. I’m not looking for just dynamic formatting but also being able to dynamically control the structure of the data in my front matter.

But to be fair this is also probably the wrong place to be doing it.

My website is a resume (cloud resume challenge). I’m wanting to generate the static files with Hugo. My resume is compromised of sections like Education and Work Experience.

Each section is compromised of entries which are basically just a title and then a bulleted list of highlights for that entry.

University1 - City, State

  • Degree
  • Cool project1
  • Cool project2

University2 - City, State

  • Degree
  • Cool project1
  • Cool project2

Job1 - Job Title

  • Cool project1
  • Cool project2

Job2 - Job Title

  • Cool project1
  • Cool project2

I was splitting each section into its own YAML file: education.yaml, experience.yaml, etc… And then using partials to render them. I got a wild hair to try and use a single partial to render all the sections. And let the YAML files dictate how the info in the section is made up. I could do that by just having the YAML files structured like:

  - title: University1 - City, State
    - Cool Project1
    - Cool Project2

but I liked the idea of the files being more readable and using a field to help guide the partial on how that data is pieced together.

But it does seem like perhaps the philosophy here is to keep the logic out of the data files. Which makes sense.

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

I think a general good practice is to separate UI logic from data.

It creates a resource object, but the file will only be published once you access .RelPermalink etc. You can still treat it as an inline “thing” by calling .Content on the resource.

1 Like