Snippets: passing a variable by reference

Hi, I wonder if is possibile to pass a variable to a snippet by reference. Something like:

snippet('article-snippet', array('playlist' => &$playlist));


Äh, why? You pass a variable to a function by reference to be able to modify the variable…

I need to pass $playlist from the main page to a snippet; the snippet should append more values to $playlist and then passing it back to the main page.


$playlist = $pages;
snippet('test', ['playlist' => $playlist]) ?>
<?php dump($playlist) ?>


  $playlist = $playlist->add(page('somepage'));
  return $playlist;

Thanks. But it doesn’t work for me. Anyway, I think it’s better (and cleaner) to store the values some other way, sessions for example…

Hm, strange, I tested this in a Starterkit. But doesn’t seem to me to be the best way to go about this, anyway.

I’m interested in this approach. It doesn’t work for me either though.

I wonder, if the snippet returns something, shouldn’t the template somehow catch it in a variable?

There’s a typo, should be

<?php snippet('test', ['playlist' => $playlist]) ?>

For me it still works.

For me it doesn’t. And it’s nothing to do with your typo - I was testing with my own variables. I just get the same old content.

Maybe it only works with that example, I doesn’t seem to work with an array. Why don’t you store the variable in the session?

I was testing with strings anyway.

Yes, I could use the session I guess, but why use the session for something that I don’t need throughout the session - only during one page load. Isn’t there a more elegant way?

You can also store it in a cookie, or use a global variable…

global $array;
$array = ['a', 'b', 'c'];

Snippet test.php

global $array;
array_splice($array, 2);


Idea: with a global variable, I can use it only within the custom method to resource to a stored array in case the method has already been called before. Then in my snippets/templates I can call the function as many times as I want and it will only shuffle the array the first time.

public function getHints()

	if( !isset( $hints ) ) {

		global $hints = [ 'a', 'b', 'c' ]
		shuffle( $hints );


	return $hints;


Could work? Will test it…

@pedroborges’s solution is even more elegant: