SystemTheory
SystemTheory

Reputation: 369

Use pug (jade) to selectively generate table of contents for articles in specified folders

I am using harp version 0.21.0 with jade support to develop a static site on Windows 7. The harp documentation links below show examples for using _layout.ejs for Nested Layouts and how to create a list of blog posts:

https://harpjs.com/docs/development/layout

https://harpjs.com/recipes/blog-posts-list

I would like to put links to articles in the side container only when the current page is in a group folder that contains more than one article as shown below.

_layout.jade
index.jade
+ group1
    article1
+ group2
  _data.json
  article1
  article2

_layout.jade

head
  title= title
  meta(name='description', content='#{ description }')
body
  #wrap
    #side
      p(align='center')
      img(src=sideimg, width='259', height='387', alt=imgtag)
      != partial("_toc.jade")
    #main
      != yield
    #footer

_data.json

 {
 "article1": {
   "pagetitle": "Article 1 Title",
   "sideimg"  : "/img/freebird.png",
   "imgtag"   : "bird flying under Sun"
 },
 "article2": {
   "pagetitle": "Article 2 Title",
   "sideimg"  : "/img/freebird.png",
   "imgtag"   : "bird flying under Sun"
 }
 }

_toc.jade pseudo code (What is proper jade syntax?)

if current page is in group1 folder then 
  do nothing
if current page is in group2 folder then
  for each article in group2 folder
    ul
      li
         insert #{ pagetitle } as link in side container

I have two questions.

  1. If this can be done using a jade partial then what is the syntax for jade code in _toc.jade?

  2. If this can be done using block and extend in jade then what is the code in that approach?

Upvotes: 1

Views: 244

Answers (1)

SystemTheory
SystemTheory

Reputation: 369

The harp documentation contains recipes with clues that I used to solve my own problem using the _toc.jade approach.

_toc.jade (This code works in the context of the original question)

if current.path[0] == 'group2'
  for group2, pagetitle in public.group2._data
    a(href="/group2/#{ pagetitle }") 
      p.i= group2.pagetitle

Upvotes: 1

Related Questions