←back to thread

392 points _kush | 3 comments | | HN request time: 0s | source
Show context
em-bee ◴[] No.44394567[source]
i have a static website with a menu. keeping the menu synchronized over the half dozen pages is a pain.

my only option to fix this are javascript, xslt or a server side html generator. (and before you ask, static site generators are no better, they just make the generation part manual instead of automatic.)

i don't actually care if the site is static. i only care that maintenance is simple.

build tools are not simple. they tend to suffer from bitrot because they are not bundled with the hosting of the site or the site content.

server side html generators (aka content management systems, etc.) are large and tie me to a particular platform.

frontend frameworks by default require a build step and of course need javascript in the browser. some frameworks can be included without build tools, and that's better, but also overkill for large sites. and of course then you are tied to the framework.

another option is writing custom javascript code to include an html snippet from another file.

or maybe i can try to rig include with xslt. will that shut up the people who want to view my site without javascript?

at some point there was discussion for html include, but it has been dropped. why?

replies(3): >>44394972 #>>44395615 #>>44395811 #
bambax ◴[] No.44395615[source]
> i have a static website with a menu. keeping the menu synchronized over the half dozen pages is a pain

You can totally do that with PHP? It can find all the pages, generate the menu, transform markdown to html for the current page, all on the fly in one go, and it feels instantaneous. If you experience some level of traffic you can put a CDN in front but usually it's not even necessary.

replies(1): >>44396116 #
em-bee ◴[] No.44396116[source]
that's the server side html generator i already mentioned. ok, this one is not large, but it still ties me to a limited set of server platforms that support running php. and if i have to write code i may as well write javascript and get a platform independent solution.

the point is, none of the solutions are completely satisfactory. every approach has its downsides. but most critically, all this complaining about people picking the wrong solution is just bickering that my chosen solution does not align with their preference.

my preferred solution btw is to take a build-less frontend framework, and build my site with that. i did that with aurelia, and recently built a proof of concept with react.

replies(1): >>44396370 #
ndriscoll ◴[] No.44396370[source]
You didn't actually indicate a downside to using xslt, and yes it would fit your use case of a static include for a shared menu, though the better way to do it is to move all of the shared pieces of your site into the template and then each page is just its content. Sort of like using a shared CSS file.

To just do the menu, if your site is xhtml, IIRC you could link to the template, use a <my-menu> in the page, and then the template just gives a rule to expand that to your menu.

replies(1): >>44396424 #
1. em-bee ◴[] No.44396424[source]
the downside to xslt is xslt itself, and lack of maintenance of xslt support in the browser. (browsers only supports xslt 1.0 and it looks like even that may be dropped in the future, making its use not futureproof without server side support)
replies(1): >>44398626 #
2. ndriscoll ◴[] No.44398626[source]
I'm not sure how xslt itself is a downside. It's a pretty natural template language to use if you already know HTML. You don't need more than 1.0 for your simple use-case. e.g. here's a complete example (tested in Firefox and Chrome):

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="html"/>
      <xsl:template match="nav-menu">
        <nav>
          <ul>
            <li><a href="page1.xhtml">Page 1</a></li>
            <li><a href="page2.xhtml">Page 2</a></li>
            <li><a href="contact.xhtml">Contact</a></li>
          </ul>
        </nav>
      </xsl:template>

      <xsl:template match="*">
        <xsl:copy><xsl:apply-templates/></xsl:copy>
      </xsl:template>
    </xsl:stylesheet>
Then here's a page to use it:

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="templates.xsl"?>
    <html>
      <head>
        <title>Welcome to my page</title>
      </head>
      <body>
        <nav-menu/>
        <h1>Welcome to the page!</h1>
        <p>This is the content</p>
      </body>
    </html>
Anywhere you want more templates, you add another

    <xsl:template match="my-element">
      <!-- HTML for my custom element -->
    </xsl:template>
And now you can use your custom <my-element/> directly in your HTML. You can of course also have attributes and children for your custom elements and do all sorts of programming things like variables and conditionals with XSLT if you dip your toes in a little further.

As far as longevity goes, it's lasted 25 years now, so that's something. As far as I know, there are a bunch of government services out there that still use it (which is great! Governments should be making things cheap and simple, not chasing trends), so removing support for it is somewhat infeasible. If it were removed, you could always make a Makefile that runs `xsltproc` on all of your xhtml files to spit out html files, so worst case you have a build step, but it's the world's easiest build step.

One nice benefit of doing things this way is that just like with CSS files, the more you pull into the template, the smaller all of your pages can be since you have a single static file for most of the page, and each page is only its unique data. If you lean into it a little more and are building an application, you can also have each page be its own "API endpoint" by returning XML in your native domain model. Databases can also output such XML directly, so you can make highly efficient single queries to build entire pages.

replies(1): >>44406762 #
3. em-bee ◴[] No.44406762[source]
you are right. xslt is not that bad. especially not for simple cases. your example would be just about enough for my website. thanks for that. i have used xslt before, but that was a long time ago.

however if you read through the comments there are a lot of people complaining, so they definitely see a downside to using xslt. as for longevity, well, a search led me to a chrome proposal to remove it that was made 10 years ago and ultimately rejected at the time. so maybe you are right about that too.