diff --git a/layout/example.yml b/layout/example.yml index 69c507e..a7f9c79 100644 --- a/layout/example.yml +++ b/layout/example.yml @@ -72,5 +72,5 @@ results: id: '{{ .ID }}' title: '{{ .Title }}' description: '{{ .Description }}' - message_text: '{{ .Content }}' - thumb_url: '{{ .PreviewURL }}' \ No newline at end of file + thumb_url: '{{ .PreviewURL }}' + message_text: '{{ text `article_message` }}' diff --git a/layout/layout.go b/layout/layout.go index da6ad94..8eb6214 100644 --- a/layout/layout.go +++ b/layout/layout.go @@ -51,9 +51,10 @@ type ( // Result represents layout-specific result to be parsed. Result struct { - result *template.Template - ResultBase `yaml:",inline"` - Markup string `yaml:"markup"` + result *template.Template + tele.ResultBase `yaml:",inline"` + Content ResultContent `yaml:"content"` + Markup string `yaml:"markup"` } // ResultBase represents layout-specific result's base to be parsed. @@ -109,20 +110,20 @@ var builtinFuncs = template.FuncMap{ // Settings returns built telebot Settings required for bot initializing. // -// settings: -// url: (custom url if needed) -// token: (not recommended) -// updates: (chan capacity) -// locales_dir: (optional) -// token_env: (token env var name, example: TOKEN) -// parse_mode: (default parse mode) -// long_poller: (long poller settings) -// webhook: (or webhook settings) +// settings: +// url: (custom url if needed) +// token: (not recommended) +// updates: (chan capacity) +// locales_dir: (optional) +// token_env: (token env var name, example: TOKEN) +// parse_mode: (default parse mode) +// long_poller: (long poller settings) +// webhook: (or webhook settings) // // Usage: -// lt, err := layout.New("bot.yml") -// b, err := tele.NewBot(lt.Settings()) -// // That's all! +// lt, err := layout.New("bot.yml") +// b, err := tele.NewBot(lt.Settings()) +// // That's all! // func (lt *Layout) Settings() tele.Settings { if lt.pref == nil { @@ -225,12 +226,12 @@ func (lt *Layout) CommandsLocale(locale string, args ...interface{}) (cmds []tel // The given optional argument will be passed to the template engine. // // Example of en.yml: -// start: Hi, {{.FirstName}}! +// start: Hi, {{.FirstName}}! // // Usage: -// func onStart(c tele.Context) error { -// return c.Send(lt.Text(c, "start", c.Sender())) -// } +// func onStart(c tele.Context) error { +// return c.Send(lt.Text(c, "start", c.Sender())) +// } // func (lt *Layout) Text(c tele.Context, k string, args ...interface{}) string { locale, ok := lt.Locale(c) @@ -265,8 +266,8 @@ func (lt *Layout) TextLocale(locale, k string, args ...interface{}) string { // Callback returns a callback endpoint used to handle buttons. // // Example: -// // Handling settings button -// b.Handle(lt.Callback("settings"), onSettings) +// // Handling settings button +// b.Handle(lt.Callback("settings"), onSettings) // func (lt *Layout) Callback(k string) tele.CallbackEndpoint { btn, ok := lt.buttons[k] @@ -279,27 +280,27 @@ func (lt *Layout) Callback(k string) tele.CallbackEndpoint { // Button returns a button, which locale is dependent on the context. // The given optional argument will be passed to the template engine. // -// buttons: -// item: -// unique: item -// callback_data: {{.ID}} -// text: Item #{{.Number}} +// buttons: +// item: +// unique: item +// callback_data: {{.ID}} +// text: Item #{{.Number}} // // Usage: -// btns := make([]tele.Btn, len(items)) -// for i, item := range items { -// btns[i] = lt.Button(c, "item", struct { -// Number int -// Item Item -// }{ -// Number: i, -// Item: item, -// }) -// } +// btns := make([]tele.Btn, len(items)) +// for i, item := range items { +// btns[i] = lt.Button(c, "item", struct { +// Number int +// Item Item +// }{ +// Number: i, +// Item: item, +// }) +// } // -// m := b.NewMarkup() -// m.Inline(m.Row(btns...)) -// // Your generated markup is ready. +// m := b.NewMarkup() +// m.Inline(m.Row(btns...)) +// // Your generated markup is ready. // func (lt *Layout) Button(c tele.Context, k string, args ...interface{}) *tele.Btn { locale, ok := lt.Locale(c) @@ -352,19 +353,19 @@ func (lt *Layout) ButtonLocale(locale, k string, args ...interface{}) *tele.Btn // Markup returns a markup, which locale is dependent on the context. // The given optional argument will be passed to the template engine. // -// buttons: -// settings: 'Settings' -// markups: -// menu: -// - [settings] +// buttons: +// settings: 'Settings' +// markups: +// menu: +// - [settings] // // Usage: -// func onStart(c tele.Context) error { -// return c.Send( -// lt.Text(c, "start"), -// lt.Markup(c, "menu"), -// ) -// } +// func onStart(c tele.Context) error { +// return c.Send( +// lt.Text(c, "start"), +// lt.Markup(c, "menu"), +// ) +// } // func (lt *Layout) Markup(c tele.Context, k string, args ...interface{}) *tele.ReplyMarkup { locale, ok := lt.Locale(c) @@ -416,26 +417,26 @@ func (lt *Layout) MarkupLocale(locale, k string, args ...interface{}) *tele.Repl // Result returns an inline result, which locale is dependent on the context. // The given optional argument will be passed to the template engine. // -// results: -// article: -// type: article -// id: '{{ .ID }}' -// title: '{{ .Title }}' -// description: '{{ .Description }}' -// message_text: '{{ .Content }}' -// thumb_url: '{{ .PreviewURL }}' +// results: +// article: +// type: article +// id: '{{ .ID }}' +// title: '{{ .Title }}' +// description: '{{ .Description }}' +// message_text: '{{ .Content }}' +// thumb_url: '{{ .PreviewURL }}' // // Usage: -// func onQuery(c tele.Context) error { -// results := make(tele.Results, len(articles)) -// for i, article := range articles { -// results[i] = lt.Result(c, "article", article) -// } -// return c.Answer(&tele.QueryResponse{ -// Results: results, -// CacheTime: 100, -// }) +// func onQuery(c tele.Context) error { +// results := make(tele.Results, len(articles)) +// for i, article := range articles { +// results[i] = lt.Result(c, "article", article) // } +// return c.Answer(&tele.QueryResponse{ +// Results: results, +// CacheTime: 100, +// }) +// } // func (lt *Layout) Result(c tele.Context, k string, args ...interface{}) tele.Result { locale, ok := lt.Locale(c) @@ -466,7 +467,7 @@ func (lt *Layout) ResultLocale(locale, k string, args ...interface{}) tele.Resul var ( data = buf.Bytes() - base ResultBase + base Result r tele.Result ) @@ -543,6 +544,7 @@ func (lt *Layout) ResultLocale(locale, k string, args ...interface{}) tele.Resul if base.Content != nil { r.SetContent(base.Content) } + if result.Markup != "" { markup := lt.MarkupLocale(locale, result.Markup, args...) if markup == nil { diff --git a/layout/layout_test.go b/layout/layout_test.go index b34e3d1..1524ddf 100644 --- a/layout/layout_test.go +++ b/layout/layout_test.go @@ -109,19 +109,17 @@ func TestLayout(t *testing.T) { }, Title: "Some title", Description: "Some description", - Text: "The text of the article", ThumbURL: "https://preview.picture", + Text: "This is an article.", }, lt.ResultLocale("en", "article", struct { ID int Title string Description string - Content string PreviewURL string }{ ID: 1853, Title: "Some title", Description: "Some description", - Content: "The text of the article", PreviewURL: "https://preview.picture", })) } diff --git a/layout/locales/en.yml b/layout/locales/en.yml new file mode 100644 index 0000000..e3bfee9 --- /dev/null +++ b/layout/locales/en.yml @@ -0,0 +1 @@ +article_message: This is an article. \ No newline at end of file