Last active
December 22, 2015 19:39
-
-
Save Fernan2/6520972 to your computer and use it in GitHub Desktop.
haml refactor
This file contains 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
Initial ugly version: | |
- if @post | |
%div{ class: "blog-titulo#{ ' peque' if @blog.titulo.length > 60 }" }= @blog.titulo | |
- else | |
%h1{ class: "blog-titulo#{ ' peque' if @blog.titulo.length > 60 }" }= @blog.titulo | |
Nice, but doesn't work: | |
- h1_or_div = @post ? '%div' : '%h1' | |
= eval(h1_or_div){ class: "blog-titulo#{ ' peque' if @blog.titulo.length > 60 }" }= @blog.titulo | |
Works, but breaks DRY -> Seems WET (Will Enhance This) | |
- clase = { class: "blog-titulo#{ ' peque' if @blog.titulo.length > 60 }" } | |
- if @post | |
%div{ clase }= @blog.titulo | |
- else | |
%h1{ clase }= @blog.titulo | |
EDIT: Finally, it could be like this: | |
- clase_titulo = "blog-titulo#{' peque' if @blog.titulo.length > 60}" | |
= content_tag(@post ? :div : :h1, @blog.titulo, class: clase_titulo) |
or in a cleaner way:
- clase_titulo = 'blog-titulo'
- clase_titulo += ' peque' if @blog.titulo.length > 60
= content_tag(@post ? :div : :h1, @blog.titulo, class: clase_titulo)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works and is (more or less) what I was looking for: