是否可以在 YAML 中与 ERB 一起使用三元运算符,如果可以的话如何?

问题描述 投票:0回答:1

我一直在尝试下面的许多语法变体,但除了语法错误之外无法得到任何内容(简单的 erb 插值可以自行工作,但逻辑三元运算符会引发语法错误):

start_prompt: "Drum roll please \U0001F941\U0001F941\U0001F941.
    <%= active_player_name %> will <%= @new_game ? "begin" : "continue" %> play.
    Let the game commence \U0001F3AC. And may the best player win \U0001F3C6."

关于如何让它发挥作用有什么想法吗? (我意识到在其他地方编写这样的逻辑是更好的做法,但是长话短说,在这种情况下,在这里更容易,无论这种做法好或多坏,我想知道这是如何/是否无论如何都有可能。谢谢

yaml erb
1个回答
0
投票

YAML 不像其他语言那样支持字符串插值。 您可能需要对您的 YAML 文件 ruby 进行预处理,然后将处理后的内容写入到 YAML 文件中。

这是一个例子:

active_player_name = "John Doe"
new_game = true

yaml_content = <<~YAML
start_prompt: "Drum roll please \\U0001F941\\U0001F941\\U0001F941.
    #{active_player_name} will #{new_game ? "begin" : "continue"} play.
    Let the game commence \\U0001F3AC. And may the best player win \\U0001F3C6."
YAML

File.open('output.yaml', 'w') { |file| file.write(yaml_content) }
© www.soinside.com 2019 - 2024. All rights reserved.