Using Pipes - serverboy/Interchange GitHub Wiki
Pipes are a simpler version of libraries; they allow the modularization of code on a function-by-function level (rather than on a module-by-module or class-by-class level). Using the pipes
class, data can be passed to a function stored in a PHP file. The pipe files are loaded on demand, meaning there is less overhead.
The simplest pipe command is pipe::through
. This function accepts two parameters.
pipe::through($data, $through);
$data
The first parameter is the data you wish to pipe.
$through
The second parameter is the name (or list of names) of the pipe(s) that you wish to use. If one pipe is to be used, simply passing a string for this parameter is sufficient. The name should not include a “.php” extension. If multiple pipes are to be used, an array of strings should be passed. In the case of multiple pipes, the value of $data
is piped sequentially through each pipe listed in the array. Note that the order of the array is obeyed.
Interchange comes bundled with Textile and Markdown. Both of these engines perform largely the same function as the other: to format text based on a simple markup language.
Another script is also bundled: it is known as SmartyPants and is useful for replacing double quotes with curly quotes and double/triple hyphens with the appropriate dash character.
All three of these scripts are accessible by means of pipe::through
. To take advantage of this, you would use code similar to the following:
<?php echo pipe::through(view_manager::get_value("PROFILE_DATA"), "textile"); // or echo pipe::through(view_manager::get_value("PROFILE_DATA"), "markdown"); // or echo pipe::through(view_manager::get_value("PROFILE_DATA"), "smartypants"); // or if you were feeling wild and crazy echo pipe::through(view_manager::get_value("PROFILE_DATA"), array("markdown", "smartypants"));
The engines are made available as simple pipes under the names “textile”, “markdown”, and “smartypants”.
Pipes are simple to implement. Simply create a file in the /pipes/
directory that contains the following basic function structure:
function execute_foo($data) { return $data; }
The above pipe would be available under the name “foo”. Its file in /pipes/
would be named foo.php
.
In the above example, the data is simply being returned unchanged. Modify your pipe to change or modify the $data
value in some useful way. As a useful reference, check out the test pipe