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()); }
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 testPlugInIncludedFileUsesParentViewOption() { // New behaviour for plugs: rendered in local context. $view = new View(); $view->loadFile(__DIR__ . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'slot-in-parent.xml'); $output = trim($view->render()); $this->assertEquals(11, $output); // Legacy behaviour: rendered in final global context // What we are trying to test is: the plug is defined in an included template, and yet its rendering process // checks properly that the top view has the GLOBAL_PLUGS defined. $view = new View([View::GLOBAL_PLUGS]); $view->loadFile(__DIR__ . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'slot-in-parent.xml'); $output = trim($view->render()); $this->assertEquals(12, $output); }
/** * @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 testRangeFunc() { $source = <<<ENDXML <li fig:walk="range(5)" fig:text="."></li> ENDXML; $this->view->loadString($source); $this->assertEquals('<li>1</li><li>2</li><li>3</li><li>4</li><li>5</li>', $this->view->render()); }
public function testScriptTagWithoutContentInIncludedFile() { vfsStream::setup('root'); $template = <<<TEMPLATE <fig:template xmlns:fig="http://www.figdice.org"> <link href="/assets/style.css" rel="stylesheet" /> <fig:include file="inner.html" /> </fig:template> TEMPLATE; vfsStream::newFile('outer.html')->withContent($template)->at(vfsStreamWrapper::getRoot()); $template = <<<TEMPLATE <fig:template> <script src="/assets/require.js"></script> </fig:template> TEMPLATE; $innerVFile = vfsStream::newFile('inner.html'); $innerVFile->withContent($template)->at(vfsStreamWrapper::getRoot()); $filename = vfsStream::url('root/outer.html'); $view = new View(); $view->loadFile($filename); $output = $view->render(); $expected = <<<EXPECTED <link href="/assets/style.css" rel="stylesheet" /> <script src="/assets/require.js"></script> EXPECTED; $this->assertEquals(trim($expected), trim($output)); // Now test by inverting the script and link, so that the script is no longer // the last tag in the template (Bolek's test) $template = <<<TEMPLATE <fig:template> <script src="/assets/require.js"></script> <link href="/assets/style.css" rel="stylesheet" /> </fig:template> TEMPLATE; $innerVFile->setContent($template); $expected = <<<EXPECTED <link href="/assets/style.css" rel="stylesheet" /> <script src="/assets/require.js"></script> <link href="/assets/style.css" rel="stylesheet" /> EXPECTED; $view = new View(); $view->loadFile($filename); $output = $view->render(); $this->assertEquals(trim($expected), trim($output)); }
public function testFunctionXmlWithRootUsesSpecified() { $xml = <<<TEMPLATE <fig:template> <fig:mount target="myXml"> <myroot> <node1>value1</node1> <node2>value2</node2> </myroot> </fig:mount> <fig:mute fig:text="xpath(xml(/myXml), '/myroot/node1')"/> </fig:template> TEMPLATE; $view = new View(); $view->loadString($xml); $output = trim($view->render()); $this->assertEquals('value1', $output); }
/** * Creates a sub-view object, invokes its parsing phase, * and renders it as the child of the current tag. * @return string or false */ private function fig_include() { //Extract from the attributes the file to include. if (!$this->hasAttribute('file')) { throw new RequiredAttributeException($this->name, $this->getCurrentFilename(), $this->getLineNumber(), 'Missing required attribute: "file" in tag: "' . $this->name . '"' . ' in file: ' . $this->getCurrentFilename() . '(' . $this->getLineNumber() . ')'); } $file = $this->attributes['file']; //Create a sub-view, attached to the current element. $view = new View(); $view->inherit($this); $view->loadFile(dirname($this->getCurrentFilename()) . '/' . $file, $this->getCurrentFile()); //Make the current node aware that it is being rendered //as an include directive (therefore, it will be skipped //when the subview tries to render it). $this->bRendering = true; //Parse the subview (build its own tree). $view->parse(); $result = $view->render(); unset($this->bRendering); return $result; }
* @version 2.0.4 * @package FigDice * * This file is part of FigDice. * * FigDice is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * 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: * * - parse and use pieces of XML data. */ // Autoload the Figdice lib require_once '../../vendor/autoload.php'; use figdice\View; $view = new View(); // Well, everything is explained in the template.xml... Go see! $view->loadFile('template.xml'); $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()); }
/** * @expectedException \figdice\exceptions\DictionaryEntryNotFoundException */ public function testAnonTransWithoutLoadedDicRaisesError() { $view = new View(); $str = <<<ENDTEMPLATE <fig:template> <fig:trans key="somekey"/> </fig:template> ENDTEMPLATE; $view->loadString($str); $view->setLanguage('en'); vfsStream::setup('root'); $view->setTranslationPath(vfsStream::url('root')); $view->render(); }
* 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 feeds * - understand the inversion of control for data */ // Autoload the Figdice lib require_once '../../vendor/autoload.php'; use figdice\View; $view = new View(); $view->loadFile('template.xml'); // In this exercise, we are not going to mount any data // directly into the View's universe. Rather, we will make // a Feed subclass, and we let the View know that it exists // (via a FeedFactory), and we will let the Template itself // decide whether to activate the feed or not (and which Feeds it needs // for this specific page). // Our Feed is declared in PrimeNumbersFeed.php // but we must let our View know about it: require_once 'Example4FeedFactory.php'; $view->registerFeedFactory(new Example4FeedFactory()); // Render the template! // When the engine will hit the fig:feed tag, it will search our // registered Feed Factories for the first one capable of instantiating // the requested Feed, and invoke it in order to obtain the data.
*/ /** * In this example we will learn to: * * - load a template by its inner, meaningful part * - declare slots and plug content into them * - include sub-templates * - 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);
* 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;
* 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 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:
public function testVfsIncludeParentDir() { vfsStream::setup('root'); $template = <<<ENDXML <xml> <fig:include file="../someFile.xml" /> </xml> ENDXML; vfsStream::newFile('template.xml')->at(vfsStream::newDirectory('subDir')->at(vfsStreamWrapper::getRoot()))->withContent($template); $template = <<<ENDXML <included> Hello </included> ENDXML; vfsStream::newFile('someFile.xml')->at(vfsStreamWrapper::getRoot())->withContent($template); $filename = vfsStream::url('root/subDir/template.xml'); $view = new View(); $view->loadFile($filename); $output = $view->render(); $expected = <<<ENDHTML <xml> <included> Hello </included> </xml> ENDHTML; $this->assertEquals($expected, $output); }
/** * Returns the data structure * behind the specified name. * Looks first in the local variables, * then in the data context of the element. * * @param string $name * @return mixed */ public function getData($name) { //Treat plain names return $this->view->fetchData($name); }
public function testDoctypeOnNonRootNodeReplacesExisting() { $view = new View(); $templateSource = <<<ENDXML <fig:template> <html fig:doctype="html"> <head fig:doctype="dummy"></head> </html> </fig:template> ENDXML; $view->loadString($templateSource); $expected = <<<EXPECTED <!doctype dummy> <html> <head></head> </html> EXPECTED; $rendered = $view->render(); $this->assertEquals($expected, $rendered); }
* 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 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); }
/** * @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 testXmlnsFigIsNotRendered() { $template = <<<ENDTEMPLATE <html xmlns:fig="http://figdice.org/"> </html> ENDTEMPLATE; $view = new View(); $view->loadString($template); $actual = $view->render(); $expected = "<html>\n</html>"; $this->assertEquals($expected, $actual); }