Nette Documentation Preview

Single-page microframework

This short example will show you how to use Nette & Latte in a single-page website.

First, create project:

mkdir microframework
cd microframework
composer require "nette/nette"
mkdir temp
mkdir log
mkdir data
chmod o+w {temp,log,data}

Second, create index.php:

?php
if (empty($template)) {
	// autoload.php @generated by Composer
	require_once __DIR__ . '/vendor/' . '/autoload.php';

	// create configurator
	$configurator = new Nette\Configurator;
	// Enable Nette Debugger for error visualisation & logging
	$configurator->enableDebugger(__DIR__ . '/log');
	$configurator->setTempDirectory(__DIR__ . '/temp');
	// Create Dependency Injection container from config.neon file
	$container = $configurator->createContainer();

	// set arbitrary data
	$connection= new Nette\Database\Connection('sqlite:data/ps.sdb');
	$context = new Nette\Database\Context(
		$connection,
		new Nette\Database\Structure($connection, $container->getService('cache.storage'))
	);
	$params['db'] = $context;

	// render
	$latte = $container->getService('latte.latteFactory')->create();
	$latte->setLoader(new Latte\Loaders\StringLoader());
	$latte->render(file_get_contents(__FILE__), $params);
	exit;
}

?>
<!DOCTYPE html>
<html>
<head>
	<title>Welcome</title>
</head>

<body>
	<div n:foreach="$db->table(events)->order(date) as $event">
		<h4>{$event->name}</h4>
		<p>{$event->date|date:"j. n. Y"}</p>
	</div>
	...

</body>
</html>