Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between snippet and template?

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?

like image 851
Safran Ali Avatar asked Aug 24 '11 14:08

Safran Ali


People also ask

What is an example of a snippet?

The definition of a snippet is a small piece of something. When you overhear only a small bit of information, this is an example of a snippet of information.

What is a snippet?

Definition of snippet : a small part, piece, or thing especially : a brief quotable passage.

What is the use of snippet?

Snippets are short, reusable text blocks that can be used on contact, company, deal and ticket records, in email templates, in chat conversations, and when logging an activity or note. If you want to create reusable emails, learn more about the templates tool.

How do I add a snippet to a template?

Clicking Snippet brings up a window that shows which snippets are available in your project. In this window, select a snippet and click Copy. Then, paste the snippet in your template (or click Insert Snippet, a button available in some scenarios), and save it.


1 Answers

Snippets usually refer to code snippets which you see in your intellisense dropdown (or Right click > Insert Snippet such as SurroundsWith, 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.

like image 115
Mrchief Avatar answered Sep 20 '22 13:09

Mrchief