{"id":4117,"date":"2022-12-15T12:55:46","date_gmt":"2022-12-15T12:55:46","guid":{"rendered":"https:\/\/info.documotor.com\/?page_id=4117"},"modified":"2023-01-25T10:22:50","modified_gmt":"2023-01-25T10:22:50","slug":"template","status":"publish","type":"page","link":"https:\/\/info.documotor.com\/?page_id=4117","title":{"rendered":"Template"},"content":{"rendered":"\n<p>The template function is used to generate a base64 document based on an already existing DocuMotor template.<\/p>\n\n\n\n<h3>Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">string template(string $id, string $stage, any $inputData)<\/code><\/pre>\n\n\n\n<p>Generates a base64-encoded document, which results from parsing the $inputData through a DocuMotor template. The template is specified by its $id and, if applicable, its $stage.<\/p>\n\n\n\n<h3>Parameters<\/h3>\n\n\n\n<p><strong>$id:<\/strong> The id of the DocuMotor template that will be used to generate a base64-encoded document.<br><strong>$stage: <\/strong>The stage of the versioned template. In case the template is not a versioned template then <code>`null`<\/code> or <code>''<\/code> can be used for this parameter. <br><strong>$inputData:<\/strong> The data that will be parsed through the DocuMotor template, before it gets converted to a base64 string. There are no specific restrictions when it comes to the shape of the data. It can be a json_object, an array, a simple value, or nothing at all.<\/p>\n\n\n\n<h3>Example<\/h3>\n\n\n\n<p>Below, a simple case where the template function is used is presented. It is worth mentioning that the sample data will only be used for the $inputData parameter. The rest of the parameteres will be manually inserted.<\/p>\n\n\n\n<pre title=\"Data\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n    \"People\": [  \/\/This array will be used as the input data\n        {\n            \"first_name\": \"Pyotr\",\n            \"last_name\": \"Brophy\",\n            \"email\": \"pbrophy0@twitpic.com\"\n        },\n        {\n            \"first_name\": \"Vivyanne\",\n            \"last_name\": \"McLeoid\",\n            \"email\": \"vmcleoid1@cyberchimps.com\"\n        },\n        {\n            \"first_name\": \"Esme\",\n            \"last_name\": \"MacIlhagga\",\n            \"email\": \"emacilhagga2@ucoz.com\"\n        }\n    ]\n}<\/code><\/pre>\n\n\n\n<p>In the transformation below, a placeholder template id is used and the $stage parameter is set to <code>null<\/code>, as that particular template isn&#8217;t versioned. Keep in mind that template ids are specific to each DocuMotor tenancy. The data is inserted as {KeyName: People}, as in this case using just &#8220;People&#8221; would not result in a valid jason. This, as well as how to retrieve a template&#8217;s id, will be clarified later in this article.<\/p>\n\n\n\n<pre title=\"Transformation\" class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">{\n    \"TemplatePath\": template('62f366cb65189c6e35c8cbee', `null`, {KeyName: People})\n}<\/code><\/pre>\n\n\n\n<p>As seen in the Output block below, the template function results in a base64 string of document that is generated when the $inputData is parsed through the template.<\/p>\n\n\n\n<pre title=\"Output\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n    \"TemplatePath\": \"UEsDBBQABggIAAAAIQCn7zB05w...\" \/\/for space reasons the base64 string has been truncated\n}<\/code><\/pre>\n\n\n\n<p>The key that holds the generated base64 string can be used to insert the corresponding document into the current template. This can be achieved using the <a href=\"https:\/\/info.documotor.com\/?page_id=2604\" data-type=\"page\" data-id=\"2604\">Text Element binding<\/a>.<\/p>\n\n\n\n<h3>Notes<\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignright size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/12\/snipnew-1024x485.png\" alt=\"\" class=\"wp-image-4164\" width=\"501\" height=\"238\" srcset=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/12\/snipnew-1024x485.png 1024w, https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/12\/snipnew-300x142.png 300w, https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/12\/snipnew-768x364.png 768w, https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/12\/snipnew.png 1133w\" sizes=\"(max-width: 501px) 100vw, 501px\" \/><\/figure><\/div>\n\n\n\n<p><strong>Finding the template id<\/strong><\/p>\n\n\n\n<p>There are two ways of finding a template id. The simplest one is to retrieve it from the template&#8217;s URL, where the template id is the last part of the link.<\/p>\n\n\n\n<p>Alternatively, it can be found by pressing the &#8220;Get Snippet&#8221; (step 1) button on the top right corner of the editor. In the appearing window, the template id can be easily spotted, as seen in the image below (step 2).<\/p>\n\n\n\n<div style=\"height:27px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"993\" height=\"757\" src=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/12\/idnew-2.png\" alt=\"\" class=\"wp-image-4172\" srcset=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/12\/idnew-2.png 993w, https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/12\/idnew-2-300x229.png 300w, https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/12\/idnew-2-768x585.png 768w\" sizes=\"(max-width: 993px) 100vw, 993px\" \/><\/figure>\n\n\n\n<p><strong>Finding the template&#8217;s stage<\/strong><\/p>\n\n\n\n<p>It is possible to find the different stages available in the unit in the unit&#8217;s <em>management <\/em>page. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"299\" src=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/12\/image-4-1024x299.png\" alt=\"\" class=\"wp-image-4269\" srcset=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/12\/image-4-1024x299.png 1024w, https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/12\/image-4-300x88.png 300w, https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/12\/image-4-768x224.png 768w, https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/12\/image-4.png 1088w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The stage needs to be defined exactly as it is defined in the corresponding unit and has to be the stage that is active on the template. To see the wanted template&#8217;s active stage go to the <em>templates<\/em> page in the unit and find the template. Before entering the template&#8217;s editor it is possible to see all the different versions of the template. Here it is easy to see which stage the wanted version of the template is, since it is bolded.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"297\" src=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/12\/image-5-1024x297.png\" alt=\"\" class=\"wp-image-4270\" srcset=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/12\/image-5-1024x297.png 1024w, https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/12\/image-5-300x87.png 300w, https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/12\/image-5-768x222.png 768w, https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/12\/image-5.png 1091w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>Input Data<\/strong><\/p>\n\n\n\n<p>In order to determine how the $inpuData parameter needs to be defined, one must think of whether or  not the used data will produce a valid json structure. In order to further clarify this, the sample data from above will be used as an example. <\/p>\n\n\n\n<pre title=\"Transformation\" class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">{\n    \"TemplatePath\": template('62f366cb65189c6e35c8cbee', `null`, People)\n}<\/code><\/pre>\n\n\n\n<p>If $inputData is defined like in the transformation block above, an error will be triggered, as the data that is inserted into the external template&#8217;s sample data isn&#8217;t a valid JSON structure. In this case, the external template is the one that is specified in the template function.<\/p>\n\n\n\n<pre title=\"Data in the current template\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n    \"People\": [  \/\/This array is used as the input data\n        {\n            \"first_name\": \"Pyotr\",\n            \"last_name\": \"Brophy\",\n            \"email\": \"pbrophy0@twitpic.com\"\n        },\n        {\n            \"first_name\": \"Vivyanne\",\n            \"last_name\": \"McLeoid\",\n            \"email\": \"vmcleoid1@cyberchimps.com\"\n        },\n        {\n            \"first_name\": \"Esme\",\n            \"last_name\": \"MacIlhagga\",\n            \"email\": \"emacilhagga2@ucoz.com\"\n        }\n    ]\n}<\/code><\/pre>\n\n\n\n<pre title=\"Data in the external template\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">[  \/\/Not a valid JSON structure\n    {\n        \"first_name\": \"Pyotr\",\n        \"last_name\": \"Brophy\",\n        \"email\": \"pbrophy0@twitpic.com\"\n    },\n    {\n        \"first_name\": \"Vivyanne\",\n        \"last_name\": \"McLeoid\",\n        \"email\": \"vmcleoid1@cyberchimps.com\"\n    },\n    {\n        \"first_name\": \"Esme\",\n        \"last_name\": \"MacIlhagga\",\n        \"email\": \"emacilhagga2@ucoz.com\"\n    }\n]<\/code><\/pre>\n\n\n\n<p>Hovewer, if the $inputData parameter is defined as it is defined in the transformation below, then the sample data of the external template will have a valid JSON structure.<\/p>\n\n\n\n<pre title=\"Transformation\" class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">{\n    \"TemplatePath\": template('62f366cb65189c6e35c8cbee', `null`, {KeyName: People})\n}<\/code><\/pre>\n\n\n\n<pre title=\"Data in the external template\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n    \"KeyName\": [  \/\/Valid JSON Structure\n        {\n            \"first_name\": \"Pyotr\",\n            \"last_name\": \"Brophy\",\n            \"email\": \"pbrophy0@twitpic.com\"\n        },\n        {\n            \"first_name\": \"Vivyanne\",\n            \"last_name\": \"McLeoid\",\n            \"email\": \"vmcleoid1@cyberchimps.com\"\n        },\n        {\n            \"first_name\": \"Esme\",\n            \"last_name\": \"MacIlhagga\",\n            \"email\": \"emacilhagga2@ucoz.com\"\n        }\n    ]\n}<\/code><\/pre>\n\n\n\n<p>Lastly, it is worth mentioning that the KeyName can be defined by the user, and usually it is named in order to facilitate a binding in the external template.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The template function is used to generate a base64 document based on an already existing DocuMotor template. Syntax Generates a base64-encoded document, which results from parsing the $inputData through a DocuMotor template. The template is specified by its $id and, if applicable, its $stage. Parameters $id: The id of the DocuMotor template that will be [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":0,"parent":271,"menu_order":191,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/4117"}],"collection":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4117"}],"version-history":[{"count":45,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/4117\/revisions"}],"predecessor-version":[{"id":4272,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/4117\/revisions\/4272"}],"up":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/271"}],"wp:attachment":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}