HUGO

Problem with CSS files merge

Hi, I have a problem merging. CSS files by page when I use the following code

{{ $style := resources.Get "css/style.css" }}
{{ $fontawesome := resources.Get "css/font-awesome.css" }}
{{ if .IsPage }}
  {{ $page := resources.Get "css/page.css" }}
{{ end }}
{{ $css := slice $style $fontawesome $page | resources.Concat "styles.css" | minify }}
<link href="{{ $css.RelPermalink }}" rel="stylesheet">

Problem appears

Error: add site dependencies: load resources: loading templates: “/opt/buildhome/repo/layouts/partials/head.html:9:1”: parse failed: template: _default/home.html:9: undefined variable “$page”

You could use something like

{{ $css := slice $style $fontawesome if .IsPage $page end | resources.Concat "styles.css" | minify }}

And I have another question. If there are many Hugo codes, will the site speed be reduced?

1 Like

I’ve reformatted your post for clarity. Please learn to use code fences (```) when posting code.

You have declared $page within an if statement, so its scope is restricted to within the if statement. The same would be true if you declared the variable within a with or range construct.

You need to do something like this:

{{ $a := "" }}
{{ if .IsPage }}
  {{ $a = "foo" }}
{{ end }}

I do not understand your question.

Thank you

If I use fewer codes like if and for and $a the page will be faster

I’m kind of a beginner in a forum and sorry if you don’t understand what I mean because I’m using google translate

When you build your site with the hugo command, the build time is affected by your template code. If you used 1000 if statements in a template, and you built a site with 1000 pages, that might add a few seconds to your build time.

But the speed at which your published pages are served to a browser would not be affected; Hugo is a static site generator.

1 Like

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