Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

How about one line of POSIX sh with cpp? Though it uses the different approach of SSI instead of templating:

  $ cat <<EOF | sed -E 's/^[\t ]*<!--(#.*)-->$/\1/; t; s/^/\a/' | cpp -traditional -nostdinc -P -C | sed -E 's/^\a//'
  <!DOCTYPE html>
  <html>
    <head><title>My site</title></head>
    <body>
      <!--#include "navbar.inc.html"-->
      <p>Foo</p>
    </body>
  </html>
  EOF
  <!DOCTYPE html>
  <html>
    <head><title>My site</title></head>
    <body>
  <header>
    <a href="/">My cool site</a>
    <nav>
      <ul>
        <li><a href="/blog.html">Blog</a></li>
        <li><a href="/about.html">About</a></li>
      </ul>
    </nav>
  </header>
      <p>Foo</p>
    </body>
  </html>
With a little elbow grease, you even get incremental rebuild: https://git.sr.ht/~q3cpma/html-cpp


That is... a cool trick! I don't know C, so it would have never occurred to me. (Let's be honest; it would have not occurred to me even if I knew C :))

What I like about the Bash + Heredocs + substitutions approach is that it looks and feels declarative to me. Template "expansion" is just in-place substitutions (parameter expansion, shell substitution, process substitution); no string munging needed to "place" or "inject" content in the HTML tree.

Anyway, I spent way too much time figuring that out, and it works well for me, so I'm going to just roll with the sunk costs :D

Thanks for sharing your approach!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: