> For the complete documentation index, see [llms.txt](https://javascriptuse.gitbook.io/advanced/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://javascriptuse.gitbook.io/advanced/javascript/yaml-in-action-tutorial-series-general-huong-dan-su-dung-file-yml-yaml-co-ban.md).

# \[YAML] In Action Tutorial Series - General - Hướng dẫn sử dụng file YML YAML cơ bản

## Ví dụ hoàn thành :)

```
store: "Viblo"
address: "Asia"
fruits:
  - name: "Orange"
    price: "1$"
  - name: "Banana"
    price: "2$"
  - value: true
array:
  - 1
  - 2
  - 3
object:
  property1: value1
  property2: value2
Array
(
  [store] => Viblo
  [address] => Asia
  [fruits] => Array
  (
    [0] => Array
    (
      [name] => Orange
      [price] => 1$
    )
    [1] => Array
    (
      [name] => Banana
      [price] => 2$
    )
    [2] => Array
    (
      [value] => 1    
    )
  )
  [array] => Array
  (
    [0] => 1
    [1] => 2
    [2] => 3
  )
  [object] => Array
  (
    [property1] => value1
    [property2] => value2
  )
)
```

## In Action Tutorial Series - General - Hướng dẫn sử dụng file YML YAML cơ bản

## I. Giới thiệu <a href="#i-gioi-thieu-0" id="i-gioi-thieu-0"></a>

### 1. YML YAML là gì? <a href="#id-1-yml-yaml-la-gi-1" id="id-1-yml-yaml-la-gi-1"></a>

* YML và YAML là một ngôn ngữ đánh dấu văn bản tương tự HTML, XML.

### 2. YML khác YAML chỗ nào? <a href="#id-2-yml-khac-yaml-cho-nao-2" id="id-2-yml-khac-yaml-cho-nao-2"></a>

* Khác nhau mỗi extension thôi. Nội dung, cú pháp giống hết nhau. Do đó phần còn lại của bài viết mình sử dụng YML thôi nhé.

### 3. Tại sao chọn YML? <a href="#id-3-tai-sao-chon-yml-3" id="id-3-tai-sao-chon-yml-3"></a>

* Dễ nhìn, dễ chỉnh sửa, phù hợp với các file config cho mọi ngôn ngữ

### II. Parse YML YAML như nào? <a href="#ii-parse-yml-yaml-nhu-nao-4" id="ii-parse-yml-yaml-nhu-nao-4"></a>

Mỗi ngôn ngữ có một library để parse khác nhau. Không phải viết lại làm gì cả. Mình lấy ví dụ trong PHP

#### 1. PHP <a href="#id-1-php-5" id="id-1-php-5"></a>

Sử dụng library `symfony/yaml`

```
composer require symfony/yaml
```

Có file config.yaml như sau

```
store: "Viblo"
address: "Asia"
fruits:
  - name: "Orange"
    price: "1$"
  - name: "Banana"
    price: "2$"
```

Đọc, xử lý trong file parse.php

```
<?php
require 'vendor/autoload.php';

use Symfony\Component\Yaml\Yaml;

$yamlContent = file_get_contents('config.yaml');

$parseContent = Yaml::parse($yamlContent);

print_r($parseContent);
```

Kết quả

```
Array
(
    [store] => Viblo
    [address] => Asia
    [fruits] => Array
        (
            [0] => Array
                (
                    [name] => Orange
                    [price] => 1$
                )
            [1] => Array
                (
                    [name] => Banana
                    [price] => 2$
                )
        )
)
```

Rất đơn giản phải không nào. Mình sẽ viết lại file config dưới dạng json, php array và xml để bạn thấy rõ hơn tại sao nên dùng YML nhé ![😄](https://twemoji.maxcdn.com/2/72x72/1f604.png) **- config.php**

```
<?php
$config = [
  "store" => "Viblo",
    "address" => "Asia",
    "fruits" => [
        [
            "name" => "Orange",
            "price" => "$1"
        ],
        [
            "name" => "Banana",
            "price" => "$2"
        ]
    ]
];
// PHP gọi thẳng ra được tiện đấy :D. Nhưng project có ngôn ngữ khác parse đống này sao giờ :(
```

**- config.json**

```
{
  "store": "Viblo",
  "address": "Asia",
  "fruits": [
    {
      "name" : "Orange",
      "price" : "$1"
    },
    {
      "name" : "Banana",
      "price": "$2"
    }
  ]
}
```

**- config.xml**

```
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <store>Viblo</store>
    <address>Asia</address>
    <fruits>
        <fruit>
            <name>Orange</name>
            <price>$1</price>    
        </fruit>
        <fruit>
            <name>Banana</name>
            <price>$2</price>
        </fruit>
    </fruits>
</root>
```

## III. Cú pháp <a href="#iii-cu-phap-6" id="iii-cu-phap-6"></a>

1. String

```
A string

'A singled-quoted string'

"A double-quoted string"
```

1. Number

```
# an integer
12
# a float
13.4
```

1. Boolean

```
true
false
```

1. Array

```
array1: [1,2,3]
// or
array2:
  - 1
  - 2
  - 3
```

1. Object

```
object1: {property1: value1, property2: value2}
//or
object2:
 - property1: value1
 - property2: value2
```

1. Comment

```
# this is a comment
```

Mong bài hướng dẫn này có thể giúp các bạn áp dụng YML vào dự án của mình dễ dàng hơn. Thanks for reading ![😄](https://twemoji.maxcdn.com/2/72x72/1f604.png)

Source:

* <http://symfony.com/doc/current/components/yaml.html>
