Module: Hanami::Assets::Helpers

Defined in:
lib/hanami/sprockets/helpers.rb

Overview

Asset helpers for use in templates

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#asset_path(source) ⇒ String

Get the path for an asset (without base URL)

Parameters:

  • source (String)

    asset source

Returns:

  • (String)

    asset path

Since:

  • 0.1.0



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/hanami/sprockets/helpers.rb', line 122

def asset_path(source)
  if external_source?(source)
    source
  else
    begin
      hanami_assets[source].path
    rescue AssetMissingError
      "#{hanami_assets.config.path_prefix}/#{source}"
    end
  end
end

#asset_url(source) ⇒ String

Get the URL for an asset

Parameters:

  • source (String)

    asset source

Returns:

  • (String)

    asset URL

Since:

  • 0.1.0



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/hanami/sprockets/helpers.rb', line 102

def asset_url(source)
  if external_source?(source)
    source
  else
    begin
      hanami_assets[source].url
    rescue AssetMissingError
      "#{hanami_assets.config.path_prefix}/#{source}"
    end
  end
end

#image_tag(source, **options) ⇒ String

Generate an image tag

Parameters:

  • source (String)

    image source

  • options (Hash)

    HTML attributes

Returns:

  • (String)

    HTML img tag

Since:

  • 0.1.0



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/hanami/sprockets/helpers.rb', line 71

def image_tag(source, **options)
  if external_source?(source)
    build_image_tag(source, **options).html_safe
  else
    begin
      # Try common image extensions
      %w[.png .jpg .jpeg .gif .svg].each do |ext|
        begin
          asset = hanami_assets[source + ext]
          return build_image_tag(asset.url, **options).html_safe
        rescue AssetMissingError
          next
        end
      end

      # Fallback to direct path
      build_image_tag("#{hanami_assets.config.path_prefix}/#{source}", **options).html_safe
    rescue AssetMissingError
      build_image_tag("#{hanami_assets.config.path_prefix}/#{source}", **options).html_safe
    end
  end
end

#javascript_tag(*sources, **options) ⇒ String

Generate a javascript script tag

Parameters:

  • sources (Array<String>)

    one or more javascript sources

  • options (Hash)

    HTML attributes

Returns:

  • (String)

    HTML script tags

Since:

  • 0.1.0



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hanami/sprockets/helpers.rb', line 46

def javascript_tag(*sources, **options)
  sources.map do |source|
    if external_source?(source)
      javascript_include_tag(source, **options)
    else
      begin
        asset = hanami_assets[source + ".js"]
        attrs = build_javascript_attributes(asset, **options)
        javascript_include_tag(asset.url, **attrs)
      rescue AssetMissingError
        javascript_include_tag("#{hanami_assets.config.path_prefix}/#{source}.js", **options)
      end
    end
  end.join("\n").html_safe
end

#stylesheet_tag(*sources, **options) ⇒ String

Generate a stylesheet link tag

Parameters:

  • sources (Array<String>)

    one or more stylesheet sources

  • options (Hash)

    HTML attributes

Returns:

  • (String)

    HTML link tags

Since:

  • 0.1.0



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hanami/sprockets/helpers.rb', line 21

def stylesheet_tag(*sources, **options)
  sources.map do |source|
    if external_source?(source)
      stylesheet_link_tag(source, **options)
    else
      begin
        asset = hanami_assets[source + ".css"]
        attrs = build_stylesheet_attributes(asset, **options)
        stylesheet_link_tag(asset.url, **attrs)
      rescue AssetMissingError
        stylesheet_link_tag("#{hanami_assets.config.path_prefix}/#{source}.css", **options)
      end
    end
  end.join("\n").html_safe
end