1

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 :) ?

4
  • The second argument of the dump() method is used to control at which level the dumper switches to the inline notation. Its default value is 2. You probably want to increase that for your use case like this: $yaml = Yaml::dump($array, 4); Commented Oct 24, 2019 at 20:14
  • @xabbuh I tried that, but the output is exactly the same :/ Commented Oct 24, 2019 at 20:22
  • I can see a different result. Which version of the Yaml component do you use? Commented Oct 24, 2019 at 20:26
  • Ok yes indeed - now it changed. The only thing is that in the notation, it still stays as " component: MyComponent" instead of " - component: MyComponent" Commented Oct 24, 2019 at 20:35

1 Answer 1

2

To control the level at which the dumper switches to using the inline notation you have to pass the desired level as the second argument to the dump() method (the default value is 2).

To get the expected structure while dumping you need to adjust the input array which currently does not represent what you expect it to be:

use Symfony\Component\Yaml\Yaml;

$array = [
    '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',
                    ],
                ],
            ],
        ],
    ],
];

$yaml = Yaml::dump($array, 6);
Sign up to request clarification or add additional context in comments.

2 Comments

I still have exactly the same issue, where it's showing "component: MyComponent" instead of "- component: MyComponent" and the "fields" are on the same level instead of a child :/
Can you update your description with the exact code that you have right now? I am pretty sure that your $array variable is not composed as you expect it to be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.