There is one extremely useful and not-so widely known toReference() method. It saves actually matched elements inside variable, by reference (as the name says). It can be used to target such cases as:
1. Preserving chain
// declare our variables (this is VERY important)
$deepClass = $section = null;
$template['div.main div.otherclass .deep-class:first']
->toReference($deepClass)
// ->... // do something
->find('.section')
->toReference($section)
// ->... // do something
->end()
->next()
// highlighter but; only one empty()
->empty()
->append($deepClass->contents())
->eq(0)->add($section)->eq(1)
// ->... // do something, stack is [$section]
->end()->end()->end()
// ->... // stack is same as after ->next()
;
2. Splitting chains
// declare our variables (this is VERY important)
$row = $titleBody = null;
$template
->find('ul:first > li')
->loopOne('posts', 'postNum', 'r')
// add dynamic class
->addClassPHP('if (! $postNum) print "first"')
->find('> .title, > .body')
->varsToStack('r["Post"]', $postFields)
// save our $titleBody variable
->toReference($titleBody)
->end()
// save our $row variable
->toReference($row)
// ->... // lots of code ;)
;
// just continue your work
$row->find('h3:first, .comments')
// ->...
;
// anywhere...
doSomethingOnFields($titleBody);
In SVN version of QueryTemplates there is a fix for cache issue. The workaround for beta2 is to do “if ($ref)” before, or turning off the cache with “$cacheTimeout = -1″.