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

https://viblo.asia/p/in-action-tutorial-series-general-huong-dan-su-dung-file-yml-yaml-co-ban-Az45bNbw5xY

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

1. YML YAML là gì?

  • 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?

  • 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?

  • 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?

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

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

  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

Source:

Last updated