Outputs: Simple Display Values
From Nephtali Documentation
Contents |
Overview
Although Nephtali's pipes can be used for all types of display, sometimes a pipe can be overkill if you want to output a simple value somewhere on the page. For these occasions, Nephtali provides the ability to register outputs. Once registered, the value of the output is databound to the page after all of the existing pipes have been databound. The output is automatically encoded to avoid security issues.
An example of a typical use of an output is modifying the title of the page, as in the example below.
Example
The example below comes from the search results page for the Nephtali website. The search term(s) is registered as an output so it can be added to the search box within the main content region of the page and the title of the page.
Markup
The markup below is found within the title of the page:
<title>Search results for "{q}" at Nephtali: A PHP web framework that functions beautifully.</title>
Additionally, here is the markup for the search field within the main content region of the page:
<form action="index.php" method="get" id="search">
<label>Search again: <input name="q" id="q" type="text" size="30" value="{q}" /></label> <input type="submit" value="GO" id="search_submit" />
</form>
PHP
The code below, in addition to retrieving the search results, registers the output 'q', which will databind any number of occurrences of '{q}' to the value:
require('nlib/google.php');
n\port\register(
$name = 'q',
$value = $_GET['q'],
$opts = array(
'min_length' => 1,
'max_length' => 50,
'filter' => n\constant\FILTER_TEXT,
'validators' => array(
'OOP searches at this website are prohibited.' => function($val){if (strpos(strtolower($val), 'oop') !== false) return false; return true;}
),
'formatter' => function($val){return strtolower($val);}
)
);
n\pipe\register(
$name = 'search',
$function = function($markup)
{
if (count($rows = n\port\validate(array('q'))))
{
n\output\register('q', '[invalid]');
return n\view\render($view = 'feedback', $markup, $rows);
}
$q = n\port\get('q');
// register search term as value
n\output\register('q', $q);
if (count($rows = nlib\google\search_site($site_url = 'nephtaliproject.com', $query = $q)))
{
return n\view\render(
$view = 'default',
$markup,
$rows,
$opts = array(
'whitelists' => array(
'content' => array('<b>', '</b>', ''', '<', '>', '"')
)
)
);
}
return n\view\render($view = 'empty', $markup);
}
);

