Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What if I don't want $partial("templates/post-list.html")$ to be in index.html? #807

Open
HenryDashwood opened this issue Nov 9, 2020 · 3 comments

Comments

@HenryDashwood
Copy link

This is probably quite an obvious question so apologies in advance.

Suppose I'm working with the example site and I decide I would like the the list of posts to be generated in a template. If I move the the line $partial("templates/post-list.html")$ into, say, templates/default.html so the header there now looks like this:

    <header>
      <div class="logo">
        <a href="/">My Hakyll Blog</a>
      </div>
      <nav>
        <a href="/">Home</a>
        <a href="/about.html">About</a>
        <a href="/contact.html">Contact</a>
        <a href="/archive.html">Archive</a>
      </nav>
      $partial("templates/post-list.html")$
    </header>

I'll get the following error when I try to rebuild the site

Removing _site...
Removing _cache...
Removing _cache/tmp...
Initialising...
  Creating store...
  Creating provider...
  Running rules...
Checking for out-of-date items
Compiling
  updated templates/default.html
  updated templates/post-list.html
  [ERROR] Hakyll.Web.Template.applyTemplate: Failed to apply template templates/default.html to item about.rst:
    In expr '$partial("templates/post-list.html")$',
    In inclusion of '$partial("templates/post-list.html")$',
    Hakyll.Web.Template.applyTemplate: Failed to apply template templates/post-list.html to item about.rst:
    In expr '$for(posts)$',
    Tried field body,
    No 'posts' field in metadata of item about.rst,
    Tried field url,
    Tried field path,
    Tried field title,
    Missing field 'posts' in context

I'm a little stuck on what I do to resolve this. Any help much appreciated!

@pcoves
Copy link

pcoves commented Nov 9, 2020

Hi 👋

I'm new to hakyll and haskell but I'll try my best.

Hakyll tells you that, while compiling about.rst and applying the templates/default.html template, the posts field is missing.
As a matter of facts, if you simply moved $partial("templates/post-list.html")$ from index.html to templates/default.html, it's true.

You now have to make the posts field available any time you apply the templates/default.html template.
This (faulty) code does it : http://ix.io/2Dxg

But now, you're facing an issue : by default you apply templates/default.html template to every page, including the posts/* ones.
So, you're basically asking hakyll to use compilation of every posts in every posts, hence : [ERROR] Hakyll.Core.Runtime.chase: Dependency cycle detected: [...].

One solution would be to have two "default" templates, one in general and one for the posts.


In my humble opinion, you don't want your posts to be displayed in every single page of your site.
I mean, you can do it, but, should you really?

EDIT: your -> you're

@HenryDashwood
Copy link
Author

Ah I see what you mean. I mean it's more a general question about how generating a list of things in a template. But yeah it seems like something that might be hard to implement. I'll put it on the back burner until I'm more adept with Ha[ske/k]ll. Thanks for your help!

@pcoves
Copy link

pcoves commented Nov 9, 2020

Well, it depends on what you really want.

Here, you try to generate a bunch of content base on your posts and I guess this page will help you understand how to deal with versions.

But yeah, generating listFields is not alway easy.
I took different pieces of code from various blogs and from hakyll's code for this one if you ever need it :

customListField :: String -> String -> Context a
customListField key custom = listFieldWith key (field custom $ return . itemBody) customListField'
    where
        customListField' :: Item b -> Compiler [Item String]
        customListField' item = do
            metadata <- getMetadata $ itemIdentifier item
            let customs = fromMaybe [] $
                    (lookupStringList key metadata) `mplus`
                    (map trim . splitAll "," <$> lookupString key metadata)
            mapM makeItem customs

This will look at an Item's metadata and create a listField base on its content.

For example adding customListField foo bar to a context will allow you to write :

$for(foo)$
    $bar$
$endfor$

So if you have

foo:
  - a
  - b
  - c

or

foo: a, b, c

The $foo$ listField will contain [a, b, c] in $bar$s.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants