Template laguage: Detect if another instance of same control template is present

Hi all,

I am looking how to detect if an instance of a control template is already populated on a window procedure.

Right now I am writing a control template which add some code in the data section. If multiple instances are implemented on the same window these will create duplicated labels.

So my idea to make my
#AT(%DataSectionAfterWindow),PRIORITY(5000)
conditional in some way that it won’t do anything if the first instance of the same template already has done this. Is this possible and if so how?

In this case you might be able to to this:

#AT(%DataSectionAfterWindow),PRIORITY(5000),WHERE(INSTANCE(%ActiveTemplateInstance)=1)

However, I suspect that this won’t be enough. This is a more robust solution:

#ATSTART
  #EQUATE(%GenSharedStuff, %False)
  #IF(NOT INLIST(%Procedure &':MyThing', %CustomFlags))
    #ADD(%CustomFlags, %Procedure &':MyThing')
    #SET(%GenSharedStuff, %True)
  #ENDIF
#ENDAT
#AT(%DataSectionAfterWindow),PRIORITY(5000),WHERE(%GenSharedStuff)
#ENDAT

The nice thing about the second option is that it will work in a much broader range of circumstances. Note that this example works when your template cares about others in the same procedure. If you need to check globally, then just drop the %Procedure portion of the %CustomFlag.

1 Like

Hi Mike,

Your suspicion was correct, the first option didn’t work. It appeared that for the only instance
%ActiveTemplateInstance=2 in the first procedure I tried. Probably because I added, removed and added again my control template.

The second solution will work I guess. Couldn’t test it yet because I overlooked some other stuff I need to reckon with when multiple instances are used. Will do that next week. I also will play what happens after a second instance is added and the first one is removed.

BTW I did’t know the symbol %CustomFlags, it isn’t in the documentation. But I found it declared in CW.tpl and ABChain.tpl. Useful to know it is available.

Thank you for this solution.

If you are talking about a #Control template, and you want to limit it to one instance, then remove the ,multi attribute.

The #Extension template also has the ,Multi attribute which limits it to once instance in a procedure.

Hi Richard,

Thank you, but my goal is to allow multiple instances, but also to prevent that some code is generated twice. For example the generation if local variables. The solution Mike posted works for me.

1 Like

This is off the top of my head, havent tested it but this would be another way to do it.

Where(%grpCountTemplateInstances('YourTemplateNameGoesHere') > 1)

#Group(%grpCountTemplateInstances,%pActiveTemplate)
#Declare(%Cnt)
#Set(%Cnt,0)
#For(%ActiveTemplate),Where(%ActiveTemplate = %pActiveTemplate)
Set(%Cnt,%Cnt+1)
#EndFor
#Return %Cnt

Fair enough. :grinning: Attached is a template which works in C6 so should work in later versions, which gives me info about a procedure and the app.

Load the global template first so it knows what viewer (notepad) you want to use, then drop the procedure extension onto the procedure you want to know stuff about. I find it quite useful, maybe others will which can give an insight into how the templates organise themselves.

Its work in progress so any bugs let me know.

AppInfo.zip (20.3 KB)

But it gives me info like this and lots of other info as well.

fwiw.

11/12/2022 21:19:35                 C:\C6 Work\AppBoost\AppBoost.app
=======================================================================================================================================================
Application: AppBoost.app
=======================================================================================================================================================


Active Template                             Type      Instance Parent Instance Primary Instance Instance Description                                                        

AsciiViewInListBox(ABC)                     EXTENSION 4        0                                Display ASCII file in list box ?Loc:AppBoostLevel                           
AsciiViewInListBox(ABC)                     EXTENSION 6        0                                Display ASCII file in list box ?Loc:AppBoostLevel:Radio1                    
IS_SysTray2(AAAIntelligentSiliconLtd)       EXTENSION 1        0                                IS_Systray2 Procedure Extension                                             
IS_EmbedInfo(AAAIntelligentSiliconLtd)      CODE      2        0                                Display Embed Info                                                          
IS_AppInfoLocal(AAAIntelligentSiliconLtd)   EXTENSION 3        0                                IS_AppInfo - Gives access to Application template info                      
IS_LocalProcedure(AAAIntelligentSiliconLtd) EXTENSION 5        0                                IS_LocalProcedure - Procedure Extension Template to Create Local Procedures 


ActiveTemplate                          AsciiViewInListBox(ABC)
ActiveTemplateType                      EXTENSION
ActiveTemplateInstanceDescription       Display ASCII file in list box ?Loc:AppBoostLevel
ActiveTemplateInstance                  4
ActiveTemplateOwnerInstance             0
ActiveTemplateParentInstance            0
ActiveTemplatePrimaryInstance           

ActiveTemplateInstanceDescription       Display ASCII file in list box ?Loc:AppBoostLevel:Radio1
ActiveTemplateInstance                  6
ActiveTemplateOwnerInstance             0
ActiveTemplateParentInstance            0
ActiveTemplatePrimaryInstance           


ActiveTemplate                          IS_SysTray2(AAAIntelligentSiliconLtd)
ActiveTemplateType                      EXTENSION
ActiveTemplateInstanceDescription       IS_Systray2 Procedure Extension
ActiveTemplateInstance                  1
ActiveTemplateOwnerInstance             0
ActiveTemplateParentInstance            0
ActiveTemplatePrimaryInstance           


ActiveTemplate                          IS_EmbedInfo(AAAIntelligentSiliconLtd)
ActiveTemplateType                      CODE
ActiveTemplateInstanceDescription       Display Embed Info
ActiveTemplateInstance                  2
ActiveTemplateOwnerInstance             0
ActiveTemplateParentInstance            0
ActiveTemplatePrimaryInstance           


ActiveTemplate                          IS_AppInfoLocal(AAAIntelligentSiliconLtd)
ActiveTemplateType                      EXTENSION
ActiveTemplateInstanceDescription       IS_AppInfo - Gives access to Application template info
ActiveTemplateInstance                  3
ActiveTemplateOwnerInstance             1
ActiveTemplateParentInstance            0
ActiveTemplatePrimaryInstance           


ActiveTemplate                          IS_LocalProcedure(AAAIntelligentSiliconLtd)
ActiveTemplateType                      EXTENSION
ActiveTemplateInstanceDescription       IS_LocalProcedure - Procedure Extension Template to Create Local Procedures
ActiveTemplateInstance                  5
ActiveTemplateOwnerInstance             0
ActiveTemplateParentInstance            0
ActiveTemplatePrimaryInstance

Hi Richard,

I try to investigate this template next week. On first sight it can be useful to learn how the templates work together. I found that it can make a difference where I place some code how for example the shipping Browse template decides to put some code as well or not.

Sometimes using #ADD() on a multi-valued symbol with the UNIQUE attribute can be useful too.

1 Like

Or without UNIQUE and check #IF (ITEMS(%MultiSymbol) = 1) after #ADD to know its the first one. Also can check #IF (INSTANCE(%MultiSymbol) = 1) later after #FOR/#FIX to know its first.

1 Like

Items was what I was trying to remember the other night but couldnt remember, thats the problem with being poisoned, I struggle to even remember what I wrote 5 mins ago in a previous embed sometimes, which makes programming virtually impossible.