<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ben Sharman on Medium]]></title>
        <description><![CDATA[Stories by Ben Sharman on Medium]]></description>
        <link>https://medium.com/@mutablestate?source=rss-fe02d9dcff19------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*s_I1MGaVC0pKY0Ds.jpeg</url>
            <title>Stories by Ben Sharman on Medium</title>
            <link>https://medium.com/@mutablestate?source=rss-fe02d9dcff19------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 16:26:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mutablestate/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Customise your Phoenix HTML Generator]]></title>
            <link>https://medium.com/@mutablestate/customise-your-phoenix-html-generator-d45fecd48683?source=rss-fe02d9dcff19------2</link>
            <guid isPermaLink="false">https://medium.com/p/d45fecd48683</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[phoenix]]></category>
            <dc:creator><![CDATA[Ben Sharman]]></dc:creator>
            <pubDate>Fri, 10 Mar 2017 19:26:20 GMT</pubDate>
            <atom:updated>2017-03-11T23:22:07.621Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FBJdKJt9CTNCpV4dMzPJaw.jpeg" /><figcaption>Generator Hostel Dublin by <a href="https://www.flickr.com/photos/yukino/">Rosa G</a></figcaption></figure><p><strong>What’s it all about?</strong></p><p>Scaffolding with Phoenix generators, especially with mix phx.gen.html, is a great way start out a project. Not only do you get a resource all wired up for CRUD it also provides unit tests and demonstrates best practices.</p><p>All this sounds great so why did I stop using such a powerful ally after that initial burst of enthusiasm? I think the main reason was the desire to take off the proverbial training wheels provided by the generator and get writing some sweet Elixir code. My confidence was up and going it alone was aided by a helpful feedback loop of error messages and stack traces.</p><p>The fundamental point i’d completely missed of using a generator with templates is achieving consistency through automation. Consistency is one of the keys to a maintainable codebase and also happens to be the basis of a great UX. By customising the Phoenix HTML generator maybe I can achieve both of these goals.</p><p><strong>The scenario</strong></p><p>Being an opinionated software engineer I’ve decided my new CRUD project will use <a href="https://milligram.github.io/">Milligram</a>, a minimalist css framework, instead of the standard bootstrap css used by Phoenix and most of the web. Making that one simple decision renders the standard Phoenix HTML generator pretty useless.</p><p><strong>One of many solutions</strong></p><p>Replace the standard mix phx.gen.html with a project specific generator and templates. The templates can then be updated and re-used to generate each new resource I add with the latest styles and layout.</p><p><strong>Let’s do it!</strong></p><ol><li>Create a new Phoenix 1.3 project</li></ol><pre>mix phx.new boiler &amp;&amp; cd boiler</pre><p>2. Replace assets/css/phoenix.css with milligram.css</p><p>3. Make the templates and mix task project directories</p><pre>mkdir -p priv/templates/boiler.gen.html &amp;&amp; mkdir -p lib/boiler/mix/tasks</pre><p>4. Copy all current HTML generator templates to the new project templates directory</p><pre>cp -a deps/phoenix/priv/templates/phx.gen.html/. priv/templates/boiler.gen.html</pre><p>5. Copy the current generator task to the new project tasks directory</p><pre>cp deps/phoenix/lib/mix/tasks/phx.gen.html.ex lib/boiler/mix/tasks/boiler.gen.html.ex</pre><p>6. Open lib/boiler/mix/tasks/boiler.gen.html.ex in your favourite editor and update the module name to Mix.Tasks.Boiler.Gen.Html</p><p>7. In the same file search / replace all instances of lowercase phx with boiler</p><p>8. Check that your code compiles and you have a boiler project mix task</p><pre>mix compile &amp;&amp; mix help | grep boiler</pre><p>If all went well you should see our new mix task listed as mix boiler.gen.html</p><p>9. Its time to edit the eex templates in priv/templates/boiler.gen.html using Milligram classes and any markup changes before using our new generator command.</p><p>Milligram uses a different grid system to bootstrap so we also need to update lib/boiler/web/templates/layout/app.html.eex</p><p><strong>Its generate time!</strong></p><p>Use our new mix task as you would with the Phoenix generator.</p><pre>mix boiler.gen.html Accounts User users name:string email:string phone:string</pre><p>Follow the instructions to add the resource to the router and run the migrations.</p><p>Start the server withmix phx.server and visit <a href="http://localhost:4000/users">http://localhost:4000/users</a> to see your custom generated HTML templates in action.</p><p><strong>Don’t stop now</strong></p><p>This is a small example of generating custom markup for a CRUD app but its only a taster of what’s possible.</p><p>Are you using Phoenix custom generators in creative ways?</p><p>Thanks for reading!</p><p>The repo can be found at <a href="https://github.com/mutablestate/boiler">https://github.com/mutablestate/boiler</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d45fecd48683" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pipe your way to consistent, readable, maintainable Elixir code]]></title>
            <link>https://medium.com/@mutablestate/pipe-your-way-to-readable-and-maintainable-elixir-code-9281093958a6?source=rss-fe02d9dcff19------2</link>
            <guid isPermaLink="false">https://medium.com/p/9281093958a6</guid>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Ben Sharman]]></dc:creator>
            <pubDate>Wed, 15 Apr 2015 09:29:56 GMT</pubDate>
            <atom:updated>2015-04-15T18:16:36.331Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>Tl:dr</strong> <em>use pipes wherever possible.</em></h4><p>Like most folks new to Elixir, discovering the pipe operator `|&gt;` and understanding how to use it was a ‘Eureka moment’ that convinced me to explore the language further.</p><p>In short, the pipe operator feeds the result of whatever is on its left side as the first parameter to a function on its right side.</p><p>For example, using <a href="http://elixir-lang.org/docs/stable/elixir/Map.html#put/3">Map.put/3</a> to update the key value to “Bar” is commonly written:</p><p><strong>Map.put(user, :name, “Bar”)</strong></p><p>It can also be written using a pipe:</p><p><strong>user |&gt; Map.put(:name, “Bar”)</strong></p><p><strong>To pipe or not to pipe<br></strong>I don’t know about you, but when I have a choice of two ways to achieve the same result I end up trying out both and asking myself <em>“which choice makes the most logical sense in this situation?”.</em> An internal bike-shedding war may or may not take place (<em>ahem</em>) and I end up going with my gut feeling.</p><p>Fast-forward a few weeks and my code is littered with both implementations causing me to re-evaluate my pipe decision making strategy.</p><p><strong>What if I never use pipe?<br></strong>Longer transformations would need lots of intermediate variables… no thanks.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ba26b5279bf11ee27b6849f2ac26037/href">https://medium.com/media/4ba26b5279bf11ee27b6849f2ac26037/href</a></iframe><p><strong>What if I only use pipe when my function expands to more than one transformation?<br></strong>Seems logical, lets have a look at it in action.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e5e9c5303bf4269667bbd70a0e2eec2/href">https://medium.com/media/2e5e9c5303bf4269667bbd70a0e2eec2/href</a></iframe><p>The conversion is simple to perform, move the `user` from the parentheses to the start of the line and add a `|&gt;`.</p><p>This is the strategy I was using and it worked out pretty well apart from a couple of niggling caveats that seemed minor then became harder to ignore.</p><ul><li>The focus of the transformation `user` is harder to see when used in the parentheses along with other parameters</li><li>Its slightly harder to expand the pipeline, compared to starting out using pipe operator, and I might perform the conversion many times everyday</li></ul><p><strong>What if I always use the pipe?</strong></p><ul><li>Clear indication that a transformation is taking place and the focus of the transformation is on the left of the pipe operator</li><li>Removes <em>‘To pipe or not to pipe’</em> decision making</li><li>Consistent code</li><li>Easy to re-order pipeline transformations</li><li>Easy to add / remove pipeline transformations</li><li>Easy to re-use pipeline transformations</li><li>Others…</li></ul><blockquote><em>“Seems silly to use the pipe to feed the collection into the first map call. Otherwise pretty nifty, I’ve been enjoying learning Elixir the last few months.” — hacker news comment</em></blockquote><p>This post was partially inspired by the comment above. From my perspective its perfectly reasonable to feed the collection into the first map call!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9281093958a6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>