Example #1
0
 /**
  * @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());
 }
Example #2
0
 public function testHtmlEntitiesBuiltin()
 {
     $view = new View();
     $view->loadString('<test><span fig:text="htmlentities(/data)"/></test>');
     $view->mount('data', '<TAG>');
     $this->assertEquals('<test><span>&lt;TAG&gt;</span></test>', $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());
    }
Example #4
0
 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());
 }
Example #5
0
 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);
 }
Example #6
0
    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));
    }
Example #7
0
    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);
    }
Example #8
0
 /**
  * @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());
 }
Example #9
0
    /**
     * @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();
    }
Example #10
0
 /**
  * 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;
 }
Example #11
0
 * @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;
Example #12
0
    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);
    }
Example #13
0
    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);
    }
Example #14
0
    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);
    }
Example #15
0
 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());
 }
Example #16
0
    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);
    }