public function testRelativePathDisambiguation() { //Check that heading dot is understated. $view = new View(); $view->loadString(trim('<fig:template>' . '<fig:mute fig:text="data"/>:' . '<fig:mute fig:walk="lines" fig:text="data"/>:' . '<fig:mute fig:walk="lines" fig:text="./data"/>' . '</fig:template>')); $view->mount('data', 12); $view->mount('lines', [['data' => 13], ['data' => 14]]); $this->assertEquals('12:1314:1314', $view->render()); }
public function testHtmlEntitiesBuiltin() { $view = new View(); $view->loadString('<test><span fig:text="htmlentities(/data)"/></test>'); $view->mount('data', '<TAG>'); $this->assertEquals('<test><span><TAG></span></test>', $view->render()); }
/** * @param ResponseInterface $response * @param array $data */ private function renderWithData(ResponseInterface $response, array $data = array()) { foreach ($data as $key => $value) { $this->view->mount($key, $value); } $response->getBody()->write($this->view->render()); }
public function testKeyFunc() { $this->view->mount('data', array('a' => 10, 'b' => 20, 'c' => 30)); $source = <<<ENDXML <fig:x fig:walk="/data"><fig:y fig:text="key()"/><fig:y fig:text="."/></fig:x> ENDXML; $this->view->loadString($source); $this->assertEquals('a10b20c30', $this->view->render()); }
/** * @expectedException \figdice\exceptions\RenderingException */ public function testAttributeEvalsToArrayException() { $source = '<xml attr="{myArray}"></xml>'; $view = new View(); $view->loadString($source); $view->mount('myArray', array(4, 5, 6)); $this->assertEquals('dummy', $view->render()); }
public function testNestedLoops() { $template = <<<ENDTEMPLATE <fig:mute fig:walk="/outer"> <fig:mute fig:walk="inner"> <fig:mute fig:text="../page"/>-<fig:mute fig:text="x"/> </fig:mute> </fig:mute> ENDTEMPLATE; $data = []; for ($i = 1; $i <= 5; ++$i) { $inner = []; for ($j = 0; $j < 3; ++$j) { $inner[] = ['x' => $j]; } $data[] = ['page' => 10 * $i, 'inner' => $inner]; } $view = new View(); $view->loadString($template); $view->mount('outer', $data); $result = preg_replace('# +#', ' ', str_replace("\n", ' ', trim($view->render()))); $this->assertEquals('10-0 10-1 10-2 20-0 20-1 20-2 30-0 30-1 30-2 40-0 40-1 40-2 50-0 50-1 50-2', $result); }
* but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with FigDice. If not, see <http://www.gnu.org/licenses/>. */ /** * In this example we will learn to: * * - work with macros */ // Autoload the Figdice lib require_once '../../vendor/autoload.php'; use figdice\View; $view = new View(); // The template we are studying now, uses the Macro tool. // A Macro is similar to a Function in PHP: it has a name, // and accepts parameter. // However it does not "return" a value: rather, it produces // in-place output, at the location where it is invoked. $view->loadFile('template.xml'); // We have learned, in example 4, how to use Feeds as data providers // for our View. In this example we will get back to mounting direct // data from our Controller into our View, for the sake of simplicity. // But you know now that it is not the smartest way to go, in the // FigDice paradigm. // So let's mount some structured data. $view->mount('countries', array('France' => array('capital' => 'Paris', 'wiki' => 'http://en.wikipedia.org/wiki/France'), 'Germany' => array('capital' => 'Berlin', 'wiki' => 'http://en.wikipedia.org/wiki/Germany'))); $output = $view->render(); echo $output;
public function testWalkOnNonCountableObjectRunsOnArrayWithObject() { $view = new View(); $view->loadString('<test fig:walk="/obj"><fig:mute fig:text="property" /></test>'); $obj = new stdClass(); $obj->property = 12; $view->mount('obj', $obj); $this->assertEquals('<test>12</test>', $view->render()); }
* - play with conditions * - play with complex attributes */ // Autoload the Figdice lib require_once '../../vendor/autoload.php'; use figdice\View; use figdice\exceptions\FileNotFoundException; // Create a Fig View object $view = new View(); // This time we do not load the outer page: // rather, our template's "entry-point" is going to be an inner block. // The inner block different on every URL of our site, but it sits at a // specific location on the generic page, which remains (almost) identical // on every URL. // The inner template is responsible for loading its container. try { $view->loadFile('template-inner.xml'); } catch (FileNotFoundException $ex) { die('template file not found'); } // Mount some data into our View // You can play with this true/false value and re-run the example, // to see the difference in output. $view->mount('isLogged', false); // Render the template! try { $output = $view->render(); } catch (FileNotFoundException $ex) { die('some include went wrong at rendering-time: ' . PHP_EOL . $ex->getMessage()); } echo $output;
* any later version. * * FigDice is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with FigDice. If not, see <http://www.gnu.org/licenses/>. */ /** * In this example we will learn to: * * - work with loops * - understand the notion of Context * - use the dot symbol, representing the current item. */ // Autoload the Figdice lib require_once '../../vendor/autoload.php'; use figdice\View; $view = new View(); $view->loadFile('template.xml'); // Mount an plain, one-dimension indexed array into 'indexed' key. // Each item is an object (or an assoc. array) with // "name" and "link" property. $view->mount('indexed', array(array('name' => 'A', 'link' => 'page_1.html'), array('name' => 'B', 'link' => 'page_2.html'), array('name' => 'C', 'link' => 'page_3.html'))); // and a nested structure in the 'nested' key $view->mount('nested', array(array('name' => 'X', 'values' => array(11, 14, 17)), array('name' => 'Y', 'values' => array(2, 4, 6)))); // Render the template! $output = $view->render(); echo $output;
* You should have received a copy of the GNU General Public License * along with FigDice. If not, see <http://www.gnu.org/licenses/>. */ /** * In this example we will learn to: * * - work with dictionaries * - provide dynamic params for named placeholders in entries */ // Autoload the Figdice lib require_once '../../vendor/autoload.php'; use figdice\View; $view = new View(); // The template we are loading now, imports a Dictionary. // Dictionaries are XML files containing entries of key/value pairs. $view->loadFile('template.xml'); // Let the View know where our Dictionaries are stored. // For the sake of the example, we keep our dictionaries locally, in // this same directory. // The Dictionaries of all the languages must be stored below one same // parent folder. This is this parent folder which you specify here. $view->setTranslationPath(dirname(__FILE__) . '/dictionaries'); // Each provided language must exist in the shape of one sub-folder below // the Translation Path: the folder names correspond to the target language // in which you wish to render your view. Therefore, below the /dictionaries // parent, there is a "fr" folder, in which the French dictionaries are found. $view->setLanguage('fr'); // Let's give some value to the number of available brown shoes: $view->mount('stock', array('shoes' => array('brown' => array('Smith 40', 'Weston 43', 'Finkers 43')))); $output = $view->render(); echo $output;
public function testAdHocEval() { $view = new View(); $view->loadString('<xml attr="some {adhoc} here"></xml>'); $view->mount('adhoc', 'test'); $this->assertEquals('<xml attr="some test here"></xml>', $view->render()); }