Update string that was loaded from a txt file

Q&A regarding installation and issues getting started with Servoy

Update string that was loaded from a txt file

Postby alexander.sternadel » Wed Nov 27, 2024 2:15 pm

Hi everyone,

I run into a problem, that I'm sure hase an easy fix but I can't seem to find it.
I'm using the web-client and it shall download an email-template (a text file with .eml as ending) and this e-mail is already prefilled with some values of variables.
This works fine when I have all the text and formating for the .eml in my code window, but as the email will need some nice desinging it will get too many line, so I want to load an external file, exchange the placeholder for the values of the variables and let the user download it as .eml

Loading the template works fine by using var vContent = plugins.file.readTXTFile(vPath), but how do I update the placeholders in vContent for the values of my variables?

Thanks for any help.

Cheers,
Alexander
alexander.sternadel
 
Posts: 14
Joined: Fri Nov 22, 2024 1:33 pm

Re: Update string that was loaded from a txt file

Postby vik.lamp.vl » Wed Nov 27, 2024 2:39 pm

Hi Alexander,

if im understanding you correctly youre trying to replace some placeholder-strings in your imported vContent?

You can do that with:
Code: Select all
utils.stringReplace(vContent,'placeholder','replacement_text')


Hope that helps :)

-Vik
Servoy Developer
Head of Development at HV-Office & Hogatec.IT

Always interested in deeper Servoy-Knowledge so lets link up!
vik.lamp.vl
 
Posts: 48
Joined: Wed May 26, 2021 3:37 pm
Location: Germany

Re: Update string that was loaded from a txt file

Postby amitchell1698743475 » Wed Nov 27, 2024 2:43 pm

Hi Alexander,

The solution we use for this is a replacePlaceholders method (see below) that sits inside of our utils scope.

Code: Select all
/**
* @param {String} _sText
* @param {Object} _oValues
*
* @return {String}
*
*/
function replacePlaceholders(_sText, _oValues) {
   /** @type {RegExp} */
   var r;
   
   _sText = _sText || '';
   _oValues = _oValues || {};
   
   var _aVars = Object.keys(_oValues);
   
   for (var i = 0; i < _aVars.length; i++) {
      r = new RegExp('\\[' + _aVars[i] + '\\]', 'gi');
      _sText = _sText.replace(r, _oValues[_aVars[i]]);
      
      r = new RegExp('\\{' + _aVars[i] + '\\}', 'gi');
      _sText = _sText.replace(r, _oValues[_aVars[i]]);
   }
   
   return _sText;
}


You will just need to ensure any of the placeholders you want to replace in your email-template file are wrapped in {} or [] parenthesis.

You can then just invoke the replacePlaceholders method like so:

Code: Select all
var vContent = 'Dear {CUSTOMER_NAME}'
var oValues = {
    customer_name: 'John Smith'
}

vContent = replacePlaceholders(vContent, _oValues);

// value of vContent is now 'Dear John Smith'


I hope this helps you solve your issue.

Cheers,

Alex.
amitchell1698743475
 
Posts: 2
Joined: Tue Oct 31, 2023 11:11 am

Re: Update string that was loaded from a txt file

Postby alexander.sternadel » Thu Nov 28, 2024 7:28 am

Thanks Alex, that solution works perfect!
alexander.sternadel
 
Posts: 14
Joined: Fri Nov 22, 2024 1:33 pm

Re: Update string that was loaded from a txt file

Postby mboegem » Thu Nov 28, 2024 9:58 am

The above function has been developed to allow placeholders like '{CUSTOMER_NAME}' or [CUSTOMER_NAME]
These stand out nicely in larger texts and the object keys are case-insensitive.

Servoy also supports placeholders out of the box:
Code: Select all
var vContent = 'Dear %%CUSTOMER_NAME%%'
utils.stringReplaceTags(vContent, {CUSTOMER_NAME: 'Alexander'})


The Object used in the stringReplaceTags function can also be a record, foundset or form
The docs will provide a little more context on this.

Hope this helps
Marc Boegem
Solutiative / JBS Group, Partner
Servoy Specialist
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance Developer

Image
User avatar
mboegem
 
Posts: 1817
Joined: Sun Oct 14, 2007 1:34 pm
Location: Amsterdam


Return to I'm just getting started

Who is online

Users browsing this forum: No registered users and 0 guests