in reply to Re^2: Last Modified for Template file
in thread Last Modified for Template file

You could write the head from the body using the INCLUDE directive, but I guess you will have to show more of your generation process to make it clear.

Replies are listed 'Best First'.
Re^4: Last Modified for Template file
by Bod (Parson) on Aug 30, 2023 at 11:22 UTC
    You could write the head from the body using the INCLUDE directive

    I could, and I do on some simple sites...

    However, my usual approach is to have a module that deals with all the Template processing. Besides new it usually has just three methods, head, process and foot.

    head deals with preview images, page titles, external CSS/JS files, etc providing a standard setup that can be changed on a per-script basis.

    foot does much the same as head but there are less variations - mostly copyright date.

    process obviously processes the body script but also deals with user login state, log files, etc depending on the site's needs.

    I was looking for a solution that I could just put in head that would give me the latest modification time of the body template as I don't want to start rewriting several websites just to get this information. As I don't think it's going to be possible, I'm looking at having another script that reads stat for the relevant body template file. There is still a problem with older scripts that generate the HTML within the script (the way I did it before the Wisdom of The Monastery reached me!) because the script modification time is the same for every page it generates.

    But there is also the question of what is actually a modified date...if a script provides the data to a template, changing the script or changing the template could cause an update to the resultant webpage.

      The real question is "did any of the content change", and that would be very very hard to answer from inside the template. You would actually need to record the newest date of any of the three: head, body, or foot, and you'd need to update the logic if you ever call out to additional templates.

      I think your best bet would be to handle it in the server. Call out to Template, build the complete bytes of the body, then hash them (md5 or sha1) and save that somewhere (database, textfile) and if it doesn't match the previous value for this URL, update the timestamp.Then add the header the way it was meant to be added - as a header.

      Edit: just saw your comment about "to process with a static site generator". In that case, in the step where you generate the pages from templates, you could just render to memory first, hash it, and only overwrite the file on disk if the content has changed. Your static site generator should then be able to get the last-modified from the mtime of the pages.