sc0ttj / mdsh

A simple static site generator, using Markdown and Bash
https://sc0ttj.github.io/mdsh/
10 stars 0 forks source link

Data folder #85

Closed sc0ttj closed 4 years ago

sc0ttj commented 4 years ago

An early attempt at solving https://github.com/sc0ttj/mdsh/issues/65

What's the improvement?

New feature: the assets/data folder

Site-wide data: Put .yml, .csv, or .sh files in the assets/data/ folder, and the data they contain will be available to all templates at build time.

Page specific data: Put .yml, .csv, or .sh files in the assets/data/my-cool-page/ folder, and the data they contain will be available to the page my-cool-page only at build time.

Details

Other new features

The foreach iterator

Easier data debugging

page_layout variable

Easily render pages with different layouts

Define it in the page front matter, or pass it in via CLI when running create_page.sh (defaults to main if empty, and must be the name of a template file in .app/templates/*/)..

Fixes

Implementation of data folder

Added example data files:

site.yml, products.yml, people.csv, arrays-example.sh

Pull in data at build time:

Added a .yml parser & csv parser func, updated cli-interface to parse data files on each build:

Updates in .app.cli-interface.bash to scan the assets/data dir for files, and parse them into vars and arrays that our build scripts and templates can use...

YAML parsers used:

CSV parser:

Much easier to use arrays in templates:

Added a foreach iterator function, to be used in the templates, small fix to mo script to make it work OK.

An example!!

  1. assets/data/products.yml contains:
  product1:
    name: Foo
    price: 10
  product2:
    name: Bar
    price: 20
  1. so you access it in your templates like so:
  {{#foreach product in products}}
    {{product.name}} costs {{product.price}}
  {{/foreach}}

TODO: