https://github.com/mark24code/snippet-template
snippet template generator powered by ERB
https://github.com/mark24code/snippet-template
Last synced: 7 months ago
JSON representation
snippet template generator powered by ERB
- Host: GitHub
- URL: https://github.com/mark24code/snippet-template
- Owner: Mark24Code
- License: bsd-2-clause
- Created: 2022-07-15T11:03:51.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-07-16T16:05:29.000Z (over 3 years ago)
- Last Synced: 2025-03-17T15:14:49.939Z (11 months ago)
- Language: Ruby
- Homepage:
- Size: 26.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Snippet Template
## features
* Snippet Template Support (base on ERB)
* Template can nest & import each other
* Template support local state & Parent template inject props
* Scaffold CLI interface support run cmd to inject files into project
# Config
1. Template Search Path
example
```ruby
SnippetTemplate::Snippet.configure do |config|
config.snippet_path = './views'
end
```
| config field | type | example |extra info |
|----|----|----|----|
|snippet_path | string\|Array| `'./snippet'` `['.', './views']`| default: `./snippet` |
# Conventional
## Snippet Class
`render` provide basic inner Class
snippet class must provide
* `initialize` method with `props` arguments
* `view` method return erb template
view template can access data from local instance attribute and props.
```ruby
require_relative './bin/render'
module SnippetTemplate
class Demo < Template
def initialize(props)
@props = props || {}
@state = "Demo"
end
def view
return %{
Hello World
data from local <%= @state %>
data from props <%= @props %>
}
end
end
end
```
## Nest Snippet Template
```ruby
require_relative './bin/render'
module SnippetTemplate
class Sub < Template
def initialize(props)
@props = props || {}
end
def view
return %{
Sub Template
Props from Parent: <%= @props[:message] %>
}
end
end
end
```
```ruby
require_relative '../render'
module SnippetTemplate
class Parent < Template
def initialize(props)
@props = props || {}
end
def view
return %{
Parent Template
<%= render(:sub, { message: "hi"}) %>
}
end
end
end
```
You can use render method to call other Snippet Template.
Well, it will search snippet template within `config.snippet_path`.
### >>> conventional: the name of snippet template file and it's Class name must be same. To make sure we can find the correct snippet. <<<
# CLI interface
try to find help
```
$./bin/scaffold.rb --help
Usage: scaffold.rb [options]
-t, --template=Template Choose snippet template
-p, --props=Props Inject props data to template
-o, --output=Output Inject files to output path
```
example
```shell
./bin/scaffold.rb -t react -p name:homepage,title:welcome -o dist/welcome.jsx
```
conventional:
```
--props :,:, ....
# will transfer to
{
:key1: val1,
:key2: val2
...
}
# then you can use in template
```