Module: CamaleonCms::ShortCodeHelper

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



121
122
123
# File 'app/helpers/camaleon_cms/short_code_helper.rb', line 121

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 = 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



109
110
111
112
113
114
115
# File 'app/helpers/camaleon_cms/short_code_helper.rb', line 109

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"; } )



134
135
136
137
138
139
# File 'app/helpers/camaleon_cms/short_code_helper.rb', line 134

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 renders "shortcode_templates/" Also can be a function to execute that instead a render, sample: lambda{|attrs, args| return "my custom content" } descr: description for shortcode



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

def shortcode_add(key, template = nil, descr = '')
  shortcode_keys << key
  CurrentRequest.shortcodes_template = shortcode_templates.merge({ key.to_s => template }) if template.present?
  shortcode_descriptions[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/"



91
92
93
# File 'app/helpers/camaleon_cms/short_code_helper.rb', line 91

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

#shortcode_delete(key) ⇒ Object

Delete the shortcode key: chortcode key to delete



97
98
99
# File 'app/helpers/camaleon_cms/short_code_helper.rb', line 97

def shortcode_delete(key)
  shortcode_keys.delete(key)
end

#shortcodes_descriptionsObject



145
146
147
# File 'app/helpers/camaleon_cms/short_code_helper.rb', line 145

def shortcodes_descriptions
  shortcode_descriptions
end

#shortcodes_initObject

Internal method



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
67
68
69
70
71
72
73
74
# File 'app/helpers/camaleon_cms/short_code_helper.rb', line 4

def shortcodes_init
  CurrentRequest.shortcodes = []
  CurrentRequest.shortcodes_template = {}
  CurrentRequest.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 do |attrs, args|
      return args[:shortcode] if attrs.blank?

      cama_load_libraries(*attrs['data'].to_s.split(','))
      ''
    end,
    "Permit to load libraries on demand, sample: [load_libraries data='datepicker,tinymce']"
  )
  # rubocop:disable Layout/LineLength
  shortcode_add(
    'asset',
    lambda do |attrs, args|
      return args[:shortcode] if attrs.blank?

      url = shortcode_asset_reference(attrs['file'], as_path: attrs['as_path'].present?)
      if attrs['image'].present?
        ActionController::Base.helpers.image_tag(url, class: attrs['class'], style: attrs['style'])
      else
        url
      end
    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',
    ->(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

#shortcodes_listObject



141
142
143
# File 'app/helpers/camaleon_cms/short_code_helper.rb', line 141

def shortcodes_list
  shortcode_keys
end