在YAML中,個人字符串很長。 我想將其保留在編輯器的80列(或相似視圖)內,因此我想破壞字符串。 這是什麼語法? javascript
換句話說,我有這個: html
Key: 'this is my very very very very very very long string'
我想要這個(或達到這種效果的東西): java
Key: 'this is my very very very ' + 'long string'
我想使用上述引號,所以不須要在字符串中轉義任何內容。 markdown
要保留換行符,請使用|
, 例如: app
| This is a very long sentence that spans several lines in the YAML but which will be rendered as a string with newlines preserved.
被翻譯爲「這是一個很長的句子 \\ n ,它跨越YAML \\ n中的幾行,但將被呈現爲字符串 \\ n,並保留換行符。 \\ n 「 less
在YAML中有5 6 NINE (或63 *,取決於您的計數方式)不一樣的方式來編寫多行字符串。 編輯器
一般,您須要>
: 工具
key: > Your long string here.
若是要在字符串中將換行符保留爲\\n
(例如,帶段落的嵌入式markdown),請使用|
。 this
key: | ### Heading * Bullet * Points
若是您不但願在末尾添加換行符,請使用>-
或|-
代替。 spa
若是您須要在單詞中間分割行或直接將換行符鍵入\\n
,請使用雙引號代替:
key: "Antidisestab\\ lishmentarianism.\\n\\nGet on it."
YAML太瘋狂了。
>
, |
) 這些容許使用\\
和"
字符而不進行轉義,並在字符串的末尾添加新行( \\n
)。
>
摺疊樣式會刪除字符串中的單個換行符(但在末尾添加一個,並將雙換行符轉換爲單行):
Key: > this is my very very very long string
→ this is my very very very long string\\n
|
文字風格將字符串中的每一個換行符轉換爲文字換行符,並在末尾添加一個:
Key: | this is my very very very long string
→ this is my very very very\\nlong string\\n
這是YAML Spec 1.2中的官方定義
標量內容可使用文字樣式(由「 |」表示)以塊符號表示,其中全部換行符均有效。 或者,能夠用摺疊樣式(用「>」表示)來書寫它們,其中每一個換行符都被摺疊到一個空格,除非它以空行或縮進的行結束。
>-
, |-
, >+
, |+
) 您能夠經過添加塊斬斷指示符來控制字符串中最後一個新行以及任何尾隨空白行( \\n\\n
)的處理:
>
, |
:「 clip」:保持換行,刪除尾隨的空白行。 >-
, |-
:「 strip」:刪除換行,刪除尾隨的空白行。 >+
, |+
:「保持」:保持換行,尾隨空白行。
, "
, '
) 這些具備有限的轉義,並構造沒有換行符的單行字符串。 它們能夠與鍵在同一行開始,也能夠先添加其餘換行。
普通樣式 (無轉義,無#
或:
組合,第一個字符的限制):
Key: this is my very very very long string
用雙引號引發來的樣式 ( \\
和"
必須用\\
進行轉義,換行符能夠按原義\\n
序列插入,行的鏈接能夠不帶尾隨\\
空格):
Key: "this is my very very \"very\" loooo\ ng string.\n\nLove, YAML."
→ "this is my very very \\"very\\" loooong string.\\n\\nLove, YAML."
單引號樣式 (必須將雙引號'
,不能使用特殊字符,可能對錶示以雙引號開頭的字符串頗有用):
Key: 'this is my very very "very" long string, isn''t it.'
→ "this is my very very \\"very\\" long string, isn't it."
在此表中, _
表示space character
。 \\n
表示「換行符」(JavaScript中爲\\n
),但「行內換行」行除外,該行的字面意思是反斜槓和n。
> | " ' >- >+ |- |+ -------------------------|------|-----|-----|-----|------|------|------|------ Trailing spaces | Kept | Kept | | | | Kept | Kept | Kept | Kept Single newline => | _ | \n | _ | _ | _ | _ | _ | \n | \n Double newline => | \n | \n\n | \n | \n | \n | \n | \n | \n\n | \n\n Final newline => | \n | \n | | | | | \n | | \n Final dbl nl's => | | | | | | | Kept | | Kept In-line newlines | No | No | No | \n | No | No | No | No | No Spaceless newlines| No | No | No | \ | No | No | No | No | No Single quote | ' | ' | ' | ' | '' | ' | ' | ' | ' Double quote | " | " | " | \" | " | " | " | " | " Backslash | \ | \ | \ | \\ | \ | \ | \ | \ | \ " #", ": " | Ok | Ok | No | Ok | Ok | Ok | Ok | Ok | Ok Can start on same | No | No | Yes | Yes | Yes | No | No | No | No line as key |
請注意「空格」以前一行的尾隨空格。
- > very "long" 'string' with paragraph gap, \n and spaces. - | very "long" 'string' with paragraph gap, \n and spaces. - very "long" 'string' with paragraph gap, \n and spaces. - "very \"long\" 'string' with paragraph gap, \n and s\ p\ a\ c\ e\ s." - 'very "long" ''string'' with paragraph gap, \n and spaces.' - >- very "long" 'string' with paragraph gap, \n and spaces. [ "very \"long\" 'string' with\nparagraph gap, \\n and spaces.\n", "very \"long\"\n'string' with\n\nparagraph gap, \\n and \nspaces.\n", "very \"long\" 'string' with\nparagraph gap, \\n and spaces.", "very \"long\" 'string' with\nparagraph gap, \n and spaces.", "very \"long\" 'string' with\nparagraph gap, \\n and spaces.", "very \"long\" 'string' with\nparagraph gap, \\n and spaces." ]
以防萬一上述內容不足以知足您的須要,您能夠添加一個「 塊縮進指示器 」(若是有,則在塊切割指示器以後):
- >8 My long string starts over here - |+1 This one starts here
若是您以折線樣式在非第一行的開頭插入多餘的空格,則會保留這些空格,並帶有額外的換行符。 流樣式不會發生這種狀況:
- > my long string - my long string
→ ["my long\\n string\\n", "my long string"]
我什至不能
*
2種塊樣式,每種都有2個可能的塊指示符(或無),以及9種可能的縮進指示符(或無),1種普通樣式和2種帶引號的樣式:2 x(2 + 1)x(9 +1)+ 1 + 2 = 63
這些信息中的一些也已在此處進行了總結。
您可能不相信,可是YAML也能夠執行多行鍵:
? > multi line key : value
若是您在Symfony中使用YAML和Twig進行翻譯,而且想在Javascript中使用多行翻譯,則在翻譯後當即添加回車符。 所以,即便是如下代碼:
var javascriptVariable = "{{- 'key'|trans -}}";
其中yml翻譯以下:
key: > This is a multi line translation.
仍將致使如下html代碼:
var javascriptVariable = "This is a multi line translation. ";
所以,Twig中的減號不能解決此問題。 解決方案是在yml中的大於號後添加此減號:
key: >- This is a multi line translation.
將得到正確的結果,在Twig中的一行上進行多行翻譯:
var javascriptVariable = "This is a multi line translation.";
使用yaml摺疊樣式,每一個換行符都被一個空格代替。 每行的縮進將被忽略。 一個換行符將插入到末尾。
Key: > This is a very long sentence that spans several lines in the YAML but which will be rendered as a string with only a single carriage return appended to the end.
http://symfony.com/doc/current/components/yaml/yaml_format.html
您可使用「塊記錄指示器」消除尾隨換行符,以下所示:
Key: >- This is a very long sentence that spans several lines in the YAML but which will be rendered as a string with NO carriage returns.
也有其餘控制工具可用(例如,用於控制壓痕)。