/** * Renders a CMS object as file content. * @return string */ public static function render($data, $options = []) { extract(array_merge(['wrapCodeInPhpTags' => true, 'isCompoundObject' => true], $options)); if (!$isCompoundObject) { return array_get($data, 'content'); } $iniParser = new Ini(); $code = trim(array_get($data, 'code')); $markup = trim(array_get($data, 'markup')); $trim = function (&$values) use(&$trim) { foreach ($values as &$value) { if (!is_array($value)) { $value = trim($value); } else { $trim($value); } } }; $settings = array_get($data, 'settings', []); $trim($settings); /* * Build content */ $content = []; if ($settings) { $content[] = $iniParser->render($settings); } if ($code) { if ($wrapCodeInPhpTags) { $code = preg_replace('/^\\<\\?php/', '', $code); $code = preg_replace('/^\\<\\?/', '', $code); $code = preg_replace('/\\?>$/', '', $code); $code = trim($code, PHP_EOL); $content[] = '<?php' . PHP_EOL . $code . PHP_EOL . '?>'; } else { $content[] = $code; } } $content[] = $markup; $content = trim(implode(PHP_EOL . self::SECTION_SEPARATOR . PHP_EOL, $content)); return $content; }
public function testRender() { $parser = new IniParser(); $data = ['var1' => 'value 1', 'var2' => 'value 21']; $path = __DIR__ . '/../fixtures/parse/simple.ini'; $this->assertFileExists($path); $str = $parser->render($data); $this->assertNotEmpty($str); $this->assertEquals($this->getContents($path), $str); $data = ['section' => ['sectionVar1' => 'section value 1', 'sectionVar2' => 'section value 2'], 'section data' => ['sectionVar3' => 'section value 3', 'sectionVar4' => 'section value 4'], 'emptysection' => [], 'var1' => 'value 1', 'var2' => 'value 21']; $path = __DIR__ . '/../fixtures/parse/sections.ini'; $this->assertFileExists($path); $str = $parser->render($data); $this->assertEquals($this->getContents($path), $str); $data = ['section' => ['sectionVar1' => 'section value 1', 'sectionVar2' => 'section value 2', 'subsection' => ['subsection value 1', 'subsection value 2'], 'sectionVar3' => 'section value 3'], 'section data' => ['sectionVar3' => 'section value 3', 'sectionVar4' => 'section value 4', 'subsection' => ['subsection value 1', 'subsection value 2']], 'var1' => 'value 1', 'var2' => 'value 21']; $path = __DIR__ . '/../fixtures/parse/subsections.ini'; $this->assertFileExists($path); $str = $parser->render($data); $this->assertEquals($this->getContents($path), $str); }
/** * Formats an INI file string from an array * * @param array $vars Data to format. * @param int $level Specifies the level of array value. * @return string Returns the INI file string. * @static */ public static function render($vars = array(), $level = 1) { return \October\Rain\Parse\Ini::render($vars, $level); }