TypoScript: Temp vs. Lib
Posted on September 16th 2010 at 22:10 inside TypoScript with 2 commentsWhen using TypoScript we often have to work with objects like TEXT, HTML, COA.. you name it. Some developers store their objects inside lib., others will use temp.. But what's the difference? And when should you use the one, and when the other?
A simple difference
Objects that are stored inside temp. are temporary, and therefore not cached. While lib. on the other hand is expected to be needed later on, and therefore cached. This means that you can't make a reference from or to a temp..
To demonstrate this, we have a nice code sample. I have created two very simple TEXT objects. I will output them under different conditions.
Displaying the difference between temp and lib objects 
- ## Temporary object (uncached)
- temp.myTemp = TEXT
- temp.myTemp.value = This value was set inside temp!
- ## Library object (cached)
- lib.myLib = TEXT
- lib.myLib.value = This value was set inside lib!
- ## Normal copying
- page.10 = COA
- page.10 {
- 10 < temp.myTemp
- 10.wrap = <strong>Temp: </strong>|<br />
- 20 < lib.myLib
- 20.wrap = <strong>Lib: </strong>|<br />
- wrap = <h2>Copying Objects</h2><p>|</p>
- }
- ## Referencing to
- page.20 = COA
- page.20 {
- 10 =< temp.myTemp
- 10.wrap = <strong>Temp: </strong>|<br />
- 20 =< lib.myLib
- 20.wrap = <strong>Lib: </strong>|<br />
- wrap = <h2>Referencing Objects (lib =< temp)</h2><p>|</p>
- }
- ## Referencing another object to temp
- ## This does actually do nothing,
- temp.myTemp =< lib.myLib
- page.30 = COA
- page.30 {
- 10 < temp.myTemp
- 10.wrap = <strong>Temp: </strong>|<br />
- 20 < lib.myLib
- 20.wrap = <strong>Lib: </strong>|<br />
- wrap = <h2>Referencing Objects (temp =< lib)</h2><p>|</p>
- }
## Temporary object (uncached)
temp.myTemp = TEXT
temp.myTemp.value = This value was set inside temp!
## Library object (cached)
lib.myLib = TEXT
lib.myLib.value = This value was set inside lib!
## Normal copying
page.10 = COA
page.10 {
10 < temp.myTemp
10.wrap = <strong>Temp: </strong>|<br />
20 < lib.myLib
20.wrap = <strong>Lib: </strong>|<br />
wrap = <h2>Copying Objects</h2><p>|</p>
}
## Referencing to
page.20 = COA
page.20 {
10 =< temp.myTemp
10.wrap = <strong>Temp: </strong>|<br />
20 =< lib.myLib
20.wrap = <strong>Lib: </strong>|<br />
wrap = <h2>Referencing Objects (lib =< temp)</h2><p>|</p>
}
## Referencing another object to temp
## This does actually do nothing,
temp.myTemp =< lib.myLib
page.30 = COA
page.30 {
10 < temp.myTemp
10.wrap = <strong>Temp: </strong>|<br />
20 < lib.myLib
20.wrap = <strong>Lib: </strong>|<br />
wrap = <h2>Referencing Objects (temp =< lib)</h2><p>|</p>
}
This produces the following output.

You can try this sample out yourself. Simply put this TypoScript code in a template on a blank dummy page.
Conclusion
In almost every case you should use temp.. Unless you need to create an object that can be changed later on. A good example of this is lib.stdHeader. This is a general object, however you should be possible to make changes later on.




TS object browser
lib has another advantage: In opposite to temp it is shown in the TS object browser.
RE: TS object browser
Indeed, did not mention that. The reason for this is again that lib. is cached (and therefore shown inside the TSOB).