GoHugo Hack To Use Hugo Template Variables In CSS

Credit: / stoned.io
stoned.io
Written by: Published by: Copyright holder: on
Using Hugo Template Variables in Hugo Stylesheets Today I will show you a quick hack to be able to make CSS styles in Hugo, use Hugo variables available to templates and such. Create /themes/yourtheme/layouts/partials/css/css.html with the following contents for example: <style> body { font-family: {{ .Site.Params.fontbody }}; } </style> Call this file right above the closing </head> tag, wherever your closing head tag may be. {{ partial "css/css.

stoned.io

Observing The Universe Within

GoHugo Hack To Use Hugo Template Variables In CSS

Using Hugo Template Variables in Hugo Stylesheets

Today I will show you a quick hack to be able to make CSS styles in Hugo, use Hugo variables available to templates and such.

Create

/themes/yourtheme/layouts/partials/css/css.html

with the following contents for example:

    <style>
        body {
             font-family: {{ .Site.Params.fontbody }};
        }
    </style>

Call this file right above the closing </head> tag, wherever your closing head tag may be.

{{ partial "css/css.html" . }}

Now whatever config variables you want to use, you can define them in

/config.toml

Voila! Now you can easily use Hugo template vars/data etc. inside hugo. It’s an ugly hack, but I like hacking. Once you define all your CSS, you all you have to do is modify your config.toml and you can make quick CSS changes.

This is NOT a substitution for CSS Pre Processors!

Now all you have to do is edit your styles in

/themes/yourtheme/layouts/partials/css/css.html

and you can use Hugo Template data and variables. The reason why we call this partial above the closing head tag is that this style will be the last one in the stylesheet cascade and would take precedence.

Enjoy folks! :)

Where is the fun in going along with the masses!?

P.S. Yes. I know it’s a dirty hack. I know CSS pre processors are heavily advised. I know. It’s okay to do this too. There is nothing wrong with it. Do what you want, and that shall be the whole of the law.

“There is no law sacred to me but that of my own nature. What is right, true and just is what is to my constitution. What is unjust, wrong, and evil is that which is against is.” - Hash Borgir

Here are some thoughts on the above quote, and why society and their judgments are the enemy of individuality.

My other Hugo Web-Development Articles:

zZ