1.背景
在用bootstrap框架,寫前端代碼時,由於每個頁面都有固定的模板格式,比如都包含CDN等,所以在每次寫代碼的時候,都要重復寫這些,效率比較低下。幸運的是,VScode中可以編輯各個語言的自定義模板,編輯完畢后,只需要寫出“prefix"部分,就可以自動補全自定義編輯的部分。
2.具體操作
在VScode中,file--preferences--user snippets,然后選擇html,顯示如下:
也可以粘貼做好的:
"Print to console": { //快捷鍵 "prefix": "bs3-HTML5", //bootstrap的模塊代碼 "body": [ "<!DOCTYPE html>", "<html lang=\"\">", "<head>", "\t<meta charset=\"utf-8\">", "\t<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">", "\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">", "\t<title>Title Page</title>", "\t<!-- Bootstrap CSS -->", "\t<link href=\"https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css\" rel=\"stylesheet\">", "\t<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->", "\t<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->", "\t<!--[if lt IE 9]>", "\t<script src=\"https://oss.maxcdn.com/libs/html5shiv/3.7.3/html5shiv.js\"></script>", "\t<script src=\"https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js\"></script>", "\t<![endif]-->", "</head>", "<body>", "<h1 class=\"text-center\">Hello </h1>", "<!-- jQuery -->", "\t<script src=\"https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js\"></script>", "<!-- Bootstrap JavaScript -->", "<script src=\"https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js\"></script>", " </body>", "</html>" ], //說明簡介 "description": "Bootstrap-3.3.7 "
然后,在空的html中,敲出prefix下的代碼,即
”bs3-HTML5“,
點擊“bs3-HTML5",則
3.拓展
由此想到,能不能在python中自定義一些模板,比如pycomcad中,常用的幾個導入語句?
prefix是”pycomcad",在空py文件中,敲出"pycomcad",點擊“pycomcad",則
表明,模板調用成功!