0

I have an HTML snippet which adds a Label and Input element. But when I invoke the snippet, it doesn't let me alter the literal's, it just adds the tags with default values.

I made a different snippet for C# and that worked as expected ($value$), but even though I've copied the code it doesn't work.

Result when invoking the snippet:

<label for="MyInput">My Label</label> <input type="text" id="MyInput" name="MyInput" /> 

The snippet:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Shortcut>sltinput</Shortcut>
      <Keywords>
        <Keyword>Html</Keyword>
        <Keyword>input</Keyword>
        <Keyword>label</Keyword>
      </Keywords>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>Input and Label element</Title>
      <Author>Stein Lundbeck</Author>
      <Description>Creates an input and matching label</Description>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>name</ID>
          <ToolTip>Name of element</ToolTip>
          <Default>MyInput</Default>
        </Literal>
        <Literal Editable="true">
          <ID>label</ID>
          <ToolTip>Label content</ToolTip>
          <Default>My Label</Default>
        </Literal>
        <Literal Editable="true">
          <ID>type</ID>
          <ToolTip>Input type</ToolTip>
          <Default>text</Default>
        </Literal>
      </Declarations>
      <Code Language="HTML" Delimiter="$"><![CDATA[<label for="$name$">$label$</label> <input type="$type$" id="$name$" name="$name$" /> $end$]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

2 Answers 2

1

That’s just how VS’s HTML editor works—it doesn’t support editable $…$ placeholders, so it always pastes your defaults. Those tab stops only work in the C#/VB/XML editors.

Quick fixes:

  • Use XML/ASPX: Change <Code Language="HTML"><Code Language="XML"> and run it in an .aspx oOR.xml file.

  • Install Web Essentials: It adds full snippet support to the HTML editor.

  • Switch to VS Code: Its HTML snippets honor placeholders out of the box.

Hope that helps! :)

Sign up to request clarification or add additional context in comments.

3 Comments

ah ok, I didn't know. I work with .cshtml files. How is the support for .cshtml files in VS Code?
VS Code actually does a pretty good job with .cshtml files once you’ve got the C# for Visual Studio Code (OmniSharp) and Razor Language Server extensions installed. Snippet placeholders work reliably there , just add your custom snippets in a razor.json (or html.json) under User Snippets, and you’ll get true tab-stop editing in your Razor views......
Snippets are so much better in VS Code, and it works without too much import or adding snippet files. Thanks for the tip! Earlier I suggested for the VS team to support more variables like time, filename, namespace, class, or interface name etc. Now it's very limited, specially versus VS Code
1

Answer

VS Code: Use JSON Snippet

To create a working, editable HTML snippet in VS Code, follow these steps:

Step 1: Open Snippet File

Open Command Palette: Ctrl + Shift + P

Type: Preferences: Configure User Snippets

Select: html.json

Step 2: Paste This Code

Paste the following inside the html.json snippet file:

{
  "HTML Input and Label": {
    "prefix": "sltinput",
    "body": [
      "<label for=\"${1:MyInput}\">${2:My Label}</label>",
      "<input type=\"${3:text}\" id=\"${1}\" name=\"${1}\" />"
    ],
    "description": "Creates an input and matching label"
  }
}

Step 3: Test the Snippet

Create or open an index.html file

Type: sltinput

Press Tab

You should see:

<label for="MyInput">My Label</label>
<input type="text" id="MyInput" name="MyInput" />

You can now tab through and edit:

Input ID

Label text

Input type

Screenshot of the implementation :: https://prnt.sc/uI9MC4wxZEPY, https://prnt.sc/4iLatAtG2hcj

1 Comment

Thanks, I've already moved my HTML and razor snippets to VS Code. It's strange that VS has so limited support for snippets (and VS Code is completely different).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.