{"id":18272,"date":"2017-06-29T15:18:39","date_gmt":"2017-06-29T19:18:39","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=18272"},"modified":"2017-07-13T09:34:23","modified_gmt":"2017-07-13T13:34:23","slug":"type-safe-html-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/06\/29\/type-safe-html-in-swift\/","title":{"rendered":"Type-Safe HTML in Swift"},"content":{"rendered":"<p><a href=\"http:\/\/www.fewbutripe.com\/swift\/html\/dsl\/2017\/06\/22\/type-safe-html-in-swift.html\">Brandon Williams<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.fewbutripe.com\/swift\/html\/dsl\/2017\/06\/22\/type-safe-html-in-swift.html\"><p>An alternative approach to views is using <a href=\"https:\/\/wiki.haskell.org\/Embedded_domain_specific_language\">&ldquo;embedded domain specific languages&rdquo;<\/a> (EDSLs). In this approach we use an existing programming language (e.g. Swift), to build a system of types and functions that models the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Abstract_syntax_tree\">structure<\/a> of the domain we are modeling (e.g. HTML).<\/p><p>[&#8230;]<\/p><p>We can use the type system to provide safety around how plain text is encoded for use with HTML. It allows us to prove that non-encoded strings never make it into our HTML.<\/p><p>[&#8230;]<\/p><p>Once you go down the road to thinking of views as functions <code>(Data) -&gt; [Node]<\/code>, you start to build up lots of lil helper views that can be reused in (hopefully) any which way. However, you soon find out that the data these subviews demand needs to be threaded all the way through the view hierarchy all the way back to the root view. This naturally leads one to something known as a <a href=\"https:\/\/hackage.haskell.org\/package\/mtl-2.2.1\/docs\/Control-Monad-Reader.html\"><code>Reader<\/code><\/a> and solves this problem in a beautiful way.<\/p><\/blockquote>\n\n<p><a href=\"http:\/\/www.fewbutripe.com\/swift\/html\/dsl\/2017\/06\/23\/rendering-html-dsl-in-swift.html\">Brandon Williams<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.fewbutripe.com\/swift\/html\/dsl\/2017\/06\/23\/rendering-html-dsl-in-swift.html\"><p>In our previous <a href=\"http:\/\/www.fewbutripe.com\/swift\/html\/dsl\/2017\/06\/22\/type-safe-html-in-swift.html\">article<\/a> we described how to build an EDSL to model HTML in Swift. Here we describe how to take our <code>Node<\/code> value type and render it to a string that can actually be served to the browser.<\/p><\/blockquote>\n\n<p><a href=\"http:\/\/www.fewbutripe.com\/swift\/html\/dsl\/2017\/06\/29\/composable-html-views-in-swift.html\">Brandon Williams<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.fewbutripe.com\/swift\/html\/dsl\/2017\/06\/29\/composable-html-views-in-swift.html\"><p>Now we are going to tackle a problem that goes up one layer in the web-server request lifecycle: creating views. These are the things responsible for taking some data, say a list of articles, and creating the HTML to represent that data. It may sound easy, but there are a lot of difficult problems to solve with views. We want our views to be composable so that we can create small sub-views focused on rendering one piece of data and be able to reuse it. We also want our views to be flexible enough for future developments that are hard to see right now.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Brandon Williams: An alternative approach to views is using &ldquo;embedded domain specific languages&rdquo; (EDSLs). In this approach we use an existing programming language (e.g. Swift), to build a system of types and functions that models the structure of the domain we are modeling (e.g. HTML).[&#8230;]We can use the type system to provide safety around how [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[339,71,901],"class_list":["post-18272","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-html","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=18272"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18272\/revisions"}],"predecessor-version":[{"id":18321,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18272\/revisions\/18321"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=18272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=18272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=18272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}