# Change template text dynamically

**URL:** <https://discourse.nodered.org/t/change-template-text-dynamically/66264>\
**Category:** Dashboard\
**Created:** [10 August 2022 07:21 UTC](https://discourse.nodered.org/t/change-template-text-dynamically/66264 "2022-08-10T07:21:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mariulin](https://avatars.discourse-cdn.com/v4/letter/m/a9adbd/32.png) [@mariulin](https://discourse.nodered.org/u/mariulin)\
**Post date:** [10 August 2022 07:21 UTC](https://discourse.nodered.org/t/change-template-text-dynamically/66264/1 "2022-08-10T07:21:04Z")

</div>

Hi all.  
Is possible in same way, to change the text of a template node depending on a content of a file?

Example:  
I read a file:  
If, the content of that file is 1, then, a part of text in template, must be 111111  
If, the content of that file is 2, then, a part of text in template, must be 222222  
If, the content of that file is 3, then, a part of text in template, must be 333333

Thanks

---

<div class="post-metadata">

**Author:** ![knoepsche](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knoepsche/32/69450_2.png) [@knoepsche](https://discourse.nodered.org/u/knoepsche)\
**Post date:** [10 August 2022 16:35 UTC](https://discourse.nodered.org/t/change-template-text-dynamically/66264/2 "2022-08-10T16:35:36Z")

</div>

Don't know what it is you want to achieve but this works:

```auto
[
    {
        "id": "cd95808ee2c88012",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "a4208e8a679d608c",
        "type": "ui_template",
        "z": "cd95808ee2c88012",
        "group": "2dee2e04.7cf00a",
        "name": "Log Anzeige",
        "order": 7,
        "width": "15",
        "height": "14",
        "format": "<p id='text_xcvy124'>{{text}}</p>\n\n\n<script>\n (function(scope) {\n scope.text = 'This text is completely useless';\n scope.original = scope.text;\n\n scope.setText = function(nr){\n switch(nr){\n case 1:\n scope.text = scope.original.replace(/text/i, \"111111\");\n break;\n case 2:\n scope.text = scope.original.replace(/text/i, \"222222\");\n break;\n case 3:\n scope.text = scope.original.replace(/text/i, \"333333\");\n break;\n default:\n scope.text = scope.original;\n break;\n }\n }\n \n //handle arriving messages\n scope.$watch('msg', function(msg) {\n\t\t\t try{\n \t\t const nr = parseInt(msg.payload);\n \t\t scope.$applyAsync(scope.setText(nr));\n\t\t\t }catch(err){\n\t\t\t //console.error(err);\n\t\t\t }\n });\n })(scope);\n</script>",
        "storeOutMessages": false,
        "fwdInMessages": false,
        "resendOnRefresh": false,
        "templateScope": "local",
        "className": "",
        "x": 990,
        "y": 160,
        "wires": [
            []
        ]
    },
    {
        "id": "2b14afc2e82b06c7",
        "type": "function",
        "z": "cd95808ee2c88012",
        "name": "Next",
        "func": "msg.topic = 'File'\nmsg.payload = msg.payload[0].replace(/\\n*$/, \"\").trim();\nreturn [msg, {tick:true}];",
        "outputs": 2,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 630,
        "y": 440,
        "wires": [
            [
                "a4208e8a679d608c",
                "6ac3d954df84eeca"
            ],
            [
                "9b6e7b40b6133601"
            ]
        ]
    },
    {
        "id": "9b6e7b40b6133601",
        "type": "chunks-to-lines",
        "z": "cd95808ee2c88012",
        "name": "",
        "nbLines": 1,
        "linesFormat": "json",
        "decoder": "UTF-8",
        "x": 640,
        "y": 560,
        "wires": [
            [
                "2b14afc2e82b06c7"
            ]
        ]
    },
    {
        "id": "cad297235e92bc5a",
        "type": "ui_upload",
        "z": "cd95808ee2c88012",
        "group": "2dee2e04.7cf00a",
        "title": "Drop Text File (*.txt , utf-8)",
        "name": "upload",
        "order": 2,
        "width": 0,
        "height": 5,
        "chunk": 256,
        "transfer": "binary",
        "x": 270,
        "y": 560,
        "wires": [
            [
                "9b6e7b40b6133601"
            ]
        ]
    },
    {
        "id": "6ac3d954df84eeca",
        "type": "debug",
        "z": "cd95808ee2c88012",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1010,
        "y": 460,
        "wires": []
    },
    {
        "id": "2dee2e04.7cf00a",
        "type": "ui_group",
        "name": "Control",
        "tab": "721af69.d130208",
        "order": 1,
        "disp": true,
        "width": "15",
        "collapse": false,
        "className": ""
    },
    {
        "id": "721af69.d130208",
        "type": "ui_tab",
        "name": "Text",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

```

---

<div class="post-metadata">

**Author:** ![mariulin](https://avatars.discourse-cdn.com/v4/letter/m/a9adbd/32.png) [@mariulin](https://discourse.nodered.org/u/mariulin)\
**Post date:** [30 August 2022 08:39 UTC](https://discourse.nodered.org/t/change-template-text-dynamically/66264/3 "2022-08-30T08:39:35Z")

</div>

> [@knoepsche](#):
>
> hat it is you want to achieve but this works:

Thank you for the reply.  
I was on holiday. Tomorrow I will try your solution  
Bye

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [29 September 2022 08:40 UTC](https://discourse.nodered.org/t/change-template-text-dynamically/66264/4 "2022-09-29T08:40:15Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
