Home > Back-end >  Visual Studio templating - conditionally hide parameter
Visual Studio templating - conditionally hide parameter

Time:09-27

I am attempting to create a custom Visual Studio project template and I have a template.json. What I am trying to achieve is to hide / disable the DoStuff parameter from the Visual Studio create project wizard if another parameter (in my case, ProjectType) was equal to something specific. It would essentially be something like the Docker OS parameter from the default Visual Studio API template.

VS create project wizard

As you can see, by default the dropdown (in my case, it would be a checkbox) is hidden / disabled, but if I check Enable Docker, it can be selected.

Below is my current template.json file which I can't seem to get right to have this feature.

{
    "$schema": "http://json.schemastore.org/template",
    "symbols": {
      "ProjectType": {
        "type": "parameter",
        "datatype": "choice",
        "choices": [
          {
            "choice": "Console"
          },
          {
            "choice": "API"
          }
        ],
        "defaultValue": "API",
        "description": "The type of the project you are building."
      },
      "DoStuff": {
        "type": "parameter",
        "datatype": "bool",
        "defaultValue": "false",
        // hide if ProjectType == API
      }
    }
}

I tried to combine it with ide.host.json to achieve this, but it's not working at all.

{
    "$schema": "https://json.schemastore.org/ide.host.json",
    "defaultSymbolVisibility": true,
    "order": 2,
    "icon": "icon.png",
    "symbolInfo": [
      {
        "id": "DoStuff",
        "isVisible": "(ProjectType == \"API\")"
      }
    ]
}

CodePudding user response:

After some more investigation and contacting Sayed Ibrahim Hashimi (sayedihashimi) on Twitter, it turns out this is currently not supported and there's likely no way to achieve it right now in .NET 6 and prior releases.

However, as per Chet Husk's (@ChetHusk) template engine upgrades comment (which redirects you to Github:dotnet/templating/docs/Conditions.md), templating conditions will be a feature added to .NET 7. The dotnet new cli already supports it, but the Visual Studio support has not yet been started on.

  • Related