Skip to content

Commit

Permalink
Template from configuration or file (#48)
Browse files Browse the repository at this point in the history
* Allow templates from configuration and templates to both be used
* Updated readme
  • Loading branch information
toddnestor authored and kernow committed Jun 1, 2017
1 parent 93c6a4e commit 1c56139
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 26 deletions.
11 changes: 5 additions & 6 deletions README.md
Expand Up @@ -139,10 +139,7 @@ shortcode.setup do |config|
end
```

If the `templates` config option is set all templates will be loaded from this hash, if a shortcode is encountered without a matching key in the `templates` config option
an exception will be raised.

Note: it's NOT possible to load templates from a config option AND from the file system, you must either load all templates from the file system or define all templates in a config option.
Note: Templates can be loaded from either the file system or the configuration templates. If `check_config_templates_first` is set to true (the default value) on the configuration then it will check configuration templates first, and file system templates if it doesn't find one. If `check_config_templates_first` is set to false on the configuration it will check for a file system template first, and then configuration templates if it doesn't find one. If it doesn't find a template in either spot then it will raise an error.

### Custom Helpers

Expand Down Expand Up @@ -257,10 +254,12 @@ shortcode.setup do |config|
# location of the template files, default is "app/views/shortcode_templates"
config.template_path = "support/templates/erb"

# a hash of templates passed as strings, if this is set it overrides the
# above template_path option. The default is nil
# a hash of templates passed as strings.
config.templates = { gallery: 'template code' }

# a boolean option to set whether configuration templates are checked first or file system templates
config.check_config_templates_first = true

# an array of helper modules to make available within templates
config.helpers = [CustomerHelper]

Expand Down
22 changes: 13 additions & 9 deletions lib/shortcode/configuration.rb
Expand Up @@ -8,6 +8,9 @@ class Shortcode::Configuration
# Allows templates to be set from strings rather than read from the filesystem
attr_accessor :templates

# Allows setting whether templates on the configuration are checked first, or templates in the file system
attr_accessor :check_config_templates_first

# Assigns helper modules to be included in templates
attr_accessor :helpers

Expand All @@ -33,15 +36,16 @@ def self_closing_tags=(self_closing_tags)
attr_accessor :use_attribute_quotes

def initialize
@template_parser = :erb
@template_path = "app/views/shortcode_templates"
@templates = nil
@helpers = []
@block_tags = []
@self_closing_tags = []
@attribute_quote_type = '"'
@use_attribute_quotes = true
@presenters = {}
@template_parser = :erb
@template_path = "app/views/shortcode_templates"
@templates = {}
@check_config_templates_first = true
@helpers = []
@block_tags = []
@self_closing_tags = []
@attribute_quote_type = '"'
@use_attribute_quotes = true
@presenters = {}
end

def register_presenter(presenter)
Expand Down
30 changes: 20 additions & 10 deletions lib/shortcode/tag.rb
Expand Up @@ -7,11 +7,10 @@ def initialize(name, configuration, attributes=[], content='', additional_attrib
end

def markup
return markup_from_config unless configuration.templates.nil?
template_files.each do |path|
return File.read(path) if File.file? path
end
raise Shortcode::TemplateNotFound, "Searched in:", template_files
template = first_priority_template
template = second_priority_template if template.nil?
return template unless template.nil?
raise Shortcode::TemplateNotFound, "No template found for #{@name} in configuration or files"
end

def render
Expand All @@ -35,12 +34,23 @@ def render_template
end
end

def markup_from_config
if configuration.templates.has_key? @name.to_sym
configuration.templates[@name.to_sym]
else
raise Shortcode::TemplateNotFound, "configuration.templates does not contain the key #{@name.to_sym}"
def first_priority_template
configuration.check_config_templates_first ? markup_from_config : markup_from_file
end

def second_priority_template
configuration.check_config_templates_first ? markup_from_file : markup_from_config
end

def markup_from_file
template_files.each do |path|
return File.read(path) if File.file? path
end
nil
end

def markup_from_config
configuration.templates[@name.to_sym]
end

def template_files
Expand Down
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -22,7 +22,8 @@
Shortcode.setup do |config|
config.template_parser = :erb
config.template_path = File.join File.dirname(__FILE__), "support/templates/erb"
config.templates = nil
config.templates = {}
config.check_config_templates_first = true
config.block_tags = [:quote, :collapsible_list, :item, :timeline_person, :rails_helper, :custom_helper]
config.self_closing_tags = [:timeline_event, :timeline_info]
config.attribute_quote_type = '"'
Expand Down
18 changes: 18 additions & 0 deletions spec/tag_spec.rb
Expand Up @@ -65,4 +65,22 @@

end

context "when templates exist both in configuration and file system" do
let(:tag) { Shortcode::Tag.new('quote', configuration ) }

before(:each) do
configuration.templates[:quote] = '<p><%= @content %></p>'
end

it 'uses the configuration template when check_config_templates_first is true' do
expect(tag.markup).to eq('<p><%= @content %></p>')
end

it 'uses the file system template when check_config_templates_first is false' do
configuration.check_config_templates_first = false

expect(tag.markup).to eq(File.open('spec/support/templates/erb/quote.html.erb').read)
end
end

end

0 comments on commit 1c56139

Please sign in to comment.