!Friendica Developers

Okay, I'm about ready to give up. How am I supposed to use the "template_vars" hook?

It says:

template_vars

Called before vars are passed to the template engine to render the page. The registered function can add, change or remove variables passed to template. $b is an array with:

template: filename of template
vars: array of vars passed to the template


In the theme I'm working on I need to append some new page variables for my "wall_thread.tpl" template, which I gather is used by the "threaded_conversation.tpl."

It seems like everything in the hook is being looped? So in my hook function I'm first checking:

if( $b['template'] == 'threaded_conversation.tpl'){...}

Because the vars for other templates don't have the $b['vars']['$threads'] sub-key which seems to hold the actual template variables defined in /src/Object/Post.php so you'd think I'd just need to do something like:

$b['vars']['$threads']['new_var'] = $something;

But that doesn't seem to work. So how am I supposed to add variables to be passed to my template?

wiki.friendi.ca/docs/addons#te…

reshared this

in reply to Random Penguin

@Random Penguin Here's a sample hook function I successfully used to modify the template variables value using the template_vars hook:
function template_vars(array &$b) {
	var_dump($b);
	if ($b['template'] == 'nav.tpl') {
		$b['vars']['$emptynotifications'] = 'These are not the notifications you are looking for.';
	}
}

Notice the ampersand & before the parameter name, this is crucial for modifying the hook data before it is sent to the final template.
in reply to Hypolite Petovan

Yes, I was passing $b by reference with &. What I was missing was the "array" keyword before it, and after dumping the variable misunderstood where I was supposed to append it.

As I said, I thought it needed to be in the $b['vars']['$thread'] where I saw the rest of the variables from /src/Object/Post.php and then reference it in my template the same way with $item.myvar.

Now I get it. I just append it to $b['vars'] and reference it directly in my template like {{$myvar}}.

Thank you @Hypolite Petovan now I've got it working in my theme!