Created
November 18, 2023 05:04
-
-
Save RomanTurner/a95ea2d143e49e7d930f633a706b5aa4 to your computer and use it in GitHub Desktop.
View Components- With Haml π
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
require "haml" | |
module Hamlet | |
def self.included(base) | |
base.extend ClassMethods | |
end | |
def render | |
engine = Haml::Template.new { self.class.get_element } | |
engine.render(self) | |
end | |
module ClassMethods | |
def element(doc) | |
@element = doc | |
end | |
def get_element | |
@element | |
end | |
end | |
end | |
class TryHamlet | |
include Hamlet | |
def initialize(title) | |
@title = title | |
end | |
def home_url | |
"www.home.com" | |
end | |
element <<~TEMPLATE | |
#content | |
.title | |
%h1= @title | |
%a(title=@title href=home_url) Stuff'Home' | |
TEMPLATE | |
end | |
component = TryHamlet.new("Teen Wolf") | |
puts component.render |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment