intro
This article shows an example of usage of Zend_Filter. It is used for filtering output of variables of view.
1.Create Controller
Create the file
application/controllers/FilterController.php
with the following content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php require_once 'Zend/Controller/Action.php'; require_once 'Zend/Filter.php'; require_once 'Zend/Filter/Input.php'; class FilterController extends Zend_Controller_Action { protected function _getOutputFilter($data) { $chain = new Zend_Filter(); $chain->addFilter(new Zend_Filter_StringToLower()); // add other filters $filters = array( '*' => $chain, 'number' => array(array('PregReplace', '/^1$/', 'ONE!')), 'column' => 'Word_UnderScoreToCamelCase' ); $of = new Zend_Filter_Input($filters, null, $data); return $of; } public function indexAction() { $values = array( 'number' => '1', 'column' => 'my_table_column', 'message' => 'THIS IS <b>TAG</b>' ); $this->view->of = $this->_getOutputFilter($values); $this->view->values = $values; } } |
2.Create View
Create the file
application/views/script/filter/index.phtml
with the following content.
1 2 3 4 5 |
<ul> <?php foreach($this->values as $key => $value) : ?> <li><?= $this->of->$key ?></li> <?php endforeach; ?> </ul> |
3.Result of Execution
Below is the output of view.
1 2 3 |
* ONE! * MyTableColumn * this is <b>tag</b> |