Coder Social home page Coder Social logo

kairyou / sublimetmpl Goto Github PK

View Code? Open in Web Editor NEW
226.0 12.0 68.0 57 KB

Create File From Templates for SublimeText 2 / SublimeText 3

Home Page: https://xhl.me

License: MIT License

Python 100.00%
sublime sublime-package sublimetmpl snippets templates

sublimetmpl's Introduction

SublimeTmpl

A Sublime Text 2/3 plugin for create file from templates.

Installation

Github

Go to the "Packages" directory (Preferences / Browse Packages). Then clone this repository:

$ git clone https://github.com/kairyou/SublimeTmpl.git

Package Control

Install Package Control. Then Package Control: Install Package, look for SublimeTmpl / tmpl and install it.

Usage

  • Create file with menu File - New File (SublimeTmpl)

  • Create file with command use cmd+shift+p then look for tmpl:

Settings

Default Settings: Preferences > Packages Settings > SublimeTmpl > Settings - Default

Custom settings (*Recommend): Preferences > Package Settings > SublimeTmpl > Settings - User

Default template files: Packages/SublimeTmpl/templates

Custom template files (Recommend): Packages/User/SublimeTmpl/templates/

Project-specific template files (ST3 only): via settings: SublimeTmpl: template_folder key within your sublime-project file (see "Features added" below)

Default key bindings

ctrl+alt+h html
ctrl+alt+j javascript
ctrl+alt+c css
ctrl+alt+p php
ctrl+alt+r ruby
ctrl+alt+shift+p python

Disable shortcuts

If you’re unhappy with default keymap, you can disable keyboard shortcuts with disable_keymap_actions preference of SublimeTmpl.sublime-settings.

Use a comma-separated list of action names which default keyboard shortcuts should be disabled. For example, if you want to disable creat html and css shortcuts, your must set the following value:

"disabled_keymap_actions": "html, css"

To disable all default shortcuts, set value to all.

Features added

  • custom template files

    put your custom template files in Packages/User/SublimeTmpl/templates Project-specific template files can be specified in project files, along with overriding other templating options.

  • *.tmpl file support ${date} variable

    default "date_format" : "%Y-%m-%d %H:%M:%S" .

  • *.tmpl file support ${saved_filename}, ${saved_filepath} on save file

    Enable: SublimeTmpl > settings - user add "enable_file_variables_on_save": true,

  • *.tmpl file support ${project_base_name}, ${project_path} and ${platform}

    NOTE: This will only work with SublimeText 3.0 or above.

    Enable: SublimeTmpl > settings - user add "enable_project_variables": true,

It is recommended that you put your own custom settings in settings - user. Please see note below in "*Detailed Instructions for Sublime Newbies"

  • custom variables: attr

    custom the attr variables in settings, example:

   "attr": {
       "author": "Your Name" ,
       "email": "[email protected]",
       "link": "http://example.org",
       "hello": "word"
   }
> The `*.tmpl` file will support `${author}` `${email}` `${link}` `${hello}` variables.
  • Project specific overrides via SublimeTmpl attribute in sublime-project file.
    "settings":
    {
        "SublimeTmpl":
        {
            "template_folder": "Path\\to\\some\\custom\\template\\folder",
            "template_replace_pattern": "{{%s}}",
            "enable_project_variables": true,
            "enable_file_variables_on_save": true,
            "attr": {  // Fully overrides "attr" settings (i.e. elements missing here will not attempt to be replaced in template)
                "author": "Alternative Name"
            },
            "project_variables": {
                // Allows for use with other template formats, provide mapping here
                // "tmpl_formatted_name": "current_template_name"
                "project_base_name": "projectbase",
                "project_path": "projectpath",
                "platform": "plat"
            },
            
            "file_variables_on_save": {
                // Allows for use with other template formats, provide mapping here
                // "tmpl_formatted_name": "current_template_name"
                "saved_filename": "name",
                "saved_filepath": "filepath"
            },
        },
    },

*Detailed Instructions for Sublime Newbies

Sublime Text keeps settings in bunch of files in a folder. For example, OS X, it is located at

/Users/yourusername/Library/Application Support/Sublime Text 3/Packages

You can navigate to it easily by using the following menu item in Sublime, for example: Sublime Text>Preferences>Browse Packages

Now go to Packages\SublimeTmpl folder and open the file SublimeTmpl.sublime-settings. Save the file with the same file name under Packages\User. Delete everything except the variables that are custom to you. By the time you are finished, the file might look as simple as this (JSON format):

{
    "attr": {
        "author": "Chuck Norris" ,
        "email": "[email protected]",
    }
}

If you don't want the "link" and "hello" variables to show up, you should copy the (for example) python.tmpl file to Packages/User/SublimeTmpl/templates and remove those elements from it.

Contributors

Donate

PayPal donate button

FAQ

  • SublimeTmpl not work after update the package.

    Please open Preferences / Settings - User, remove "SublimeTmpl" from ignored_packages.

  • Alert message Replace pattern None doesn't seem to work: unsupported operand type(s) for %: 'NoneType' and 'str'

    Please restart Sublime Text and try again.


Source: https://github.com/kairyou/SublimeTmpl

Docs: 中文文档

sublimetmpl's People

Contributors

kairyou avatar oop99 avatar pjakobsen avatar vfasky avatar xenofanes9 avatar xu-cheng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sublimetmpl's Issues

Set Syntax in Custom Templates

Hi, is it possible to set the Syntax (e.g. LaTeX) for each custom template?

edit: sorry, found it. Added

    "template_name": {
    "syntax": "Packages/LaTeX/LaTeX.tmLanguage",
    "extension": "tex" // default_extension
},

in SublimeTmpl.sublime-settings.

Thanks for your plugin!

Default Keymap Not Extracting

Hi @kairyou,
Thanks for this plugin. Really tiny bug, but in sublime-tmpl.py the file_list array is missing your default keymap file. This is causing it to never be extracted from the package.

Is there any way you could fix this? (BTW, I suspect this is actually the source of the problem the user in #11 reported experiencing.)

Also, two other related things I'm noticing, having just installed this plugin:

  • When I try to open the default keymap file (which currently doesn't exist) the path that the latest ST3 opens is ~/.config/sublime-text-3/Packages/SublimeTmpl/Default (Linux).sublime-keymap. Particularly, your keymap file doesn't have the (Linux) in it.
    I'm not 100% sure if this matters or if Sublime would automagically fall back to the other path if it existed (I should probably check) and I know this should be a cross-platform plugin, but it's worth mentioning in case these files are all supposed to have the OS in them now.

  • Along the same lines -- though a slightly different quirk -- is that when choosing to open the user keymap file, the path ~/.config/sublime-text-3/Packages/User/Default (Linux).sublime-keymap is opened for me.
    I mention this because I can only assume that you need to create at least a blank file custom to SublimeTmpl to prevent it from falling back to the global. (Interestingly, for some reason this fallback doesn't occur with the user settings file, which opens as ~/.config/sublime-text-3/Packages/User/SublimeTmpl.sublime-settings, but I'm guessing that's because I don't have a file named ~/.config/sublime-text-3/Packages/User/Default (Linux).sublime-settings.)

Licence

Can you set a licence on the repo please?

Thanks 😄

Add paths.

How to add path to customized templates that is placed in User folder.

这个插件能做到实时更新么

举个例子,我公司,和家里的电脑同事打开了 ST,公司的电脑在编辑印象笔记的一个笔记本,编辑完后更新上去了, 家里的电脑没做任何操作, 如何这时候,家里的电脑更新 , 就会覆盖公司的更新,
这个时候我要看最新的数据,每次都只能重新打开笔记本,有什么办法能让家里的电脑不用重新打开笔记本,向刷新网页一样去获取公司电脑更新的最新内容么? 因为有时新开了一个笔记本,那边还有一个窗口,开着个旧的笔记本,如果不小心在开着旧笔记本的窗口按了更新,那就GG了

Why does it show the CR ?

I install it in Sublime Text3 Build 3083 by git clone the url and it works.However,when I create a html template,it shows that:

I save it named hah.html.
And then I close the file,I opened it again,it shows that:

Please fix the problem!!

ideas

this is not an issue, just my idea about this plugin. I think to be truly interesting, it should handle file creation and naming too, appearing on the menu after a right click on a specific folder.
Because right now, i don't really file any advantage to use this, rather than hit ctrl+N and launch a snippet.

Best regards

TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'

Traceback (most recent call last): File "D:\Temp\SublimeTemp\sublime_plugin.py", line 1082, in run_ return self.run(edit, **args) File "D:\Temp\SublimeTemp\Data\Packages\SublimeTmpl\sublime-tmpl.py", line 111, in run tmpl = self.get_code(type) File "D:\Temp\SublimeTemp\Data\Packages\SublimeTmpl\sublime-tmpl.py", line 205, in get_code return self.format_tag(code) File "D:\Temp\SublimeTemp\Data\Packages\SublimeTmpl\sublime-tmpl.py", line 213, in format_tag pattern = get_replace_pattern(settings) File "D:\Temp\SublimeTemp\Data\Packages\SublimeTmpl\sublime-tmpl.py", line 39, in get_replace_pattern x = replace_pattern % "test" TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'

This is what i get when i select File>New File (SublimeTmpl)>python on a fresh install.

For other ed?

Maybe plugin is easy to port to CudaText?
(Cud has dropdown near New icon, and it supports simple templates, but not vars.)

在模板中添加script标签包含js代码自定义的模板就会有问题

在html模板中添加如下代码创建的文件就变为空了:

<script> $(function(){ $("img.lazy").lazyload({ threshold : 200 }); //本专题页点击多规格跳转到产品详情页 $('.desc').on('click', function(event){ event.preventDefault(); findParent(this, 'item').find('.godetail').click(); }); function findParent(obj, str){ var $obj = $(obj); while(!$obj.hasClass(str)){ $obj = $obj.parent(); } return $obj; } }); </script>

Add steps to README for custom variables

I'm a fairly experienced dev but with the README for this package, I couldn't figure out how to get my custom templates to work. Perhaps someone can make a pull request to explain a bit more the steps to get a custom template working, and how to use custom variables.

Custom templates gets ignored

I have created a custom template, but when I create a new file from that template the files is blank.
The template is stored in User/SublimeTmpl/template as blogpost.tmpl

I also added a new item to the MENU as:
{
"caption": "BLOG POST",
"command": "sublime_tmpl",
"args": {
"type": "blogpost"
}

模板里添加上次修改时间

#23 在这个议题中,我仿照sublime-tmpl.py中的saved_filename设置了一个last_changetime变量,但on_pre_save方法只会在第一次保存时调用,我想每次保存文件都调用来记录修改时间,这个应该写在哪儿呢?

            filepath = view.file_name()
            filename = os.path.basename(filepath)
            #为了显示上次修改的时间我加了这三行代码
            time_format=settings.get('date_format','%Y-%m-%d')
            last_changetime=datetime.datetime.now().strftime(time_format)
            view.run_command('sublime_tmpl_replace', {'old': '${saved_filepath}', 'new': filepath})
            view.run_command('sublime_tmpl_replace', {'old': '${saved_filename}', 'new': filename})
            view.run_command('sublime_tmpl_replace', {'old': '${last_changetime}','new': last_changetime})

是否可以像模板引擎一样变量自定义?

我想新建一个vue模板,根据文件名给其添加类名,比如MyFile.vue添加类名my-file
在webstorm模板引擎中我是这么写的
#set($lowerName = $NAME.replaceAll("(a-zA-Z)", "$1-").toLowerCase())

<script> export default { name: '${NAME}', data () { return {} } } </script> <style lang="scss" rel="stylesheet/scss" scoped> .$lowerName { } </style>

那么在sublime中能否做到,可以的话怎么做呢?

Where is the default Key Bindings ?

It's an excellent sublime plug-in component. But I'm new, when I learn the code, I can't find the default Key Bindings, for example ''ctrl+alt+c:css", so where are those?

无法新建Md模板

你好,我在C:\Users\yuedingwagnji\AppData\Roaming\Sublime Text 3\Packages\User\SublimeTmpl\templates
这里添加了一个 md模板,然后修改了key-binder 我是参照这个的http://blog.jobbole.com/96993/ ,这4个文件我都改了,可还是不行,控制台报错如下:
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Program Files\Sublime Text 3\sublime_plugin.py", line 812, in run_
return self.run(edit, **args)
File "C:\Users\yuedingwagnji\AppData\Roaming\Sublime Text 3\Packages\SublimeTmpl\sublime-tmpl.py", line 36, in run
tmpl = self.get_code(type)
File "C:\Users\yuedingwagnji\AppData\Roaming\Sublime Text 3\Packages\SublimeTmpl\sublime-tmpl.py", line 89, in get_code
code = sublime.load_resource(self.tmpl_path)
File "C:\Program Files\Sublime Text 3\sublime.py", line 190, in load_resource
s = sublime_api.load_resource(name)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb1 in position 71: invalid start byte

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.