function _makeMarkdown() { $template = (include __DIR__ . '/templates.php'); $catTemplate = _::template($template['markdown']['category']); $funcTemplate = _::template($template['markdown']['function']); $content = ""; $methodsByCategory = _::groupBy(_getMethods(), function ($method) { return $method->category; }); $formatMenuEntry = function ($name) { return _anchor($name); }; foreach ($methodsByCategory as $category => $methods) { $categoryContent = ""; foreach ($methods as $function) { $categoryContent .= $funcTemplate(compact('function')); } $content .= $catTemplate(['category' => $category, 'content' => $categoryContent, 'functions' => _::chain($methods)->pluck('name')->map($formatMenuEntry)->value()]); } $installation = ""; $usage = ""; $menu = _::template($template['markdown']['menu'], ['categories' => _::map($methodsByCategory, function ($methods) { return _::pluck($methods, 'name'); })]); return _::template($template['markdown']['main'], compact('menu', 'content')); }
function _template($templateString, $data = [], $settings = []) { return Underscore::template($templateString, $data, $settings); }
/** * @tags utilities */ public function testTemplate() { // interpolate $this->string(_::template('<%=$a%>', ['a' => "hello"]))->isEqualTo('hello'); // escape $this->string(_::template('<%-$b%>', ['b' => "<tag>hello</tag>"]))->isEqualTo('<tag>hello</tag>'); // evaluate $this->string(_::template('<% echo $c %>', ['c' => "hello"]))->isEqualTo('hello'); // mix $this->string(_::template('<% for($i=1; $i<=$d; $i++): %>i<%=$i%><% endfor %>', ['d' => 3]))->isEqualTo('i1i2i3'); // function $fn = _::template('<%=$a%>'); $this->string($fn(['a' => 1]))->isEqualTo('1'); $this->string($fn(['a' => 2]))->isEqualTo('2'); $this->string($fn(['a' => 3]))->isEqualTo('3'); }