sintaxi / harp

Static Web Server/Generator/Bundler
http://harpjs.com
5k stars 343 forks source link

Relative path for use with _contents #555

Closed PhilipHarney closed 5 years ago

PhilipHarney commented 8 years ago

I'm putting together a site in Harp, using Jade templates, which will be composed of multiple topics, written by different contributors. Ideally, to make life easy for them, each contributor will only have to worry about a single folder: the one for their topic.

My present folder structure is something like this:

  +public
    |_layout.jade
    +topic1
     |_data.json
     |pg1.md
     |pg2.md
     +img
      |pic1.png
      |pic2.png
    +topic2
     |_data.json
     |pg1.md
     |pg2.md
     +img
      |imageA.png
      |imageB.png

Each topic contains a sub-folder of images and I want to iterate over that folder, using the _contents element (outlined here). I have the desired Jade code functioning with an absolute path, such as:

for image in public.topic1.img._contents
    img(
           src="./img/#{ image }"
    )

I feel it should be possible to do something like this:

for image in current.img._contents
    img(
           src="./img/#{ image }"
    )

And that seems like a useful feature for loads of scenarios. Apologies if it exists and I just couldn't find it.

matteo-bombelli commented 8 years ago

hello, I'm working in ejs so maybe I'm offtopic (sorry), but if you want you can check https://github.com/matteo-bombelli/harp-ejs-functions

and looks in https://github.com/matteo-bombelli/harp-ejs-functions/blob/master/moduleSet/common/functions/lib/general.ejs

MB.general.ArrayToProps() function will do that:

<% 
var current2 = MB.ArrayToProps(current.path, public);
for(i in current2.img._contents){
var image = current2[i]; %>
<img src="img/<%- image %>" />
<% } %>

but it's better to read my starting readme before...

If you know how to do it, you can convert it to jade (I am a frontend / php developer so ejs seems easier to me :) )