Templates

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
anmldr
Posts: 459
Joined: Tue Sep 11, 2012 11:13 pm

Templates

Post by anmldr » Sat Jul 18, 2020 4:56 am

I found "template" recently in the dictionary. Other than that, I have not found much info about where it might be advantageous to use them or really even how to use them. Please point me to a LC Lesson, video, or any source which I can use to better understand them.

If I understand correctly, the template is created (card or control). Let's say I am creating a templateCard, I can set all of the properties of the new "card". However, no card really exists, right? The templateCard is only the instructions for how to create a new card. Similarly for template controls.

I have created controls before that DO exist on a card that also holds things like graphics and I copy those and add them to a new card when I use "create card" or "card new". Of course, creating cards this way means that the card's properties need to be set each time that the new card is created. I could create a function that would do this and call on that whenever a new card is created.

So, what is the difference or advantage of using templates? Am I missing something (besides not understanding how to use them best)?

TIA,
Linda

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Templates

Post by bogs » Sat Jul 18, 2020 11:52 am

The advantage to using them is much as you say in the second paragraph, in that you are looking for uniformity, and you set the generic properties of the (card, object, whatever), then when ever you want another (card, object, whatever) you call the template and it gets placed, already set up.

In the case your creating them on the fly, this is a completely abstract scenario, no (card, object, whatever) exists anywhere but in code, you call the code, and the doohickey appears as an object according to the template.

The second way to use a template (of sorts) is to design the doohickey visually, and then your end user would determine when and where to use it. This lesson is about using template controls in that manner, like you are talking about in your 3rd paragraph.
http://lessons.livecode.com/m/4071/l/74 ... -in-an-app

I don't think your missing anything, the above is just reaffirmation of what you've already stated. As to when to use which of the two methods, if I were creating a template of multiple controls, arranged in a specific way, I'd use the graphic approach since it makes the layout easy and perfect every single time, like the lesson above shows.

If I needed to create single objects, depending on a set of circumstances, then I would use the template objects most likely. Say you had a group, and the group contained buttons and corresponding fields inline, and the group doesn't change size but the number of button / field combos was changeable. In that case, I would use templateButton and templateField to set them up in the group.

That isn't the only way it would be used, just how I use it most often.

I also came across this tidbit from Craig which is something else to keep in mind, it illustrates one of the main differences between a graphic template, and the global templateObject -
Re: create option button
If you think about it, you are using your "parent" button as a template. Same concept, but the templateButton is global; that is, you can create new controls anywhere, not just on the current card. Also, you can change the properties of the templateButton from anywhere. Or nowhere, for example, from the message box
Image

bobcole
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 135
Joined: Tue Feb 23, 2010 10:53 pm
Location: Saint Louis, Missouri USA

Re: Templates

Post by bobcole » Sat Jul 18, 2020 4:30 pm

Using the templateGraphic makes it easy to assure graphics have some identical properties.
This script initializes the templateGraphic once then uses it 64 times to build a checkerboard by changing the location and background color.
Put this script into a button and give it a try.
Bob

Code: Select all

on mouseUp pButtonNumber
   --initialize templateGraphic
   put 30 into tPixels --height and width of squares
   set the style of the templateGraphic to "rectangle"
   set the height of the templateGraphic to tPixels
   set the width of the templateGraphic to tPixels
   set the opaque of the templateGraphic to true
   set the showName of templateGraphic to true
   set the label of templateGraphic to empty
   --initialize other
   put 50 into tStartRowLoc  --number of pixels from top of stack
   put 30 into tStartColLoc  --number of pixels from left edge of stack
   put tStartColLoc,tStartRowLoc into tStartLoc  --confusing, seems backward, but: col = x axis, row = y axis
   put 235,235,235 into bgColor1 --Mercury (a light gray)
   put 193,193,193 into bgColor2 --Magnesium (a medium gray)
   put 0 into tCellCount
   
   -- code to build 8x8 grid
   repeat with r = 1 to 8
      add 1 to tCellCount
      put tStartRowLoc + tPixels * (r - 1) into tRowLoc
      repeat with c = 1 to 8
         add 1 to tCellCount
         put tStartColLoc + tPixels * (c - 1) into tColLoc
         put tColLoc,tRowLoc into tCellLoc  --confusing, seems backward, but: col = x axis, row = y axis
         set the loc of the templateGraphic to tcellLoc --starts at 30,50
         if (tCellCount mod 2 = 0) then --alternate colors
            set the backgroundColor of the templateGraphic to bgColor1 --even
         else
            set the backgroundColor of the templateGraphic to bgColor2 --odd
         end if  
         put r & comma & c into tCellName       
         create graphic tCellName
      end repeat
   end repeat
end mouseUp

stam
Posts: 2682
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Templates

Post by stam » Fri Jul 31, 2020 1:50 pm

So that’s a bit like a class then?
Is this only for graphics?

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Templates

Post by Klaus » Fri Jul 31, 2020 1:55 pm

stam wrote:
Fri Jul 31, 2020 1:50 pm
So that’s a bit like a class then?
Sorry, no idea.
stam wrote:
Fri Jul 31, 2020 1:50 pm
Is this only for graphics?
No, EVERY object (stack, card etc.) has a TEMPLATExxx that you can modify before creating that object.

stam
Posts: 2682
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Templates

Post by stam » Fri Jul 31, 2020 2:05 pm

Klaus wrote:
Fri Jul 31, 2020 1:55 pm
Sorry, no idea.
Thanks for the rapid response Klaus!

By class I meant the classical “blueprint” from which to create instance objects. Good to know we can sorta do this for interface elements.

If I modify a template I presume that changes for the lifetime of the app? Or do the changes stay in scope only for the current handler/script?

I‘m still exploring LC, so have no current use for this and haven’t looked into it yet, but I’m envisioning heavy use of this for some future projects...

Thanks in advance!

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Templates

Post by bogs » Fri Jul 31, 2020 2:07 pm

stam wrote:
Fri Jul 31, 2020 1:50 pm
So that’s a bit like a class then?
Very similar but not exactly. Templates only cover existing controls, as far as I know, a class can be comprised of any object.
Image

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Templates

Post by Klaus » Fri Jul 31, 2020 2:13 pm

Hi stam,
stam wrote:
Fri Jul 31, 2020 2:05 pm
By class I meant the classical “blueprint” from which to create instance objects. Good to know we can sorta do this for interface elements.
I have a vague idea about "class", but don't have any experience in OOP, so I cannot answer this exactly.
But bogs already did.
stam wrote:
Fri Jul 31, 2020 2:05 pm
If I modify a template I presume that changes for the lifetime of the app? Or do the changes stay in scope only for the current handler/script?
No that will last for the "lifetime" (session) of the app.
Keep in mind that the IDE resets the TEMPLATExxx everytime you drag any object from the TOOLS palette!

If you also want to reset the TEMPLATExxx you have been modifying, just do:

Code: Select all

...
reset the templatebutton
# and/or any other templatexxx
...
Best

Klaus

JackieBlue1970
Posts: 64
Joined: Thu Jan 16, 2020 10:28 pm
Location: Max Meadows, VA USA

Re: Templates

Post by JackieBlue1970 » Fri Jul 31, 2020 3:13 pm

stam wrote:
Fri Jul 31, 2020 1:50 pm
So that’s a bit like a class then?
From what I can see, it is like Inheritance of properties in OOP rather than an instance of class.

Moderaters note: Removed the wrong quoting.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9662
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Templates

Post by dunbarx » Fri Jul 31, 2020 4:12 pm

It was mentioned that a template only applies to "existing" objects, fields, graphiics, etc.

Is there any sense in asking if such a thing as the "templateWidget" can exist at all? I only ask because one could build a personal custom widget in LCB and then set the template.

Craig

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”