片段和模板有什么区别?
尝试确定何时使用片段以及何时使用模板,因为它们似乎都能够完成相同的事情。
是性能更好还是其他更好,或者它们完全不同并且我误解了某些东西?
Trying to determine when to use a snippet and when to use a template, since they all seem to be able to accomplish the same thing.
Is one better for performance or such, or are they completely differently and I’m misunderstanding something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
片段通常是指您在智能感知下拉列表中看到的代码片段(或
右键单击>插入片段
,例如SurroundsWith
、forEach
等)。它们通常跨越几行,用于提供重复使用的代码模式的快捷方式。模板是指项目/项目模板,它可以包含代码以及其他内容,例如项目的文件结构等。将其视为代码文件或项目的脚手架。
例如,当您选择创建新的网站项目时,网站项目模板通过添加诸如
default.aspx
之类的文件和文件夹(App_Code
等)来创建典型网站的脚手架。还有所谓的 T4 模板,用于代码生成。其范围超出了几行(这通常是片段添加的内容)。您可以添加代码和控制逻辑,这使得它们比片段更强大。同时,为
forEach
之类的东西编写 T4 模板是一种矫枉过正的行为。您也不能右键单击并在编辑器中在此处插入片段。比较性能在这里并不重要,因为每个都是一次性的事情。您可以根据您想用它做什么来选择其中之一。
Snippets usually refer to code snippets which you see in your intellisense dropdown (or
Right click > Insert Snippet
such asSurroundsWith
,forEach
etc.). They usually span few lines and are used to provide shortcut to repeatedly used code patterns.Templates refer to Item/Project templates which can contain code plus other things such as file structure for a project and more). Think of it as a scaffolding for code file or a project.
E.g. WebSite project templates creates the scaffolding for a typical WebSite by adding files such as
default.aspx
and folders (App_Code
etc) when you choose to create a new website project.There is also what is known as T4 templates which are used for code generation. The scope of this goes beyond a handful of lines (which is typically what a snippet would add). You can add code and control logic which makes them way more powerful than snippets. At the same time, writing a T4 template for something like
forEach
is a overkill. You also cannot right click and say insert snippet here within the editor.Comparing performance is not relevant here as each is a one time thing. You choose one over either based on what you want to do with it.