VS Code 自定义代码片段
在 VS Code 属性设置中找到 User Snippets
,新建全局代码片段。
例如:创建名为 flask-jinja.code-snippets
自定义全局代码片段
{
// Place your global snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
// Placeholders with the same ids are connected.
// Example:
// "Print to console": {
// "scope": "javascript,typescript",
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"gettext": {
"scope": "html",
"prefix": "_",
"body": "{{ _('$1') }}",
"description": "create a gettext code"
},
"variable": {
"scope": "html",
"prefix": "jv",
"body": "{{ $1 }}",
"description": "echo a variable"
},
"expression": {
"scope": "html",
"prefix": "js",
"body": "{% $1 %}",
"description": "echo a expression"
},
"url_for": {
"scope": "html, python",
"prefix": "url",
"body": "url_for('$1')",
"description": "echo a url_for()"
},
"if statement": {
"scope": "html",
"prefix": "if",
"body": [
"{% if $1 %}",
"$0",
"{% endif %}"
],
"description": "echo a if control structure"
},
"for statement": {
"scope": "html",
"prefix": "for",
"body": [
"{% for $1 in $2 %}",
"$0",
"{% endfor %}"
],
"description": "echo a for structure"
}
}
编辑器输入 jv
就可以快速格式化一个 {{ $1 }}
Jinja2 变量打印片段。