Ante Programmer's cookbook

This is a collection of PHP code to show how to set up for the Ante templates in the User Guide.

The Ante Solution

View the template

This is a simple example, so simple code will suffice.

Assuming that the template is in the file antesol.html, the following code will produce:

		<?php
		require_once ANTEPATH.'Ante.inc';
		$datasource = new AwArrayDataSource( array(
			'title' => 'My title',
			'body' => 'My Body'
		));
		Ante::render('antesol.html', $datasource);
		?>
		

This will produce:

		<html>
		<head>
		<title class="aw_title">My title</title>
		</head>
		<body class="aw_body">My Body</body>
		</html>
		

Conditionals

View the template

This will require some more interesting code.

		<?php
		require_once ANTEPATH.'Ante.inc';
		$loggedin = !empty($_SESSION['loginname']);
		if ($loggedin){
			$datasource = new AwArrayDataSource( array(
				'body' => 'This will become the body text for people who have logged in.'
			));
		} else {
			$datasource = new AwArrayDataSource( array(
				'notloggedin' => 'not logged in',
				'body' => 'This will become the body text for people who have not logged in.'
			));
		}
		Ante::render('conditions.html', $datasource);
		?>
		

This time we're using the PHP superglobal $_SESSION, and assume that if $_SESSION['loginname'] is not empty that the person is logged in. Then we create a datasource, depending on whether the person is logged in. If 'notloggedin' is not set, is null or false the contents of the div with class aw_notloggedin will not show.

Special Classes

View the template

Again we're going to use a simple AwArrayDataSource.

		<?php
		require_once ANTEPATH.'Ante.inc';
		$datasource = new AwArrayDataSource( array(
			'name' => 'Joan',
			'gender' => 'female'
		));
		Ante::render('special.html', $datasource);
		?>
		

Namespaces

View the template

Array Data Source makes doing namespaces easy.

		<?php
		require_once ANTEPATH.'Ante.inc';
		$datasource = new AwArrayDataSource( array(
			'name' => 'Joan',
			'boss' => array(
				'name' => 'Bob'
			)
		));
		Ante::render('names.html', $datasource);
		?>
		

Loops

View the template

It also makes loops easy.

		<?php
		require_once ANTEPATH.'Ante.inc';
		$datasource = new AwArrayDataSource( array(
			'name' => 'Dan',
			'employee' => array(
				'hidden', /* this is the 0, Ante arrays are 1 based */
				array( 'name' => 'David'),
				array( 'name' => 'Albert'),
				array( 'name' => 'Joanne')
			)
		));
		Ante::render('loops.html', $datasource);
		?>
		

And simpler loops are even easier.

		<?php
		require_once ANTEPATH.'Ante.inc';
		$datasource = new AwArrayDataSource( array(
			'fruit' => array(
				'hidden',
				'Oranges',
				'Grapefruit',
				'Lime',
				'Lemon
			)
		));
		Ante::render('loops2.html', $datasource);
		?>
		

Included Tempaltes

View the template

If your team decides for included templates such as used in the first sample bit in the userguide on Included Tempaltes, what you do is tell Ante to include both templates.

		<?php
		require_once ANTEPATH.'Ante.inc';
		$datasource = new AwArrayDataSource( array(
			'title' => 'My title',
			'body' => 'My Body'
		));
		Ante::render(array('inc1.html', 'inctemplate.html'), $datasource);
		?>
		

This code assumes that there's a database table comments with columns: title, username, timestamp, and text.

			<?php
			require_once '../../adodb5/adodb.inc.php';
			require_once '../Ante.inc';
			$adb = ADONewConnection($connectionstring);
			$sql = 'select * from comments order by cid desc';
			$res = $adb->Execute($sql);
			$datasource = new AwArrayDataSource($res->fields );
			Ante::render('comments.html', $datasource);
			?>