Created
December 18, 2014 21:47
-
-
Save waj/c50a4a6abcb372f2c702 to your computer and use it in GitHub Desktop.
Crystal Layouts with ECR
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
macro embed_ecr(filename, io_name) | |
\{{ run("ecr/process", {{filename}}, {{io_name}}) }} | |
end | |
macro ecr_file(filename) | |
def to_s(__io__, section = nil) | |
embed_ecr {{filename}}, "__io__" | |
end | |
def to_s(__io__, &block) | |
embed_ecr {{filename}}, "__io__" | |
end | |
def to_s | |
String.build do |io| | |
to_s(io) do | |
yield io | |
end | |
end | |
end | |
end |
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 "ecr/macros" | |
class Layout | |
ecr_file "layout.ecr" | |
def render(view, io) | |
to_s(io) do |section| | |
view.to_s(io, section) | |
end | |
end | |
end | |
class View | |
macro content_for(section) | |
if section == {{section}} | |
{{yield}} | |
return | |
end | |
end | |
macro render_partial(ecr_name) | |
embed_ecr {{ecr_name}}, "__io__" | |
end | |
end | |
class Foo < View | |
ecr_file "foo.ecr" | |
def initialize(@name) | |
end | |
end | |
foo = Foo.new "world" | |
layout = Layout.new | |
layout.render(foo, STDOUT) |
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
<% content_for(:header) do %> | |
<title>Foo</title> | |
<% end %> | |
<% render_partial "partial.ecr" %> |
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
<html> | |
<header> | |
<% yield :header %> | |
</header> | |
<body> | |
<% yield %> | |
</body> | |
</html> |
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
<div>Hello <%= @name %></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment