<td colspan="2" align="center">
    <h2>{$params_tpl['title']}{$params_tpl['help']}</h2>
    <div class="" style="display: flex; flex-direction: column; ">{$tpl_types['field1']}</div>
    <div class="" style="display: flex; flex-direction: column; ">{$tpl_types['template']}</div>
    <div class="" style="display: flex; flex-direction: column;">{$tpl_types['result']}</div>
    <div class="" style="display: flex; flex-direction: column;">{$tpl_types['action']}</div>
    <div class="" style="display: flex; flex-direction: column;">{$tpl_types['filename']}</div>
</td>
<script>
    (function() {
        var url = $('textarea[name="calc_params[{$tpl_num}][template]"]');
        var flexField = $('select[name="calc_params[{$tpl_num}][field1]"]');
        var template = $('textarea[name="calc_params[{$tpl_num}][template]"]');

        var values = {};
        for (var i = 0; i < flexField[0].length; i++) {
            values[flexField[0][i].innerHTML] = flexField[0][i].getAttribute('value');
        }        

        flexField.prepend('<option value="" selected></option>');

        template.change(function() {
            {literal}
            var separator =/{([^}]+)}/g;
            {/literal}
            function replace() {
              //@fixme Здесь как будто бы ничего не меняется, нужно обсудить
                for (var i = 0; i < c; i++) {
                    for (var  j = 0; j < Object.keys(values).length; j++) {
                        if(a.search('{' + Object.keys(values)[j] + '}')) {
                            var re = Object.keys(values)[j];
                            {literal}
                            if (a.indexOf("\n")) {
                                a = a.replace(/\n/, "\\r\\n");
                            }
                            a = a.replace('{' + re + '}', '{' + values[Object.keys(values)[j]] + '}');
                            {/literal}
                        }
                    }
                }
              const newValue = template.val();
              const optionsNames = newValue.split(separator).filter(Boolean);
              const optionsFieldsId = optionsNames
                .filter(optionName => Object.prototype.hasOwnProperty.call(values, optionName))
                .map(optionName => {
                  const fieldIdWithF = values[optionName];
                  return fieldIdWithF.split('.')[0].replace('f', '');
                })
              setStartCalcFields(optionsFieldsId || []);
            }
            var a = template.val();
            //количество элементов на замену
            var c = 0;
            var b = a.split(separator);
            for (var  i = 0; i < b.length; i++) {
                if(b[i] !== "") c++;
            }
            replace();
            set_tpl_events();
        });

        $(flexField).change(function() {
            if ($(this).val() == "") {
                return;
            }

            var text = url.val();
            var option = $('option[value="' + $(this).val() +'"]');
            {literal}
            var optinForInsert = '{' + option.html() + '}';
            var position = $(url).prop('selectionStart');
            text = text.substr(0, position) + optinForInsert + text.substr(position);
            {/literal}

            url.val(text);
            var index = this.selectedIndex;
            var indextext = this.selectedIndex.innerHTML;
            var indexVal = flexField[0][index].getAttribute('value');

            $(url).change()
            this.selectedIndex = 0;
        });
    })();
</script>