Templates
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Templates
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
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
Re: Templates
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 -
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
-
- VIP Livecode Opensource Backer
- Posts: 135
- Joined: Tue Feb 23, 2010 10:53 pm
- Location: Saint Louis, Missouri USA
Re: Templates
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
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
Re: Templates
So that’s a bit like a class then?
Is this only for graphics?
Is this only for graphics?
Re: Templates
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!
Re: Templates
Hi stam,
But bogs already did.
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:
Best
Klaus
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.
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
...
Klaus
-
- Posts: 64
- Joined: Thu Jan 16, 2020 10:28 pm
- Location: Max Meadows, VA USA
-
- VIP Livecode Opensource Backer
- Posts: 9662
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Templates
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
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