VS Code php tag autocomplete

后端 未结 7 2233
刺人心
刺人心 2020-12-23 11:47

kind of trivial question but here it goes. I can\'t figure out why VS Code autocompletes (with tab) all html tags correctly while php tag not. When i type \"php\" and hit t

相关标签:
7条回答
  • 2020-12-23 12:03

    I would elaborate on Lane's answer by putting the final cursor represented by $0 in a new line between the tags. In the snippet, each comma inside the "body" array represents a new line.

    So in Visual Sutiio Code go to File > Preferences > User Snippets search for "html.json" and edit it by pasting this:

    {
      // Place your snippets for html here. 
      "php": {
        "prefix": "php",
        "body": ["<?php", "$0", "?>"],
        "description": "php tag"
      }
    }
    
    0 讨论(0)
  • 2020-12-23 12:05

    this hack worked for me: go to 'Preferences: Open User Snippets', and paste this snippet into the html (yes, that's html) user snippets:

    "php": {
        "prefix": "php",
        "body": [
            "<?php $1 ?>"
        ],
        "description": "php tag"
    }
    
    0 讨论(0)
  • 2020-12-23 12:08

    I prefer a custom keyboard shortcut added to keybindings.json - this lets you add php tags in places where intellisense doesn't work properly, like inside the quotes of html attributes. You can also select some text (maybe you copied some php from elsewhere without the full php tags) and automatically wrap it in php tags. Here is what I use:

    {
      "key": "cmd+alt+ctrl+p",
      "command": "editor.action.insertSnippet",
      "when": "editorTextFocus",
      "args": {
        "snippet": "<?php $1$TM_SELECTED_TEXT$0 ?>"
      }
    }
    
    0 讨论(0)
  • 2020-12-23 12:09

    As I understand from your question you haven't added the external PHP IntelliSense extension and using the default PHP IntelliSense provided by the VS Code, If that's the situation you need to add the extension to your Visual Studio Code Editor.

    To add the extension use keyboard and press Ctrl+Shift+p you will get the command palette and type the following command Extensions: Install Extension then on to the left panel of the editor you will get the option to search for extensions, you may search for PHP IntelliSense and install it. Editor will ask to restart the editor to apply the extension.

    I recommend to disable VS Code's built-in PHP IntelliSense by adding the following property "php.suggest.basic":false to the settings json config of the editor (keyboard press Ctrl+, you will get the settings json to the right of the editor) to avoid duplicate suggestions.

    For PHP IntelliSense to work You need to have atleast PHP 7 installed, You can either add it to your PATH or set the "php.executablePath":"php physical path" in the settings json (keyboard press Ctrl+, you will get the settings json)

    0 讨论(0)
  • 2020-12-23 12:11

    maybe you should install PHP Awesome Snippetts extension.

    0 讨论(0)
  • 2020-12-23 12:18

    In Visual Studio Code, go to File > Preferences > User Snippets > html.json file and then paste the following code:

    "php": {
        "prefix": "php",
        "body": [
            "<?php $1 ; ?>",
            "$2"
        ],
        "description": "php tag"
    }
    
    0 讨论(0)
提交回复
热议问题