Created
July 18, 2017 08:39
-
-
Save honewatson/7ba83dadd99cd68a0dbb09d4009969df to your computer and use it in GitHub Desktop.
Nim Stateless Components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import stringinterpolation | |
| import sequtils | |
| type | |
| Link = tuple[name: string, url: string] | |
| Links = seq[Link] | |
| State = tuple[title: string, links: Links] | |
| proc join(items: seq[string]): string = | |
| result = "" | |
| for ch in items: | |
| add(result, ch) | |
| var state: State = (title: "Menu", links: @[("Contact", "/contact"), ("About Us", "/about-us")]) | |
| proc li(link: Link): string = | |
| ifmt"""<li><a href="${link.url}">${link.name}</a></li>""" | |
| proc ul(links: Links): string = | |
| ifmt""" | |
| <ul> | |
| ${links.map(li).join} | |
| </ul> | |
| """ | |
| proc menu(state: State): string = | |
| ifmt""" | |
| <h3>${state.menu}</h3> | |
| ${ul(state.links)} | |
| """ | |
| echo menu(state) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment