Class: Warped::Emails::Base
- Inherits:
-
Object
- Object
- Warped::Emails::Base
- Defined in:
- lib/warped/emails/components/base.rb
Overview
Base class for all email components
This class provides a number of helper methods for building email components.
Usage
To create a new component, you can subclass this class and implement the template
method.
class MyComponent < Warped::Emails::Base
def template
content_tag(:div, "Hello, World!")
end
end
You can then render the component using the render
method in your mailer views
<%= render MyComponent.new %>
Variants
Components can define variants using the variant
method. Variants are different styles for the component that can be used to change the appearance of the component.
class MyComponent < Warped::Emails::Base
variant do
base { "color: red;" }
size do
sm { "font-size: 12px;" }
md { "font-size: 16px;" }
lg { "font-size: 20px;" }
end
end
default_variant size: :md
def initialize(size: nil)
super()
@size = size
end
def template
content_tag(:div, style: style())
end
end
You can then specify the variant to use using the style
method in your component
<%= render MyComponent.new %> # => <div style="color: red; font-size: 16px;"></div>
<%= render MyComponent.new(size: :lg) %> # => <div style="color: red; font-size: 20px;"></div>
The variant blocks can return either a string or an array of strings. If an array is returned, the strings will be joined with a semi-colon.
The default_variant
method can be used to specify the default variant to use if none is specified.
The style
method can be used to apply the variant styles to the component, or to apply additional styles on top of the variant styles.
Slots
Components can define slots using the slot
method. Slots are placeholders for content that can be passed in from the outside.
class MyComponent < Warped::Emails::Base
slot :title
def template
content_tag(:div, title)
end
end
You can then pass content to the slot using the with_<slot_name> method in your mailer views
<%= render MyComponent.new do |my_component| %>
<% my_component.with_title("Hello, World!") %>
<% end %>
Direct Known Subclasses
Align, Button, Divider, Heading, Layouts::Columns, Layouts::Cta, Layouts::Main, Spacer, Stepper, Table, Text
Instance Attribute Summary collapse
-
#view_context ⇒ Object
readonly
Returns the value of attribute view_context.
Instance Method Summary collapse
Methods included from Styleable
#base_styles, #default_variants, included, #style, #variants
Methods included from Slottable
Instance Attribute Details
#view_context ⇒ Object (readonly)
Returns the value of attribute view_context.
89 90 91 |
# File 'lib/warped/emails/components/base.rb', line 89 def view_context @view_context end |
Instance Method Details
#content ⇒ Object
98 99 100 |
# File 'lib/warped/emails/components/base.rb', line 98 def content @content_block end |
#helpers ⇒ Object
102 103 104 105 106 |
# File 'lib/warped/emails/components/base.rb', line 102 def helpers return view_context if view_context.present? raise ArgumentError, "helpers cannot be used during initialization, as it depends on the view context" end |
#render_in(view_context, &block) ⇒ Object
108 109 110 111 112 113 |
# File 'lib/warped/emails/components/base.rb', line 108 def render_in(view_context, &block) @view_context = view_context @content_block = capture { block.call(self) } if block_given? template end |
#template ⇒ Object
94 95 96 |
# File 'lib/warped/emails/components/base.rb', line 94 def template raise NotImplementedError end |