Pipes: Dynamic Displays

From Nephtali Documentation

Jump to: navigation, search

Nephtali handles the dynamic display of content within pages using 'pipes', a simple approach that cleanly separates markup from PHP code.

A pipe consists of 2 components:

Markup
The markup for a pipe includes embedded views and delineates the regions (if any) that are to be databound. The markup for the various views is contained within the normal markup for the rest of the page.
PHP
Nephtali provides a simple functional interface to define pipes for a page (or across directories of pages.) The pipe servers as a micro-controller, as it handles domain logic and determines which of the markup views is to be displayed.

Contents

Advantages of pipes

Using pipes to display dynamic regions provides many advantages, including:

  1. One dynamic region can error out without impacting the display of the rest of the page.
  2. Markup for the various pipe views is kept in one spot, the file where all of the other page markup is found.

Pipe example

Below, you can see the code powering the Nephtali homepage's 'Sites using Nephtali' section.

Markup for the Nephtali homepage

In the markup below, notice there's an 'a', 'b', 'empty' or 'error' (set automatically if an exception occurs) view for each of the possible states.

Dynamic data is repeated out in the data region of views 'a' or 'b' for every row available.

			<h2>Sites using Nephtali</h2>
			<ul id="nephtali_sites">
				<!--pipe:sites-->
				<!--view:a-->
				<!--data-->
				<li>
					<h4><a href="{link}">{title}</a></h4>
					<p>{description}</p>
				</li>
				<!--data-->
				<!--view:a-->
				<!--view:b-->
				<!--data-->
				<li>
					<h4 style="background-color:#CCC;"><a href="{link}">{title}</a></h4>
					<p>{description}</p>
				</li>
				<!--data-->
				<!--view:b-->
				<!--view:empty-->
				<li>
					<h4>There are no Nephtali sites</h4>
					<p>What a sad day :(</p>
				</li>
				<!--view:empty-->
				<!--view:error-->
				<li>
					<h4>There has been an error</h4>
					<p>Please try back later</p>
				</li>
				<!--view:error-->
				<!--pipe:sites-->
			</ul>

PHP code for the Nephtali homepage

And, here's the corresponding PHP code that serves as a micro-controller for the possible views within the 'sites' pipe.

// contrived a/b example
n\pipe\register(
	$name = 'sites', 
	$function = function($markup)
	{
		if (count($rows = n\sql\source\query('SELECT * FROM sites')))
		{
			n\watch('rows', $rows);
			return n\view\render($name = (rand(0,1)?('a'):('b')), $markup, $rows);
		}

		return n\view\render($name = 'empty', $markup);
	}
);