Live-Poll icon indicating copy to clipboard operation
Live-Poll copied to clipboard

[question] [discussion] why use pug

Open GiggioG opened this issue 5 years ago • 4 comments

I was going through the code and I was wondering why do we use pug? Yeah, there are the includes but that can easily be done with simple javascript. We don't use it's more complex features.

GiggioG avatar Jan 04 '21 09:01 GiggioG

i was wondering that as well

ShawKai91 avatar Jan 04 '21 18:01 ShawKai91

I'm not sure exactly, but we are also using the templating features of pug. I think this is a pretty good reason to use it, since every poll response and results page needs to have different options.

Personally, I prefer it to client side javascript tomfoolery but that's just my opinion

elunico avatar Jan 04 '21 20:01 elunico

right that footer thing would be harder to implant with out it.

but why pug in particular?

ShawKai91 avatar Jan 05 '21 01:01 ShawKai91

@ShawKai91, watch this:

let resp = await fetch("http://url.to/the/footer/will?be=here");
resp = await resp.text();
document.querySelector(div.replace#footer).outerHTML = resp;

you can maybe do this for each one, but there is also this more elegant solution I use for my github pages:

let data = document.querySelector("script[src=\"http://url.to/exactly/this=javascript?file&goes=here"]")
    .attributes["data-includes"].value
    .split("|");
let includes = [];
data = data.map(el=>el.split(","));
includes.forEach(async i=>{
    let raw = await fetch(i[1]);
    let text = await raw.text();
    let remove = document.querySelectorAll(`i.incl-${i[0]}`)
    remove.forEach(r=>r.outerHTML = text);
}) 

and to use it we simply have to put this in the

of each page:
<script src="http://url.to/exactly/this=javascript?file&goes=here" data-includes="header,http://url.to/the/header/will?be=here|footer,http://url.to/the/footer/will?be=here"></script>

and then put these in where we want the replacement to happen

<i class="incl-header"></i>
<i class="incl-footer"></i>

Implement this if you want, I won't because someone probs has a reason to use pug.

EDIT: Forgot to say this in the original comment, but 2 things to add:

  1. If you are worried about how this approach requires the client to do n+1 additional requests for every include, we can do this on the server side, by replacing it before even sending a response to the client.
  2. I will go through the commit history and see who added pug and personaly ask him why.

GiggioG avatar Jan 05 '21 11:01 GiggioG