YAML là gì?YAML (YAML Ain’t Markup Language) là một chuẩn dữ liệu kiểu serialization dành cho tất cả các ngôn ngữ. Nó được sử dụng phổ biến để tạo ra các file config cho nhiều ứng dụng, VD: như Docker Compose. (YAML Ain’t Markup Language) là một chuẩn dữ liệu kiểu serialization dành cho tất cả các ngôn ngữ. Nó được sử dụng phổ biến để tạo ra các file config cho nhiều ứng dụng, VD: như Docker Compose. Show Nội dung chính ShowShow
Thông tin cơ bản:
Cú pháp cơ bảnĐọc 1 file YAML sử dụng ngôn ngữ lập trình # Programing Languages - PHP - Perl - NodeJS # Shopping list [milk, pumpkin pie, eggs, juice] Đọc file YAML trong Perl # Indented Block name: Nguyen Van A age: 33 # Inline Block {name: Nguyen Van A, age: 33} Đọc file YAML trong PHP data: | There once was a short man from Ealing Who got on a bus to Darjeeling It said on the door "Please don't spit on the floor" So he carefully spat on the ceiling data: > Wrapped text will be folded into a single paragraph Blank lines denote paragraph breaks Đọc file YAML trong Python customer: first_name: Dorothy family_name: Gale Đọc 1 file YAML sử dụng ngôn ngữ lập trìnhĐọc file YAML trong PerlĐọc file YAML trong PHP cpan YAML::XS Đọc file YAML trong Python #!/usr/bin/perl # # Read YAML Config File by vinasupport.com # use strict; use warnings; use YAML::XS 'LoadFile'; use Data::Dumper; my $config = LoadFile('config.yaml'); print Dumper($config); Đọc file YAML trong PHPĐọc file YAML trong Python <?php function cb_yaml_date($value, $tag, $flags) { return new DateTime($value); } $yaml = <<<YAML event1: name: My Event date: !date 25.05.2001 YAML; $ndocs = 0; $data = yaml_parse($yaml, 0, $ndocs, array('!date' => 'cb_yaml_date')); print_r($data); Giới thiệu:
Đọc file YAML trong PythonGiới thiệu: pip3 install PyYAML 1. Một số câu lệnh Jinja2: #!/usr/bin/env python3 import yaml with open("configs.yaml", 'r') as stream: try: print(yaml.safe_load(stream)) except yaml.YAMLError as error: print(error) 2. Các bước thực hiện: Version mới nhất: 1.2 2 phút đọc Giới thiệu:
1. Một số câu lệnh Jinja2:2. Các bước thực hiện: biến bắt đầu với {{ tên biến và kết thúc với }} example: tạo biến có tên interface {{ interface }} Version mới nhất: 1.2 câu lệnh for bắt đầu với {% for statement %} và kết thúc với {% end %} example: {% for interface in interfaces %} … {% endfor %} Định dạng mở rộng: .yaml, .yml Câu lệnh if bắt đầu với {% if statement %} và kết thúc với {% endif %} example: {% if interface == ‘ge-0/0/2’ %} … {% endif %} Tổ chức: yaml.org comment starts with ‘{#’ and ends with ‘#} example: {# set description only for interface ge-0/0/2 #} Định nghĩa cấu trúc một danh sách (list) hay mảng (array) pip install jinja2 ( cho python 2.7 ) pip3 install jinja2 ( cho python > 3.6 ) 2. Các bước thực hiện:Version mới nhất: 1.2 # Indented Block name: Nguyen Van A age: 33 # Inline Block {name: Nguyen Van A, age: 33}0 Định dạng mở rộng: .yaml, .yml mở terminal hoặc command-line và start python ( ở đây tôi đang dùng terminal trên Linux ), Import packages, set environment và load jinja2 template: # Indented Block name: Nguyen Van A age: 33 # Inline Block {name: Nguyen Van A, age: 33}1 Tổ chức: yaml.org # Indented Block name: Nguyen Van A age: 33 # Inline Block {name: Nguyen Van A, age: 33}2 Định nghĩa cấu trúc một danh sách (list) hay mảng (array)interfaces.yaml file để lưu lại các thông tin cấu hình # Indented Block name: Nguyen Van A age: 33 # Inline Block {name: Nguyen Van A, age: 33}3 Định nghĩa kiểu dữ liệu key-value # Indented Block name: Nguyen Van A age: 33 # Inline Block {name: Nguyen Van A, age: 33}4 Định nghĩa ký tự dạng chuỗi, ký tự dạng chuỗi không yêu cầu dấu quote Định nghĩa 1 đối tượng
Để đọc file yaml trong PHP, các bạn có thể sử dụng function yaml_parse_file được PHP hỗ trợ sẵn. Đoạn code example như bên dưới. |