Pulling data from a model/database to display in an included latte file

Member | 1

Hello all,

I have a layout.latte file that is used as a skeleton template with a main area and a sidebar. I have a sidebar.latte file that I wish to include in the layout.latte template. The sidebar.latte will be dynamic and it needs to pull data from a model to display things like total users etc.

Model snippet example

	/** @return Nette\Database\Table\Selection */
	public function totalUsers()
		$totalUsers = $this->getUsers()->count('*');
		return $totalUsers;

layout.latte snippet example

	<main class="main">
			{include content}
	<aside class="sidebar">
		{include "Sidebar/sidebar.latte"}

Now I think I might be having a brain fart, because I can't work out how to get the data from the model into the included sidebar.latte.

I tried with a presenter, but the presenter doesn't seem to do anything as the sidebar.latte is included and not viewed directly via a route etc.

So I would appreciate any advice. Can I call totalUsers() directly from within the sidebar.latte file? If not, how would I best go about things?


Rick Strafy
Nette Blogger | 61

The same way as you got variables to your layout.latte, probably some sort of BasePresenter and in beforeRender() method you can assign $this->template->something to your data and in template use $something.

More suitable way for that sidebar is to have it like component https://doc.nette.org/…n/components.