31.2.2. Editing text templates
If the Text Document is selected as the template type in the template designer, the text editor is opened in the text template input mode.
Text template editing mode is also available for text documents with the Text template extension set.
31.2.2.1. Text template format
The whole text of the template is divided into areas. Areas must follow each other and cannot intersect or be included in each other. In the template text the areas are highlighted as follows:
#Area ХХХХХ #EndRegion
Where ХХХХХ is an area name.
You do not need to specify the end of the area. The declaration of the area beginning means the end of the previous area.
To store area options in several languages, you can specify a language code after an area name.
#Area Header1 RU #EndRegion #Area Header1 EN #EndRegion
If the language code is not specified for some variant of the area, then this variant of the area is returned when the language code that is not specified in any variant of the area descriptions is passed to the GetArea() method.
Each area consists of service and text parts.
#Area ХХХХХ [service part] [text part] #EndRegion
31.2.2.2. Area service part
The service part of area has no explicit boundaries. It includes all the lines from the beginning of the area that start with the special character "#". The service part describes the text template fields used within the area. If service part is not necessary, it may be absent.
The control structures of the text template are highlighted in color.
31.2.2.2.1. Service area structure
The service part of the area consists of declarations of some common parameters of the area and descriptions of the field formats included in the area. All descriptions made in the service part of the area are valid only within the region. If the field format is described in a certain area and this field is found in another area without a format description, the default format is used for it in that another area.
At the beginning of the service part of the area, the keyword of the common area #ReplaceChar A B may follow. It is used to replace characters within the lines of the area:
- A is a character to replace.
- B is a character replacing the A character.
The characters must be enclosed in single quotes.
For example, if you write #ReplaceChar "@" "#", then inside the lines the "@" characters not in the field names will be replaced with "#".
It makes sense to use this keyword in cases where the design of the lines of the template should include the # character (its direct use would indicate that the text is a service text).
31.2.2.2.2. Field descriptions
The replacement description is followed by a description of the field formats used in the area.
To create a field, use the #Field FFFF keyword, where FFFF is a name of the field whose format is described.
Then the following lines contain keywords to describe the field. The field description is active until the beginning of the next field description.
#Align {Left | Right | Center | Justify} indicates the alignment of the field within its character space.
#Field OperationMode #Align Justify
#Format <Format string> specifies the format string for the field output.
#Field Date1 #Format "DF=dd.MM.yy" #Field Tm1 #Format "ND=4; NFD=0; NDS=."
#Block <Parameter> indicates that space reserved for the field must be filled with the "#" characters. If <Parameter> = True and the contents of the field does not fit in the allotted space, the rest space is filled.
#Field ReportSummary #Format "ND=18; NFD=2; NDS=." #Block True
31.2.2.3. Area text part
The text part of the area consists of the lines of the text template. It begins after the last line of the service part of the area, and continues to the area end. Inside the text template lines, you can specify a field name: [FFFF].
The number of spaces reserved for the field corresponds to the number of spaces indicated by the brackets (including brackets). If the field size is one character, one bracket is allowed. Example:
![Name ]! [Code ]! [Description ] !
If the field name is shifted to the left bracket, it is considered that the field has left alignment, and if the name is shifted to right bracket, then the right alignment assumed. If there are spaces to the left and right of the field name, it is centered within the character space of the field.
If the field name is greater than the character space allocated for the field, the field name is indicated using the Fields keyword.
Example:
#Field Number #Format "ND=3; NFD=0" #Field Code #Format "ND=5; NFD=0" #Field Remark #Format "ND=1" ![ ]! [ Code]![ Description ]! [! #Fields Number Remark
Parameters in the Fields keyword are specified only for fields with names not indicated in the body of the template in brackets.
31.2.2.3.1. Auto-wrap
If the text does not fit in the allocated field size, it may be necessary to use automatic text wrap to a new line. For this purpose you can use the indication of the location of the field in the template in curly and angle brackets:
- {FFFF}. The field area is limited by curly brackets. They show that the autowrap of text stored in the FFFF field is possible onto this line, and indicate the area for the text that does not fit in the field FFFF before. In this case, even if the line does not need to display any of the fields, the line is still displayed.
- <FFFF>. The field area is limited by angle brackets. They show that auto-wrap of text stored in the FFFF field is possible, and indicate the area for the text that does not fit on the previous line in the FFFF field. In this case, if all the fields present in the line are indicated in angle brackets, but none of them are required for text output, the line is not displayed. If, when output text from a field to a line in which the field is placed in angle brackets, this text still does not fit, that line is duplicated until all the text from this field is displayed.
Example:
![ YY]! [XX ]![ ZZ]! ! ! {XX }! ! ! ! <XX >! !
This example indicates that field XX is placed on the first line. The second line contains the part of the XX field that does not fit in the first line. The third line contains part of the XX field, which does not fit in the first and in the second line. If there is no text in the XX field for the third line, it is not displayed at all; and if this text remains, the third line is displayed up to the end of the text from the XX field.
31.2.2.4. Text template keyword description
Area
Description:
Indicates the beginning of the template area. If the previous area is not finished, then the end of the previous area.
Syntax:
#Area <Area name> <Language code>
Parameters:
<Area name> Required
Specifies the name of the area to receive it by the GetArea() method of a text document.
<Language code> Optional
Specifies the language code of the area. If you intend to use the configuration in several languages, then you can specify several areas with the same name in the text template. In this case each area should contain a language code. If the language code is not specified, this area is selected when the area is requested with a language code not specified in the description of the areas.
EndOfArea
Description:
Indicates the explicit end of the template area.
Syntax:
#EndRegion
ReplaceChar
Description:
Allows replacing characters within area lines.
Syntax:
#ReplaceChar <Char to replace> <Replacing char>
Parameters:
<Char to replace> Required
Indicates the character in single quotes to be replaced.
<Replacing char> Required
Specifies character to replace <Char to replace>.
Field
Description:
Specifies the field for which you want to specify formatting keywords.
Syntax:
#Field <Field name>
Parameters:
<Field name> Required
Field name.
Align
Description:
Specifies alignment when displaying the field contents.
Syntax:
#Align <Align parameter>
Parameters:
<Align parameter> Required
Field alignment value. Can take the following values:
- Left
- Right
- Center
- Justify
Format
Description:
Specifies the output format of the field.
Syntax:
#Format <Format string>
Parameters:
<Format string> Required
Defines the format for representing the field value.
Example:
"DF=dd.MM.yy"
Description:
If the parameter value is True, filling mode is set. The "#" character is displayed in the entire allocated space in a field if its value does not fit into the space.
Syntax:
#Block <Parameter>
Parameters:
<Parameter> Optional
Determines whether the field must be filled. Can take the following values:
- True
- False
Fields
Description:
Specifies a list of fields which names cannot be specified at their place.
Syntax:
#Fields <Field name 1> <Field name 2> … <Field name N>
Parameters:
<Field name> Required
Field name. It is necessary to specify all field names that are not listed in the text of the template area.
31.2.2.5. Invoice print example
Let us suppose that the configuration object Document called Invoice has a template with name PrintText in the list of text-type templates:
#Area Header RU Invoice # [DocumentNumber ] From: [From ] To: [To ] ================================================= | N| Description | Price |Qty| Amount | #Area Line RU #Field Price #Format "ND=10; NFD=2; NDS=." #Field Qty #Align Right #Format "ND=4; NFD=0; NDS=." #Field Amount #Format "ND=12; NFD=2; NDS=." | + + + + | |[]|[Description ]|[ Price]|[ ]|[ Amount]|#Fields Number Qty | |<Description >| | | | #Area Footer RU #Field TotalQty #Align Right #Format "ND=4; NFD=0; NDS=." #Field TotalAmount #Align Right #Format "ND=12; NFD=2; NDS=." ================================================= Total [ ] [TotalAmount] #Fields TotalQty CEO: [CEO ]
This template contains the following areas:
- Header. Required to output the report header.
- Line. Required to output the table.
- Footer. Required to output totals.
The document form contains the Print button. Pressing it triggers the Click() event. The handler procedure of this event is placed in the form module.
Procedure PrintToText(Button) TextDoc = New TextDocument(); PrintText(TextDoc); TextDoc.Show(); EndProcedure
The procedure creates the text document TextDoc and calls the procedure PrintText(TextDoc), which fills the text document based on the invoice data. The procedure is located in the document module. After filling out the document, it is displayed on the screen.
Text of the PrintText() procedure:
Procedure PrintText(TextDoc) Export // Get the template Template = GetTemplate("PrintText"); // Set language code Template.TemplateLanguageCode = "RU"; // Header Area = Template.GetArea("Header"); Area.Parameters.DocumentNumber = Number; Area.Parameters.From = Format(Date,"DF=dd.MM.yyyy"); Area.Parameters.To = Contractor; TextDoc.Output(Area); // Processing the table part "Components" LineTotal = New Structure("TotalQty, TotalAmount",0,0); For Each LineComponent From Components Do Area = Template.GetArea("Line"); Area.Parameters.Number = LineComponent.LineNumber; Area.Parameters.Description = LineComponent.Products; Area.Parameters.Qty = LineComponent.Quantity; Area.Parameters.Price = LineComponent.Price; Area.Parameters.Amount = LineComponent.Amount; TextDoc.Output(Area); LineTotal.TotalQty = LineTotal.TotalQty + LineComponent.Quantity; LineTotal.TotalAmount = LineTotal.TotalAmount + LineComponent.Amount; EndDo; // Footer Area = Template.GetArea("Footer"); Area.Parameters.Fill(LineTotal); IR = InformationRegisters.Responsibles; Area.Parameters.Director = IR.GetLast(Date).Director; TextDoc.Output(Area); EndProcedure