Module: Hanami::Assets::Helpers
- Defined in:
- lib/hanami/sprockets/helpers.rb
Overview
Asset helpers for use in templates
Instance Method Summary collapse
-
#asset_path(source) ⇒ String
Get the path for an asset (without base URL).
-
#asset_url(source) ⇒ String
Get the URL for an asset.
-
#image_tag(source, **options) ⇒ String
Generate an image tag.
-
#javascript_tag(*sources, **options) ⇒ String
Generate a javascript script tag.
-
#stylesheet_tag(*sources, **options) ⇒ String
Generate a stylesheet link tag.
Instance Method Details
#asset_path(source) ⇒ String
Get the path for an asset (without base URL)
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
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
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, **) if external_source?(source) build_image_tag(source, **).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, **).html_safe rescue AssetMissingError next end end # Fallback to direct path build_image_tag("#{hanami_assets.config.path_prefix}/#{source}", **).html_safe rescue AssetMissingError build_image_tag("#{hanami_assets.config.path_prefix}/#{source}", **).html_safe end end end |
#javascript_tag(*sources, **options) ⇒ String
Generate a javascript script tag
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, **) sources.map do |source| if external_source?(source) javascript_include_tag(source, **) else begin asset = hanami_assets[source + ".js"] attrs = build_javascript_attributes(asset, **) javascript_include_tag(asset.url, **attrs) rescue AssetMissingError javascript_include_tag("#{hanami_assets.config.path_prefix}/#{source}.js", **) end end end.join("\n").html_safe end |
#stylesheet_tag(*sources, **options) ⇒ String
Generate a stylesheet link tag
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, **) sources.map do |source| if external_source?(source) stylesheet_link_tag(source, **) else begin asset = hanami_assets[source + ".css"] attrs = build_stylesheet_attributes(asset, **) stylesheet_link_tag(asset.url, **attrs) rescue AssetMissingError stylesheet_link_tag("#{hanami_assets.config.path_prefix}/#{source}.css", **) end end end.join("\n").html_safe end |