Main Content

clone

Class: mlreportgen.dom.TemplateText
Namespace: mlreportgen.dom

Copy template text object

Since R2024b

Description

clonedTText = clone(sourceTText) copies the specified template text.

example

Note

To learn how to examine the source or updated template file directly, see Open Template Files.

Examples

expand all

To add additional content or holes, or to change the styling in an existing template, create a new TemplateDocumentPart object to replace the existing TemplateDocumentPart object. You can replace the relevant document part while the rest of the source template content remains unchanged.

Use tmplview to display the template document part,"partToModify", in the source template. This document part currently contains the static text "Text in template document part" followed by a template hole, "templateHole". In this example, you add additional text before and after the template hole by finding the part to modify, copying the original text and template hole, and appending your additional text.

tmplview("existingTemplate","html",OpenDocument="partToModify");

tmplview window showing the contents of the partToModify template document part from the template existingTemplate.htmtx.

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

import mlreportgen.dom.*

Create a template by copying the existing template.

t = Template("updatedTemplate","html","existingTemplate");
open(t);

Find the part to modify in the document parts.

toModifyIdx = strcmp({t.TemplateDocumentParts.Name},"partToModify");
toModifyPart = t.TemplateDocumentParts(toModifyIdx);

Define a new template document part based on the document part.

newTemplateDocPart = TemplateDocumentPart("partToModify");

Copy the children of the original template document part into the new document part before and after the template hole. When your for loop reaches the template hole, add the first line of new text, copy the template hole, and then add the second line of new text.

for child = toModifyPart.Children
    % If the child is a template hole
    if isa(child,"mlreportgen.dom.TemplateHole")
        switch child.HoleId       
            case "templateHole"
                append(newTemplateDocPart,...
                    Text("Text before template hole."));
                append(newTemplateDocPart,TemplateHole(child.HoleId));
                append(newTemplateDocPart,...
                    Text("Content after template hole"));
            otherwise
                append(newTemplateDocPart,TemplateHole(child.HoleId))
        end
        % If the child is not the template hole, copy the child to 
        % the new document part
    else  
        append(newTemplateDocPart,clone(child));
    end
end

Replace the part to modify with the new template document part.

t.TemplateDocumentParts(toModifyIdx) = newTemplateDocPart;

Close the template.

close(t);

Confirm the changes to the template document part by using tmplview.

tmplview("updatedTemplate.htmtx",OpenDocument="partToModify");

tmplview window showing the contents of the partToModify template document part from the template existingTemplate.htmtx.

To learn how to examine the source or updated template file directly, see Open Template Files.

Input Arguments

expand all

Template text to copy, specified as an mlreportgen.dom.TemplateText object.

Output Arguments

expand all

Copied template text, returned as an mlreportgen.dom.TemplateText object.

Version History

Introduced in R2024b