Module: BunBunBundle::Helpers
- Includes:
- SafeHtml
- Defined in:
- lib/bun_bun_bundle/helpers.rb
Overview
Asset helpers for use in views/templates.
Include this module in your view helpers to get access to ‘bun_asset` and tag helpers. Works with ERB, Haml, Slim, or any other templating engine.
Example (ERB):
<img src="<%= bun_asset("images/logo.png") %>">
<%= bun_js_tag("js/app.js") %>
<%= bun_css_tag("css/app.css") %>
Instance Method Summary collapse
-
#bun_asset(path) ⇒ Object
Returns the public path to an asset from the manifest.
-
#bun_css_tag(source, **options) ⇒ Object
Generates a <link> tag for a CSS entry point.
-
#bun_img_tag(source, **options) ⇒ Object
Generates an <img> tag for an image asset.
-
#bun_js_tag(source, **options) ⇒ Object
Generates a <script> tag for a JS entry point.
Instance Method Details
#bun_asset(path) ⇒ Object
Returns the public path to an asset from the manifest.
Prepends the configured public_path and asset_host:
bun_asset("js/app.js") # => "/assets/js/app.js"
bun_asset("images/logo.png") # => "/assets/images/logo-abc123.png" (production)
25 26 27 28 |
# File 'lib/bun_bun_bundle/helpers.rb', line 25 def bun_asset(path) fingerprinted = BunBunBundle.manifest[path] "#{BunBunBundle.asset_host}#{BunBunBundle.config.public_path}/#{fingerprinted}" end |
#bun_css_tag(source, **options) ⇒ Object
Generates a <link> tag for a CSS entry point.
bun_css_tag("css/app.css")
# => '<link href="/assets/css/app.css" type="text/css" rel="stylesheet">'
46 47 48 49 50 51 |
# File 'lib/bun_bun_bundle/helpers.rb', line 46 def bun_css_tag(source, **) attrs = { type: 'text/css', rel: 'stylesheet' } .merge() .merge(href: (bun_asset(source))) bun_safe(%(<link #{bun_html_attrs(attrs)}>)) end |
#bun_img_tag(source, **options) ⇒ Object
Generates an <img> tag for an image asset.
bun_img_tag("images/logo.png", alt: "Logo")
# => '<img src="/assets/images/logo.png" alt="Logo">'
58 59 60 61 62 63 |
# File 'lib/bun_bun_bundle/helpers.rb', line 58 def bun_img_tag(source, **) src = bun_asset(source) alt = .delete(:alt) || File.basename(source, '.*').tr('-_', ' ').capitalize attrs = { alt: alt }.merge().merge(src: src) bun_safe(%(<img #{bun_html_attrs(attrs)}>)) end |
#bun_js_tag(source, **options) ⇒ Object
Generates a <script> tag for a JS entry point.
bun_js_tag("js/app.js")
# => '<script src="/assets/js/app.js" type="text/javascript"></script>'
35 36 37 38 39 |
# File 'lib/bun_bun_bundle/helpers.rb', line 35 def bun_js_tag(source, **) src = bun_asset(source) attrs = { type: 'text/javascript' }.merge().merge(src: src) bun_safe(%(<script #{bun_html_attrs(attrs)}></script>)) end |