Create Svelte views seamlessly in Rails applications https://svelte.rb.obl.ong
2024-08-03 21:49:06 -04:00
bin init 2024-07-26 21:32:54 -04:00
lib steep & rbs types 2024-08-03 21:49:06 -04:00
sig/lib steep & rbs types 2024-08-03 21:49:06 -04:00
test init 2024-07-26 21:32:54 -04:00
.gitignore steep & rbs types 2024-08-03 21:49:06 -04:00
actionview-svelte-handler.gemspec locals! 2024-07-27 06:25:13 -04:00
Gemfile type declarations 2024-08-03 17:21:38 -04:00
Gemfile.lock type declarations 2024-08-03 17:21:38 -04:00
LICENSE.md init 2024-07-26 21:32:54 -04:00
package-lock.json preprocess 2024-08-02 09:03:57 -04:00
package.json preprocess 2024-08-02 09:03:57 -04:00
Rakefile init 2024-07-26 21:32:54 -04:00
rbs_collection.lock.yaml steep & rbs types 2024-08-03 21:49:06 -04:00
rbs_collection.yaml steep & rbs types 2024-08-03 21:49:06 -04:00
README.md update readme 2024-08-02 09:05:49 -04:00
Steepfile steep & rbs types 2024-08-03 21:49:06 -04:00

Action View Svelte Handler

Copyleft License: LGPL-3.0-or-later Gem Version

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

  1. Ensure you have Node.js >=v12.16.0 and NPM in your $PATH

  2. Add the actionview-svelte-handler gem to your Gemfile by executing:

 bundle add actionview-svelte-handler 
  1. And then execute the generator:
 bin/rails generate svelte:install 
  1. Enjoy!

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/.