Templates are documents built in Microsoft Word or Excel formats. Along with the document content, it is possible to specify mappings that will be replaced by the issue fields, custom fields (Mappings), and other data such as comments, work logs, attachments and much more(Iterations).
Regarding Xray data, as you know, Xray provides Issue types based on Jira native Issue types with the addition of customized data. This customized data is possible to get printed on documents as described on Exporting Xray Data page.
Read the following pages in order to allow you understanding how do document generator templates work: