Programming Guide : Writing a Template Assistant : How You Can Ensure Compatibility Between the Assistant and ApplyTemplate : Example--ApplyTemplate Compatibility
 
Share this page          
Example--ApplyTemplate Compatibility
In this example, the frame template assistant procedure from the previous example has been modified for use with the ApplyTemplate utility. The difference between this example and the previous one is that the procedure must place the dynamically generated trim field inside of the top-level composite field of the frame template.
For this example, assume that the $_TopComposite macro variable of the frame template has been assigned a value of FreetrimComposite.
First, the parameters are defined:
Procedure Create_Dyn_Fld
(
fs = FrameSource not null,
return_code = integer not null,
batch_mode = integer not null
)=
Because the procedure must support batch mode, a default string is specified. Otherwise, the user is prompted for the string:
if batch_mode = TRUE then
text_string.value = 'Hello, world!';
else
status = curprocedure.replypopup(messagetext =
    'Enter text string.',
     reply = text_string);
if status != PU_OK then
    return_code = ER_FAIL;
    return;
endif;
endif;
Next, a text trim field is created and loaded with the specified text string:
trim_field = freetrim.create();
trim_field.textvalue = text_string.value;
Finally, the procedure must assign a value to the return code parameter and attach the composite field to the form. Because the trim field must be assigned to the frame template's top composite field, a recursive search procedure is called to search for a composite field with the name FreetrimComposite. When it is found, the trim field is added to that composite:
topcomposite = callproc
    find_top_composite(fs.topform);
trim_field.ParentField = topcomposite;
return_code = ER_OK;
How You Can Attach the Assistant to a Frame Template
In the same application that contains the Create_Dyn_Fld frame assistant procedure, a frame template can be created (for example, My_Frame_Template). To associate the frame template My_Frame_Template with its assistant, open the Property Inspector for the frame template in its editor, and specify Create_Dyn_Fld as the assistant procedure.
When a user frame created from the My_Frame_Template frame template is run through the ApplyTemplate utility, any modifications made to the frame outside of the composite field defined by the $_TopComposite macro variable is unaffected.
The full text of the frame assistant procedure is as follows:
Procedure Create_Dyn_Fld
(
    fs = FrameSource not null,
    return_code = integer not null,
    batch_mode = integer not null
)=
declare
    trim_field = FreeTrim default null;
    text_string = StringObject;
    topcomposite = CompositeField default null;
    status = integer not null;
    find_top_composite = procedure returning CompositeField;
enddeclare
begin
    if batch_mode = TRUE then
        text_string.value = 'Hello, world!';
    else
        status  = curprocedure.replypopup(messagetext
            = 'Enter text string.',
                reply = text_string);
    if status != PU_OK then
            return_code = ER_FAIL;
            return;
    endif;
    endif;
    topcomposite = callproc
            find_top_composite(composite = fs.topform);
    trim_field.ParentField = topcomposite;

    return_code = ER_OK;
    return;
end;
procedure find_top_composite
(
    composite = compositefield;
)=

declare
    child = compositefield default null;
    i = integer not null;
enddeclare
begin
    if composite.name = 'FreetrimComposite' then
        return composite;
    endif;
    for i = 1 to composite.childfields.lastrow() do
        if composite.childfields[i].isa(class =
            CompositeField) = TRUE then
                child = find_top_composite(composite =
                    composite.childfields[i]);
                if child IS NOT NULL then
                return child;
            endif;
        endif;
    endfor;
    return NULL;
end;