texttemplateconverter TTextTemplateConverter component
Return to Introduction  Previous page  Next page
Overview
The TextTemplateConverter is the utility which translates some specified %keywords% inside the text into some specified values. It can be used as plug-in for SendMail component to replace some keywords in email templates (like %recipient_email% or %sender_name%) to their actual values.  

How to use?
Drop component on your form and specify some keywords which you are using in your text templates to the Name properties of Params collection. Also specify the text strings, which should be replace that keywords, to the Value propertes of Params. You can modify the collection of Names/Values both at design and run-time.  
 
When your program calls the Convert method, it will find all the keywords (specified in Name properties) within the text template and replace them to their actual values (specified in Value properties). For example, our Params property contains 2 following items:  
Item1:  
Name  = %RECIPIENT_NAME%  
Value = Bill Gates  
Item2:  
Name  = %RECIPIENT_EMAIL%  
Value = billgates@microsoft.com  
 
Plus we have the text template, which contains %RECIPIENT_NAME% and %RECIPIENT_EMAIL% keywords instead of real name and email address. For example, the text like:  
To: %RECIPIENT_NAME% <%RECIPIENT_EMAIL%>  
Dear %RECIPIENT_NAME%,  
etc, etc...  
 
After calling the Convert method, all the keywords specified in Name property of each item will be replaced by the words specified in Value property. For example, after convertion, the text displayed above will looks like follows:  
To: Bill Gates <billgates@microsoft.com>  
Dear Bill Gates,  
etc, etc...  
 
iiinfo The keywords can looks like %KEYNAME%, or <KEYNAME>, or just KEYNAME, whatever you like. In most cases you should specify the keywords which are used in your text templates just once, and you can do it at design-time and don't modify them at run-time at all.  
 
However, your program should specify required Values every time before processing of the template (before the program calls Convert method to translate the keywords into the values). To specify the Values for each required keyword (Names) you can use ParamByName method, or just enumerate each item of Params collection. Here is some examples:  
// how to add the the template parameters at run-time  
var  
  Param: TacTemplateParameter;  
begin  
  Param := TextTemplateConverter1.Params.Add;  
  Param.Name := '%keyword%';  
  Param.Value := 'Value';  
 
// how to modify the template values at run-time  
TextTemplateConverter1.ParamByName('%keyword%').Value := 'value';  
TextTemplateConverter1.ParamByValue('value').Name := '%another_keyword%';  
 
// also you can enumerate all parameters  
var  
  I: Integer;  
begin  
  I := TextTemplateConverter1.Count;  
  if I <> 0 then  
   for I := 0 to I - 1 do  
    with TextTemplateConverter1 do  
     Caption := Params[I].Name + ' = ' + Params[I].Value;  
end;  
 
tip If you are using this component as plug-in for SendMail component, to replace the keywords in email templates, you don't need to call Convert method. All that you need is just point the TextTemplateConverter to the TemplateConverter property of SendMail component and all the text of outgoing email, the body of message and the message headers will be translated automatically.  

See also
SendMail component.  
File not found.