valyala / quicktemplate

Fast, powerful, yet easy to use template engine for Go. Optimized for speed, zero memory allocations in hot paths. Up to 20x faster than html/template

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Include template

ArtavazdAvetisyan opened this issue · comments

Thank you very much for the great job, Aliaksandr. I just was not able to find how to include another template into the current one (I have some standard repetitive parts in my templates). E.g. in the html/template package it is {{ template "FILENAME" }} construction.

There are no templates in qtc in the sense as in the html/template. There are template functions instead, which may be called from go code in order to obtain template result. Each qtpl file may contain multiple template functions. Each template function may embed arbitrary number of another template functions via {%= templateFunc() %} call. Embedded template functions may be located in any qtpl file either in the current directory (aka go package) or in another directory. When embedding template functions from another directory (go package), it must be imported first via {% import %} section and then refered via package name like a public function from another package in go program.

Template functions are compiled by qtc (quicktemplate compiler) in an ordinary go functions with the same name, so they may be called directly from go code. These functions return string result. qtc creates an additional go function for each template function for performance optimization reasons. This function starts with Write prefix. See peformance optimization tips section for more details.

If you want embedding the whole static file into template, then use {% cat "/path/to/file" %}. Remember that the file contents is embedded into template during program compilation, not during program execution. If you want embedding static file contents during program execution, then use standard ioutil.ReadFile or similar function.

Closing this question as answered.