/** * @covers zenCode::render * @todo Implement testRender(). */ public function testRender() { //A basic usage $actual = zenCode::render('div>ul>li>p["text"]+p["text two"]'); $need = '<div><ul><li><p> text</p><p> text two</p></li></ul></div>'; $this->assertEquals($need, $actual); //You can pass an array of data and refence with {}. The example also show the enumerator with 3 $var = array('hello' => 'ni hao', 'test' => array('hello', 'world')); $actual = zenCode::render('div>ul>li[{hello}]+li["{test[$]}"]*2', $var); $need = "<div><ul><li> ni hao</li><li> hello</li><li> world</li></ul></div>"; $this->assertEquals($need, $actual); //With user functions referenced in an array. $actual = zenCode::render('a#test.thisclass.otherclass[ref="{test[0]}"]["content text "]>ul>li#$[href="{test[$]}link"]*2', $var); $need = '<a id="test" class="thisclass otherclass" ref="hello"> content text <ul><li id="0" href="hellolink"></li><li id="1" href="worldlink"></li></ul></a>'; $this->assertEquals($need, $actual); // with function calls $var = array('a_function1' => 'myfunc1', 'a_function2' => 'myfunc2'); $actual = zenCode::render('div["{a_function1("test")}+div["{a_function2($)}"]*3', $var); $need = "<div> Test</div><div> This 0</div><div> This 1</div><div> This 2</div>"; $this->assertEquals($need, $actual); //Inline Code. Inline code is wrapped with {= } $var = array('test' => array('hello', 'world')); $actual = zenCode::render('div["{= return $test[0] . " - ". $test[1]; }"]', $var); $need = "<div> hello - world</div>"; $this->assertEquals($need, $actual); $actual = zenCode::render('div["{= $x = $test[0]; $y = $test[1]; return $x . " - " . $y; }"]', $var); $need = "<div> hello - world</div>"; $this->assertEquals($need, $actual); }
/** * * @param type $code * @param type $var * @return type */ function render($code, $var = array()) { $zen = new zenCode(); $code = explode('>', $code); $domarray = array(); foreach ($code as $item) { $sub = explode('+', $item); foreach ($sub as $key => $s) { $domarray[] = array('type' => $key ? '+' : '>', 'data' => $s); } } $html = $zen->_parse($domarray, 0, $var); return trim($html); }