Skip to content

Mark24Code/snippet-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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.

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

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

./bin/scaffold.rb  -t react -p name:homepage,title:welcome -o dist/welcome.jsx

conventional:

--props  <key1>:<val1>,<key2>:<val2>, ....

# will transfer to

{
  :key1: val1,
  :key2: val2
  ...
}

# then you can use in template

About

snippet template generator powered by ERB

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages