Skip to content

Yaml with Symfony Yaml

World Wide Web Server edited this page Jul 4, 2012 · 7 revisions

Category:Libraries::Yaml | Category:Yaml

[h3]YAML Ain't Markup Language[/h3]

YAML is a human friendly data serialization standard for all programming languages. YAML is a great format for your configuration files. YAML files are as expressive as XML files and as readable as INI files.

[h3]Symfony YAML[/h3] Symfony YAML is a PHP library that parses YAML strings and converts them to PHP arrays. It can also converts PHP arrays to YAML strings. sfYaml is licensed by a Attribution-Share Alike 3.0 Unported License. [url=http://components.symfony-project.org/yaml/trunk/book/A-License]see license[/url]

Thread: [url=http://codeigniter.com/forums/viewthread/168491/]http://codeigniter.com/forums/viewthread/168491/[/url]

[strong]Wrapper (codeigniter library) and sfyaml.[/strong]

-File:ci_sfyaml.zip

Tested with codeigniter versions:

  • 1.7.2

To load this library: [code] $this->load->library('yaml'); [/code]

[h3]Method Summary[/h3]

[strong]load[/strong] Loads YAML into a PHP array.

  • @param string $input Path of YAML file or string containing YAML [code] $input = APPPATH . 'directory/filename.yml'; $this->yaml->load($input); // return array [/code]

[strong]dump[/strong] Dumps a PHP array to a YAML string.

  • @param integer $inline - The level where you switch to [url=http://en.wikipedia.org/wiki/YAML#Basic_components_of_YAML]inline[/url] YAML

[code]$this->yaml->dump($array, $inline); // return string[/code] [code] $array = array( 'one' => array ( 'id' => 1, 'username' => 'administrator', 'password' => 'adminpass', 'email' => '[email protected]', 'fullname' => 'Jonh Doe', 'role' => 1 ),

'two' => array ( 'id' => 2, 'username' => 'manager', 'password' => 'manpass', 'email' => '[email protected]', 'fullname' => 'Jane Doe', 'role' => 2 ) ); $inline = 2; $this->yaml->dump($array, $inline); [/code]

Returns this YAML string representing the original PHP array: [code] one: id: 1 username: administrator password: adminpass email: [email protected] fullname: 'John Doe' role: 1 two: id: 2 username: manager password: manpass email: [email protected] fullname: 'Jane Doe' role: 2 [/code]

[strong]write_yml[/strong] Save a file with a YAML string Dumped from a PHP array.

  • @param string $file File name
  • @param array $array PHP array
  • @param string $path Optional specific directory path to save the file [code]$this->write_yml($file, $array, $path = '');[/code] [code] $path = APPPATH.'fixtures'; $this->yaml->write_yml('temp.yml', $array, $path); [/code] [code] $file = APPPATH.'fixtures/temp.yml'; $this->yaml->write_yml($file, $array); [/code] In both cases the file is saved to: [em]application/fixtures/temp.yml[/em]

[strong]getSpecVersion[/strong] Gets the YAML specification version to use. [code]$this->yaml->getSpecVersion() // return string[/code]

[strong]setSpecVersion[/strong] Sets the YAML specification version to use. [code]$this->yaml->setSpecVersion($version) // return void[/code]

[strong]Note:[/strong] Default Yaml specification version is 1.2

Codeigniter Comunity username: [strong]OliverHR[/strong]

Clone this wiki locally