From 5fda9610f907482497a7b32abb2ab2f85a4e6906 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Fri, 17 Sep 2021 14:00:36 -0500 Subject: templates: Add 'import' action (#4321) Related to (closed) Issue #2094 on template inheritance. This PR adds a new function called "import" which works like "include", except it only takes one argument and passes it to the referenced file to be used as "." in that file. * Update tplcontext.go Add {{ render "/path/to/file.ext" $data }} via funcRender * Update tplcontext.go * Refactor funcInclude, add funcImport to enable {{block}} and {{template}} * Fix funcImport return of nil showing up in html * Update godocs for and --- modules/caddyhttp/templates/templates.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'modules/caddyhttp/templates/templates.go') diff --git a/modules/caddyhttp/templates/templates.go b/modules/caddyhttp/templates/templates.go index 6fed8c8..8f2ea2c 100644 --- a/modules/caddyhttp/templates/templates.go +++ b/modules/caddyhttp/templates/templates.go @@ -91,9 +91,25 @@ func init() { // {{httpInclude "/foo/bar?q=val"}} // ``` // +// ##### `import` +// +// Imports the contents of another file and adds any template definitions to the template stack. If there are no defitions, the filepath will be the defition name. Any {{ define }} blocks will be accessible by {{ template }} or {{ block }}. Imports must happen before the template or block action is called +// +// **filename.html** +// ``` +// {{ define "main" }} +// content +// {{ end }} +// +// **index.html** +// ``` +// {{ import "/path/to/file.html" }} +// {{ template "main" }} +// ``` +// // ##### `include` // -// Includes the contents of another file. Optionally can pass key-value pairs as arguments to be accessed by the included file. +// Includes the contents of another file and renders in-place. Optionally can pass key-value pairs as arguments to be accessed by the included file. // // ``` // {{include "path/to/file.html"}} // no arguments -- cgit v1.2.3