Module: CamaleonCms::ShortCodeHelper

Included in:
CamaleonController
Defined in:
app/helpers/camaleon_cms/short_code_helper.rb

Instance Method Summary collapse

Instance Method Details

#cama_strip_shortcodes(text) ⇒ Object

remove all shortcodes from text Arguments

text: String that contains shortcodes

return String



112
113
114
# File 'app/helpers/camaleon_cms/short_code_helper.rb', line 112

def cama_strip_shortcodes(text)
  text.gsub(/#{cama_reg_shortcode}/, "")
end

#do_shortcode(content, args = {}) ⇒ Object

run all shortcodes in the content content: (string) text to find a short codes args: custom arguments to pass for short codes render, sample: my_model, a: true

if args != Hash, this will re send as args = {owner: args}
args should be include the owner model who is doing the action to optimize DB queries

sample: do_shortcode(“hello [greeting ‘world’]!”, @my_post) ==> “hello world!” sample: do_shortcode(“hello [greeting ‘world’]”, ‘asd’, owner: @my_post) return rendered string



100
101
102
103
104
105
106
# File 'app/helpers/camaleon_cms/short_code_helper.rb', line 100

def do_shortcode(content, args = {})
  args = {owner: args} unless args.is_a?(Hash)
  content.scan(/#{cama_reg_shortcode}/) do |item|
    content = _cama_replace_shortcode(content, item, args)
  end
  content
end

#render_shortcode(text, key, template = nil) ⇒ Object

render direct a shortcode text: text that contain the shortcode key: shortcode key template: template to render, if nil this will render default render file Also can be a function to execute that instead a render, sample: lambda{|attrs, args| return “my custom content”; } render_shortcode(“asda dasdasdas[owen a=‘1’] [bbb] sdasdas dasd as das[owen a=213]”, “owen”, lambda{|attrs, args| puts attrs; return “my test”; })



122
123
124
125
126
127
# File 'app/helpers/camaleon_cms/short_code_helper.rb', line 122

def render_shortcode(text, key, template = nil)
  text.scan(/#{cama_reg_shortcode(key)}/).each do |item|
    text = _cama_replace_shortcode(text, item, {}, template)
  end
  text
end

#shortcode_add(key, template = nil, descr = '') ⇒ Object

add shortcode key: shortcode key template: template to render, if nil will render “shortcode_templates/<key>” Also can be a function to execute that instead a render, sample: lambda{|attrs, args| return “my custom content”; } descr: description for shortcode



73
74
75
76
77
# File 'app/helpers/camaleon_cms/short_code_helper.rb', line 73

def shortcode_add(key, template = nil, descr = '')
  @_shortcodes << key
  @_shortcodes_template = @_shortcodes_template.merge({"#{key}"=> template}) if template.present?
  @_shortcodes_descr[key] = descr if descr.present?
end

#shortcode_change_template(key, template = nil) ⇒ Object

add or update shortcode template key: chortcode key to add or update template: template to render, if nil will render “shortcode_templates/<key>”



82
83
84
# File 'app/helpers/camaleon_cms/short_code_helper.rb', line 82

def shortcode_change_template(key, template = nil)
  @_shortcodes_template[key] = template
end

#shortcode_delete(key) ⇒ Object

delete shortcode key: chortcode key to delete



88
89
90
# File 'app/helpers/camaleon_cms/short_code_helper.rb', line 88

def shortcode_delete(key)
  @_shortcodes.delete(key)
end

#shortcodes_initObject

Internal method



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/helpers/camaleon_cms/short_code_helper.rb', line 3

def shortcodes_init
  @_shortcodes = []
  @_shortcodes_template = {}
  @_shortcodes_descr = {}

  shortcode_add("widget", nil, "Renderize the widget content in this place.
              Don't forget to create and copy the shortcode of your widgets in appearance -> widgets
              Sample: [widget widget_key]")

  shortcode_add("load_libraries",
                lambda{|attrs, args|
                  return args[:shortcode] unless attrs.present?
                  cama_load_libraries(*attrs["data"].to_s.split(",")); return "";
                },
                "Permit to load libraries on demand, sample: [load_libraries data='datepicker,tinymce']")

  shortcode_add("asset",
                lambda{|attrs, args|
                  return args[:shortcode] unless attrs.present?
                  url = attrs["as_path"].present? ? ActionController::Base.helpers.asset_url(attrs["file"]) : ActionController::Base.helpers.asset_url(attrs["file"])
                  if attrs["image"].present?
                    ActionController::Base.helpers.image_tag(attrs["file"], class: attrs["class"], style: attrs["style"])
                  else
                    url
                  end
                },
                "Permit to generate an asset url (
                  add file='' asset file path,
                  add as_path='true' to generate only the path and not the full url,
                  add class='my_class' to setup image class,
                  add style='height: 100px; width: 200px;...' to setup image style,
                  add image='true' to generate the image tag with this url),
                sample: <img src=\"[asset as_path='true' file='themes/my_theme/assets/img/signature.png']\" /> or [asset image='true' file='themes/my_theme/assets/img/signature.png' style='height: 50px;']")

  shortcode_add("data",
                lambda{|attrs, args|
                  attrs.present? ? (cama_shortcode_data(attrs, args)) : args[:shortcode]
                },
                "Permit to generate specific data of a post.
                Attributes:
                  object: (String, defaut post) Model name: post | posttype | category | posttag | site | user |theme | navmenu
                  id: (Integer) Post id
                  key: (String) Post slug
                  field: (String) Custom field key, you can add render_field='true' to render field as html element, also you can add index=2 to indicate the value in position 2 for multitple values
                  attrs: (String) attribute name
                          post: title | created_at | excerpt | url | link | thumb | updated_at | author_name | author_url | content
                          posttype: title | created_at | excerpt | url | link | thumb | updated_at
                          category: title | created_at | excerpt | url | link | thumb | updated_at
                          posttag: title | created_at | excerpt | url | link | thumb | updated_at
                          site: title | created_at | excerpt | url | link | thumb | updated_at
                          user: title | created_at | excerpt | url | link | thumb | updated_at
                          theme: title | created_at | excerpt | url | link | thumb | updated_at
                          navmenu: title | created_at | excerpt | url | link | thumb | updated_at
                  Note: If id and key is not present, then will be rendered for current model
                Sample:
                  [data id='122' attr='title'] ==> return the title of post with id = 122
                  [data key='contact' attr='url'] ==> return the url of post with slug = contact
                  [data key='contact' attr='link'] ==> return the link with title as text of the link of post with slug = contact
                  [data object='site' attr='url'] ==> return the url of currrent site
                  [data key='page' object='posttype' attr='url'] ==> return the url of post_type with slug = page
                  [data field=icon index=2] ==> return the second value (multiple values) for this custom field with key=icon of current object
                  [data key='contact' field='sub_title'] ==> return the value of the custom_field with key=sub_title registered for post.slug = contact
                  [data object='site' field='sub_title'] ==> return the value of the custom_field with key=sub_title registered for current_site")
end