Main Content

tmplview

Preview content and styles of source template

Since R2024b

Description

Use tmplview to preview the content and styles of a source template. The function generates a set of documents and places them in a directory based on the name and type of the source template. The files have the same file type as the output of an mlreportgen.dom.Document object based on the source template. For an overview of the output document structure and how the templates represent the template body, template document part holes, and stylesheets, see Template Content and Styles Preview Structure.

tmplview(templateObj) generates preview documents from the source template in templateObj, and opens the main template document in the appropriate viewer. You must close template objects by using the close method before generating the preview documents.

Note

Opening a Microsoft® Word document on a Linux® or Macintosh platform calls the soffice command. To use the soffice command on a Linux machine, you must install Apache® OpenOffice® or LibreOffice®. On Macintosh, you must install Apache OpenOffice in the /Applications folder.

example

tmplview(templatePath) generates documents from the template at the path specified by templatePath.

tmplview(templateName,format) generates documents from the template specified by templateName and in the format, specified by format.

tmplview(___, OpenDocument=openDoc) specifies which document to open after template preview generation, specified by openDoc.

Examples

collapse all

This example shows how to create and preview the content and styles of a template. After previewing the template, modify the template and regenerate your preview to examine your modifications.

Create Template

Import the DOM namespace so that you do not have to use fully qualified names for the DOM API classes.

import mlreportgen.dom.*

Create and open the HTML template.

typeTemplate ="html";
nameTemplate = "bookreviewTemplate";
tmpl = Template(nameTemplate, typeTemplate);
open(tmpl);

Create and add a custom paragraph style.

pStyle = TemplateParagraphStyle("companyParaStyle");
pStyle.Formats = [FontFamily("calibri"), ...
    FontSize("12pt"), ...
    WhiteSpace("preserve")];
tmpl.Stylesheet.addStyle(pStyle);

Create and add a custom table style.

tblStyle = TemplateTableStyle("companyTableStyle");
tblStyle.TableEntriesFormats = Border("solid");
tblStyle.HeaderFormats = [Bold(), ...
    BackgroundColor("cadetblue")];
tblStyle.OddRowFormats = BackgroundColor("lightblue");
tmpl.Stylesheet.addStyle(tblStyle);

Define the main template content.

h = Heading1("Books I've Read");
append(tmpl,h);

Create and append a hole for a table of books.

hole = TemplateHole("booksTable");
append(tmpl,hole);

Create and append a section for book reviews.

h = Heading1("Reviews");
append(tmpl,h);
hole = TemplateHole("bookReviews");
append(tmpl,hole);

Define a template document part for the individual book ratings.

tdp = TemplateDocumentPart("BookReporter");

Create and append the Title hole.

title = Heading2();
append(title, TemplateHole("Title", "Title of the book"));
append(tdp, title);

Create and append the Author hole.

author = Heading3();
append(author, TemplateHole("Author", "Author of the book"));
append(tdp, author);

Create and append the Rating hole. Use your custom paragraph style, "companyParaStyle".

rating = Paragraph("I rate this book: ");
rating.StyleName = "companyParaStyle";
append(rating, TemplateHole("Rating", "Rating of the book"));
append(rating, " out of 5 stars.");
append(tdp, rating);

Append the template document part to the template. Close the template.

tmpl.TemplateDocumentParts(end+1) = tdp;
close(tmpl);

Preview Template with tmplview

Generate the template preview documents using the Template object from the Create Template section.

tmplview(tmpl);

Alternatively you could also also use:

  • the name and format type of the template, tmplview(nameTemplate, typeTemplate)

  • the path of the template, tmplview(tmpl.OutputPath)

In the viewer, you see the content in the main template document.

Snippet of viewer window displaying the content of the main template document

To view the styles in the template, specify the template and use the openDoc argument to specify the stylesheet document. You can use any of the template specification syntaxes with the openDoc argument.

tmplview(tmpl.OutputPath,openDoc="stylesheet"); 

Snippet of viewer window displaying the template styles in the stylesheet document

To view your custom template document part, BookReporter, specify the template and use the openDoc argument to specify the template document part. If you need to find the name of a template document part, you can look in the file folder generated by tmplview.

Snippet of MATLAB file explorer displaying the file structure of the template preview documents. A red box emphasizes the BookReporter.htmx file.

tmplview(nameTemplate,typeTemplate,openDoc="BookReporter");

Snippet of viewer window displaying the content of the custom template document part BookReporter.

Modify Template and preview changes

Modify the copy of the code to get the changes you want in the template. In this case, update the ratings Paragraph text to " out of 10 stars".

typeTemplate ="html";
nameTemplate = "bookreviewTemplate";
tmpl = Template(nameTemplate, typeTemplate);
open(tmpl);

pStyle = TemplateParagraphStyle("companyParaStyle");
pStyle.Formats = [FontFamily("calibri"), ...
    FontSize("12pt"), ...
    WhiteSpace("preserve")];
tmpl.Stylesheet.addStyle(pStyle);

tblStyle = TemplateTableStyle("companyTableStyle");
tblStyle.TableEntriesFormats = Border("solid");
tblStyle.HeaderFormats = [Bold(), ...
    BackgroundColor("cadetblue")];
tblStyle.OddRowFormats = BackgroundColor("lightblue");
tmpl.Stylesheet.addStyle(tblStyle);

h = Heading1("Books I've Read");
append(tmpl,h);
hole = TemplateHole("booksTable");
append(tmpl,hole);

h = Heading1("Reviews");
append(tmpl,h);
hole = TemplateHole("bookReviews");
append(tmpl,hole);

tdp = TemplateDocumentPart("BookReporter");
title = Heading2();
append(title, TemplateHole("Title", "Title of the book"));
append(tdp, title);

author = Heading3();
append(author, TemplateHole("Author", "Author of the book"));
append(tdp, author);

rating = Paragraph("I rate this book: ");
rating.StyleName = "companyParaStyle";
append(rating, TemplateHole("Rating", "Rating of the book"));

Change the rating text to 10 stars.

append(rating, " out of 10 stars.");
append(tdp, rating);

tmpl.TemplateDocumentParts(end+1) = tdp;
close(tmpl);

View the updated template.

tmplview(nameTemplate,typeTemplate,openDoc="BookReporter");

Snippet of viewer window displaying the content of the updated custom template document part BookReporter.

Input Arguments

collapse all

Template DOM object to use to generate template preview files, specified as an mlreportgen.dom.Template object.

Template file path and name, including the file extension, to use to generate the preview files, specified as a character vector or string scalar.

Data Types: char | string

Template name to use to generate the preview files, specified as a character vector or string scalar. Do not include the file extension.

Data Types: char | string

Template format of template specified by templateName, specified as one of these values:

  • "docx"

  • "html"

  • "html-file"

  • "html-multipage"

  • "pdf"

Data Types: char | string

Template component or template document part to display, specified as:

  • "templateBody"

  • "stylesheet"

  • A string scalar or character vector that contains the name of a template document part, such as "myDocPart"

Data Types: char | string

Limitations

  • tmplview does not view Word templates in MATLAB Online.

  • tmplview is not supported in a MATLAB® web app.

Version History

Introduced in R2024b