I am using the Symfony YAML Component to model and output a yml file.
What I want to achieve is the following:
id: my-page
name: My Page
fields:
pageTitle: My Page Title
placeholders:
project-main:
- component: MyComponent
fields:
imageSmall:
src: /path/to/image.jpg
imageMed:
src: /path/to/image.jpg
I am almost managing that, just need help to understand how I need to form the line from componentName onwards. The result I'm getting at the moment is the following:
id: my-page
name: My Page
fields:
pageTitle: 'My Page Title'
placeholders:
project-main: { component: MyComponent, 0: { fields: { imageSmall: 'src: /path/to/image.jpg', imageMed: 'src: /path/to/image.jpg' } } }
Whilst this is the PHP Code I'm using to obtain this, is the following:
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;
$array = array(
"id" => "my-page",
"name" => "My Page",
"fields" => ["pageTitle" => "My Page Title"],
"placeholders" => ["project-main" => array("component" => "MyComponent", array("fields"=>array("imageSmall" => "src: /path/to/image.jpg","imageMed" => "src: /path/to/image.jpg")))]);
$yaml = Yaml::dump($array);
file_put_contents('file.yaml', $yaml);
Anyone can help put me on the right track :) ?
**
- UPDATE
**
I have followed the tip in the comments and updated the PHP as follows:
$array = array(
"id" => "my-page",
"name" => "My Page",
"fields" => ["pageTitle" => "My Page Title"],
"placeholders" => ["project-main" => ["component" => "MyComponent", "fields"=>array("imageSmall" => "src: /path/to/image.jpg","imageMed" => "src: /path/to/image.jpg")]]);
$yaml = Yaml::dump($array,4);
file_put_contents('file.yaml', $yaml);
After this, my output is as follows:
id: my-page
name: My Page
fields:
pageTitle: My Page Title
placeholders:
project-main:
component: MyComponent
fields:
imageSmall:
src: /path/to/image.jpg
imageMed:
src: /path/to/image.jpg
So I have moved a bit, but still getting "component: MyComponent" instead of "- component: My Component". Any tips on solving this one last issue :) ?
dump()method is used to control at which level the dumper switches to the inline notation. Its default value is2. You probably want to increase that for your use case like this:$yaml = Yaml::dump($array, 4);