protected function templateToEach(Token $token) { $children = $token->getChildren(); $has_items = false; foreach ($children as $child) { if ($child->name == 'item') { $has_items = true; break; } } if (!$has_items) { return; } $with_each_token = new Token('with_each', 'double', array('select' => '$.items')); $with_each_token->setChildren($children); $token->clearChildren(); $token->addChild($with_each_token); }