How to use template options in page content in hugo

Can I use template options in the content of a message with Hugo? For instance. if I have the following options:

[params.company]
name = "My Company"

Can I then do something similar in the content of the message?

This site, {{ .Site.BaseURL }} is operated by {{ params.company.name }}

I tried, but literally typed above, instead of interpolating the variables.

+4
source share
1 answer

1. Front side of the track

, * , MD , front matter .md. Hugo , . .

, .MD, -, (, <div>) .

, .md :

---
title: "Post about the front matter"
<more key pairs>
nointro: false
---

nointro: true . , false, . <div>.

nointro :

, , -:

<div class="article-body {{ if .Params.nointro }} no_intro {{ end }}">
{{ .Content }}
</div><!-- .article-body -->

, {{ .Content }}, .

, hugo/themes/highlighter/layouts/partials/blog-single-content.html, . .

, Boolean, , :

MD :

---
title: "One of our clients"
<more key pairs>
companyname: "Code and Send Ltd"
---

Text content is put here.

, ( IF):

Hugo:

{{ if .Params.companyname }}{{ .Params.companyname }}{{ end }}

2. config. (Toml/yaml/json)

, , " " , , hugo/config.toml. companyname config, :

hugo/config.toml:

BaseURL = "_%%WWWPATH%%_"
languageCode = "en-uk"
title = "Code and Send"
pygmentsUseClasses = true
author = "Roy Reveltas"
theme = "Highlighter"
[params]
  companyname = ""

- {{ .Site.Params.headercommentblock }}.

, , , , , , -. , , . , .


3. Gulp/npm

, *, , .

, : 1) Prod 2) Dev. Prod URL- : CDN . Dev , .

, ( ): _%%WWWPATH%%_ _%%CDNPATH%%_. , , . hugo/config.toml :

hugo/config.toml:

BaseURL = "_%%WWWPATH%%_"

, , HTML Grunt:

grunt:

replace: {
  dev: {
    options: {
      patterns: [{
        match: /_%%CDNPATH%%_+/g,
        replacement: function () {
          return 'http://127.0.0.1:1313/'
        }
      }, {
        match: /_%%WWWPATH%%_+/g,
        replacement: function () {
          return 'http://127.0.0.1:1313/'
        }
      }...

Gulp / npm , Grunt. , Grunt .

, , Hugo params, Hugo , placeholder - , .

catch, Gulp/Grunt/npm scripts, Husky npm, - (, %%_). , Husky -, :

package.json :

"scripts": {
  "no-spilled-placeholders": "echo \"\n\\033[0;93m* Checking for spilled placeholders:\\033[0m\"; grep -q -r \"%%_\" dist/ && echo \"Placeholders found! Stopping.\n\" && exit 1 || echo \"All OK.\n\"",
  "precommit": "npm run no-spilled-placeholders"
},

, grep %%_ , . , JSON. ( ) . - , : %_, _%, %__, __% ..

+2

Source: https://habr.com/ru/post/1668728/


All Articles