Including files/scripts

Is there an equivalent on Caddy2 for something like this:

<!--#include virtual="/cgi-bin/fortune.sh"-->

That is, the ability to include scripts within a web page served by Caddy2.

Yes, and it’s even better than server-side includes (SSI). See the templates module:

See the include and httpInclude template actions.

This is used heavily on the Caddy website:

1 Like

This doesn’t look like it, quite. I might be wrong, but can I execute scripts with the templates module? It is not simply inclusion—I used the wrong work on my original entry—but execution and rendering near realtime. Do you have an example of a script execution using the templates module?

Not by running an arbitrary shell script, no. The closest thing currently is httpInclude which makes another HTTP request for more content.

Unfortunately we haven’t bothered making the templates module pluggable yet (to allow plugins to add their own functions) but that would be a possible refactor that would allow a plugin to provide this kind of functionality.

I’m not sure arbitrary shell execution is something we necessarily want in the standard distribution of Caddy, it’s probably better left to a plugin to provide (once templates is made extensible).

FYI, I opened up a feature request issue for this:

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.

FYI we just merged a change which makes templates pluggable, so you can write your own plugin for Caddy to add functions to do whatever you need.