{"id":201,"date":"2016-07-25T09:06:26","date_gmt":"2016-07-25T13:06:26","guid":{"rendered":"https:\/\/mberlove.com\/blog\/?p=201"},"modified":"2016-07-23T23:16:43","modified_gmt":"2016-07-24T03:16:43","slug":"3-tips-tricks-for-slim-ruby","status":"publish","type":"post","link":"https:\/\/mberlove.com\/blog\/3-tips-tricks-for-slim-ruby\/","title":{"rendered":"3 Tips &#038; Tricks for Slim (Ruby)"},"content":{"rendered":"<p>For those coming to the Slim template engine from other tools, or from nothing at all in this area, getting acquainted with Slim can prove daunting &#8212; it did for me!<\/p>\n<p>&nbsp;<\/p>\n<p>Fortunately, it&#8217;s the kind of syntactic methodology that succumbs quickly to a little hard and fast learning. Personally, I ran across a few &#8220;gotchas&#8221; that I want to share lest you run across them yourself. Knowing these beforehand will save you time, effort, and frustration.<\/p>\n<p>&nbsp;<\/p>\n<h1>Inline conditionals<\/h1>\n<p>Getting logic on the same line as an element can be tricky, especially if it&#8217;s more than one straightforward directive. For conditionals, you can employ the ternary operator. Here&#8217;s how to do that with Slim:<\/p>\n<pre class=\"default prettyprint prettyprinted\"><code><span class=\"pun\">.<\/span><span class=\"pln\">item <\/span><span class=\"kwd\">class<\/span><span class=\"pun\">=(<\/span><span class=\"pln\">condition <\/span><span class=\"pun\">?<\/span> <span class=\"str\">'outcome-1-class'<\/span> <span class=\"pun\">:<\/span> <span class=\"str\">'outcome-2-class'<\/span><span class=\"pun\">)\r\n <\/span><span class=\"pln\"> .subitem\r\n<\/span><\/code><\/pre>\n<p class=\"default prettyprint prettyprinted\">Obviously this method can be applied to more than just classes.<\/p>\n<p class=\"default prettyprint prettyprinted\"><a href=\"http:\/\/stackoverflow.com\/questions\/13967324\/slim-langs-if-else-nested-code\">See the original solution on StackOverflow<\/a>.<\/p>\n<h1 class=\"default prettyprint prettyprinted\">HTML from a string<\/h1>\n<p class=\"default prettyprint prettyprinted\">Slim allows you to embed plain HTML directly inside its own syntax. If your code is coming from an external source in the form of a string, however, it won&#8217;t render, due to Rails treating the string as potentially malicious, and escaped. You can avoid this simply by running it through html_safe, as:<\/p>\n<pre class=\"default prettyprint prettyprinted\">codeString.html_safe<\/pre>\n<h1 class=\"default prettyprint prettyprinted\">Text inside a tag<\/h1>\n<p class=\"default prettyprint prettyprinted\">Setting up the alignment of your content just right so that Slim recognizes your intent is no simple feat for the uninitiated. Placing nested or complex content in a link tag in particular gave me serious difficulty.<\/p>\n<p class=\"default prettyprint prettyprinted\">The basic outline goes something like this:<\/p>\n<pre class=\"lang-rb prettyprint prettyprinted\"><code><span class=\"pln\">a href<\/span><span class=\"pun\">=<\/span><span class=\"str\">\"location\"<\/span><span class=\"pln\"> span <\/span><span class=\"kwd\">class<\/span><span class=\"pun\">=<\/span><span class=\"str\">\"item\"<\/span> <span class=\"pun\">|<\/span> <span class=\"typ\">Something to say<\/span><span class=\"pln\"> span <\/span><span class=\"kwd\">class<\/span><span class=\"pun\">=<\/span><span class=\"str\">\"item-other\"\r\n | Something else to say\r\n<\/span><\/code><\/pre>\n<p class=\"lang-rb prettyprint prettyprinted\"><a href=\"http:\/\/stackoverflow.com\/questions\/13514660\/nesting-a-text-inside-a-tag-in-slim\">See the original on StackOverflow<\/a>.<\/p>\n<p class=\"lang-rb prettyprint prettyprinted\">As usual, riff on this as you require.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For those coming to the Slim template engine from other tools, or from nothing at all in this area, getting acquainted with Slim can prove daunting &#8212; it did for me! &nbsp; Fortunately, it&#8217;s the kind of syntactic methodology that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[125],"tags":[160,72,161,41,162,52],"class_list":["post-201","post","type-post","status-publish","format-standard","hentry","category-programming","tag-html","tag-ruby","tag-slim","tag-software-development","tag-templating","tag-tips-and-tricks"],"_links":{"self":[{"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/posts\/201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/comments?post=201"}],"version-history":[{"count":6,"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/posts\/201\/revisions"}],"predecessor-version":[{"id":207,"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/posts\/201\/revisions\/207"}],"wp:attachment":[{"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/media?parent=201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/categories?post=201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/tags?post=201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}