2017-10-05 16 views
0

私は、次のデータを持っている:どのようにRウィスカーのテンプレートエンジンを使用して改行と番号付きリストを生成する

data <- list(name = "Chris", 
      children_names = c("Alex", "John") 
      ) 

Rのテンプレートエンジンを使用してwhisker、私はレンダリングされたときに、この出力、 を取得したい:

I am Chris 
My children are: 
    Child No 1 is Alex 
    Child No 2 is John 

これは私の現在のコードです:

library(whisker) 
template <- 
'I am {{name}} 
My children are: 
{{children_names}} 
' 

data <- list(name = "Chris", 
      children_names = c("Alex", "John") 

      ) 

text <- whisker.render(template, data) 
cat(text) 

# which produces: 

# I am Chris 
# My children are: 
# Alex,John 

W私は欲しいものではありません。 これを行う正しい方法は何ですか?

答えて

1

あなたはおそらくすでにこれを考え出したが、ケースには、あなたはそうではありません。

library(whisker) 

template <- 
    'I am {{name}} \n 
    My children are: \n 
    {{#children_names}} 
    Child No {{number}} is {{cname}} 
    {{/children_names}}' 

data <- list( 
    name = "Chris", 
    children_names = list(
    list(cname = "Alex", number = 1), list(cname = "John", number = 2) 
) 
) 

text <- whisker.render(template, data) 
cat(text) 

# I am Chris 
# 
# My children are: 
# 
# Child No 1 is Alex 
# Child No 2 is John 
関連する問題