1. Home
  2. Blog
  3. Contact
Blogitems
16Sep

TypoScript: Temp vs. Lib

Posted on September 16th 2010 at 22:10 inside TypoScript with 2 comments

When 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 

  1. ## Temporary object (uncached)
  2. temp.myTemp = TEXT
  3. temp.myTemp.value = This value was set inside temp!
  4.  
  5. ## Library object (cached)
  6. lib.myLib = TEXT
  7. lib.myLib.value = This value was set inside lib!
  8.  
  9. ## Normal copying
  10. page.10 = COA
  11. page.10 {
  12.   10 < temp.myTemp
  13.   10.wrap = <strong>Temp: </strong>|<br />
  14.   20 < lib.myLib
  15.   20.wrap = <strong>Lib: </strong>|<br />
  16.   wrap = <h2>Copying Objects</h2><p>|</p>
  17. }
  18.  
  19. ## Referencing to
  20. page.20 = COA
  21. page.20 {
  22.   10 =< temp.myTemp
  23.   10.wrap = <strong>Temp: </strong>|<br />
  24.   20 =< lib.myLib
  25.   20.wrap = <strong>Lib: </strong>|<br />
  26.   wrap = <h2>Referencing Objects (lib =&lt; temp)</h2><p>|</p>
  27. }
  28.  
  29. ## Referencing another object to temp
  30. ## This does actually do nothing,
  31. temp.myTemp =< lib.myLib
  32. page.30 = COA
  33. page.30 {
  34.   10 < temp.myTemp
  35.   10.wrap = <strong>Temp: </strong>|<br />
  36.   20 < lib.myLib
  37.   20.wrap = <strong>Lib: </strong>|<br />
  38.   wrap = <h2>Referencing Objects (temp =&lt; lib)</h2><p>|</p>
  39. } 
## 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 =&lt; 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 =&lt; 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.

Tags

typo3 typoscript temp lib cache

Comments (2)

Gravatar: SteffenSteffen2010-09-27 09:05

TS object browser

lib has another advantage: In opposite to temp it is shown in the TS object browser.

Gravatar: Sebastiaan de JongeSebastiaan de Jonge2010-10-07 09:35

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).

Your comment