Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(): conditional render tag #10

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/comfortable_mexican_sofa/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ module ComfortableMexicanSofa::Content
require_relative "content/tags/helper"
require_relative "content/tags/partial"
require_relative "content/tags/template"
require_relative "content/tags/conditional_render"
35 changes: 35 additions & 0 deletions lib/comfortable_mexican_sofa/content/tags/conditional_render.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Renders image tag from http://picsum.photos
# Example: {{cms:lorem_picsum 400, 300}}
class ComfortableMexicanSofa::Content::Tag::ConditionalRender < ComfortableMexicanSofa::Content::Tag

attr_reader :condition, :if_identifier, :else_identifier

def initialize(context:, params: [], source: "")
super
@condition = params[0]
@if_identifier = params[1]
@else_identifier = params[2]

unless @if_identifier.present?
raise Error, "Need at least one dimension of the image"
end
end

def content
if condition
snippet(if_identifier).content
elsif else_identifier.present?
snippet(else_identifier).content
end
end

# Grabbing or initializing Comfy::Cms::Snippet object
def snippet(identifier)
context.site.snippets.detect { |s| s.identifier == identifier } ||
context.site.snippets.build(identifier: identifier)
end
end

ComfortableMexicanSofa::Content::Renderer.register_tag(
:conditional_render, ComfortableMexicanSofa::Content::Tag::ConditionalRender
)