bin | ||
lib | ||
sig/lib | ||
test | ||
.gitignore | ||
actionview-svelte-handler.gemspec | ||
Gemfile | ||
Gemfile.lock | ||
LICENSE.md | ||
package-lock.json | ||
package.json | ||
Rakefile | ||
rbs_collection.lock.yaml | ||
rbs_collection.yaml | ||
README.md | ||
Steepfile |
Action View Svelte Handler
actionview-svelte-handler
is a template handler for Action View that allows you to create Svelte views in Rails applications with ease.
Usage
Add .html.svelte
views to your application instead of .html.erb
.
Caution
This will make ERB helpers unavailable because Svelte is registered as a template handler, replacing ERB when it is used.
To pass props, use the Svelte.props
object in your controller (or set instance variables), and then access them as a store with $props
.
Example
users_controller.rb
:
def show Svelte.props[:user] = User.find_by(username: params[:username]) end
show.html.svelte
:
<script> let name = $props.user.name $: mailto = "mailto://" + $props.user.email </script> <h1>Hello, {name}</h1> <a href={mailto}>{$props.user.email}</a> <style> h1 { color: red } </style>
Server-side rendering
Server-side rendering is enabled by default, to disable it on a render-by-render basis, pass the svelte[:ssr]
local to render
like so:
render "view", locals: { svelte: { ssr: false } }
Disabling globally is covered in the Configuration.
Configuration
Configuration options can be found in config/initializers/svelte.rb
. The following is a list of options and some information:
Option with type | Description | Default |
---|---|---|
ssr <Boolean> | Global server-side rendering. Does not override the svelte[ssr] local. | true |
aliases <Hash> | Path aliases when importing from Svelte views. | See path aliases |
preprocess <Hash> | Configuration for svelte-preprocess | {} |
Path aliases
Path aliases are used as quick references from your Svelte views to key paths in your application. They are primarily utilized when importing other Svelte components (i.e. partials).
The list of default path aliases is as follows:
Alias | Resolution |
---|---|
$root | Rails.root |
$app | Rails.root.join "app" |
$views | Rails.root.join "app", "views" |
Installation
-
Ensure you have Node.js >=v12.16.0 and NPM in your
$PATH
-
Add the
actionview-svelte-handler
gem to your Gemfile by executing:
bundle add actionview-svelte-handler
- And then execute the generator:
bin/rails generate svelte:install
- Enjoy!
Copyright
Copyright (C) 2024 Software Freedom Conservancy and Action View Svelte Handler contributors
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this program. If not, see https://www.gnu.org/licenses/.