I am trying to set up sublime text 2 with PHP, but I am having some trouble getting the snippets to work. I have loaded a PHP project and see that the syntax is correctly set to PHP. When I go to preferences -> browse -> packages _>php , I can see a number of PHP snippets that I am unable to activate including $_files $_get ,$post. As far as I can tell, the tab trigger for these are '$_'
<snippet>
<content><![CDATA[\$_COOKIE['${1:variable}']]]></content>
<tabTrigger>$_</tabTrigger>
<scope>source.php</scope>
<description>COOKIE['…']</description>
</snippet>
but this does not generate any snippets being displayed, when I try it in a php file.
Can anyone give me advice on what to do next?
Thank you,
Bill.
Works on my Sublime Text 2, but only within the <?php ?> tags, not just in a file called .php.
Typing "$_ + TAB" within <?php ?> tags gives a list of all the $_ Snippets COOKIE, FILES, ENV, etc...
My next steps would be:
Do other Snippets work?
Did you create a Snippet that could overrule the $_ ?
Re-install ;-)
You enabled the source for php, you can only be call this snippet in your php file only.
create new file and save as somename.php
write <?php after
type $_ and hit TAB or Ctrl + Space
It generates the snippet, lets happy coding.
Related
I'm using Laravel so all the views are .blade.php files. Visual Studio Code won't format the HTML because of the PHP extension. I removed the "blade" part of the filename, but it still isn't formatting the files properly (via Alt+Shift+F).
I also tried about five extensions, but none of them do the reformatting.
How can I format .blade.php files in Visual Studio Code?
The extension Beautify (from HookyQR) just does it very well. Either add PHP, and any other file extension type, to the configuration. As said by Nico, here is an example:
Go to user settings (Ctrl + Shift + P → User settings (UI) or Ctrl + , (comma)
Search for Beautify in the field above. And click on "Edit in settings.json" for "Beautify: Config".
For the "html" section, add "php" and "blade".
###Usage
You can invoke it directly. Press F1, and then write Beautify. The auto completion gives you two choices, "Beautify file" and "Beautify selection". Choose the one you need, and it will do the job. That's a straightforward direct way.
You can also add a keybinding to have a keyboard shortcut. Here is how to do it:
Open keybindings.json (go to menu File → Preferences → Keyboard Shortcuts)
Click in the above. Open and edit file keybindings.json
Add the following into the closed brackets, []
{
"key": "alt+b",
"command": "HookyQR.beautify",
"when": "editorFocus"
}
Choose any key you want, and make sure you don't override an existing one. Search first in the left side if it exists or not.
Note that all of those things are well documented in the description of the extension.
Extra: Note about blade
(suite to #Peter Mortensen clarification pinpoint)
blade or blade.php
If you are confused if it's blade or blade.php for the setting! I'll clear it up for you! It's blade! That's vscode keyword for the language!
How do you know ?
First if you open the list of languages as by the image bellow:
Write blade
You can see Laravel Blade (blade)! The language keyword is in the paratheses! blade!
Well but how to check!
Try with blade.php in the settings!
Try to beautify
You'll get an asking context menu for what language (html, css, js)!
So it doesn't works!
To really know ! Put back blade!
And it will work and beautify directly!
How well it works with blade
The awesome answer to that! Is try it, and see for yourself!
But if you ask me! I'll say it does work as with html! It can be too handy! And you may need to fix some lines! Depending on your expectations and your style!
Here an illustration! I screwed the indentation in purpose
And here the beautification result:
Beautify (from HookyQR) does solve this problem!
Many people add "blade.php" and "php" to the HTML configuration. Beautify does not recognize and manually choose an HTML template. Instead, just adding "blade" to the HTML configuration fixes all issues.
I think Beautify (by HookyQR) might do the trick. Just add 'php' and/or 'blade.php' to the HTML section of its configuration to beautify PHP / Blade view files.
It works for me!
If you want something that just works out of the box, add the format nested HTML in PHP files support Visual Studio Code should already have.
It uses all the native settings for html.format, format on save, etc. Give the extension Format HTML in PHP a try. I made it because every other solution made me annoyed, because it didn't work 100%.
I found this extension called Format HTML in PHP that works well for me.
Andrew Schultz mentioned the extension: "Format HTML in PHP".
This is indeed a great extension for format PHP files with HTML markup. It also formats JavaScript code included in PHP files.
Moreover. Good news! The developer is already working successfully to include Laravel blade.php files. In the meantime I format Laravel blade files in Visual Studio Code by switching language manually from Blade to PHP then I use the extension "Format HTML in PHP" with Ctrl + Alt + F or right click. It works great for me.
Using a newish feature in php, vscode seems to format and highlight embedded HTML beautifully. (php 7.3, released after the question was posted) adds what are called "heredoc" and "neardoc" strings. They are well described here: https://andy-carter.com/blog/what-are-php-heredoc-nowdoc.
Basically, you put your HTML in like this:
[php code.....]
echo
<<<HTML
<div>
<h1>This is a headline</h1>
<p> this is a paragraph. lorem ipsum lorem ipsum blah black </p>
</div>
HTML;
[^ don't forget the ; follow with more php if you want]
it looks just great on screen.
I use Format HTML in PHP which works well in a file containing both HTML and PHP. However it does not appear to format a 'pure' php file such as a Class. So I have also installed Phpfmt, which does a great job with the pure php file but wrecks the indenting in a file with mixed content. Here's an example of what I get:
<?php
if (!$logged_in) {
$login_form = 'data-toggle="modal" data-target="#Modal1"';
$href = '#';
} else {
$login_form = '';
$href = 'members';
}
?>
The opening php tag is correctly aligned with the html. 'I see that under 'Supported Transformations' it says:
AlignPHPCode: Align PHP code within HTML block.
But as you can see from the above, it's not working for me. Any suggestions??
This can be done by using HTML formatting for ".blade.php":
Open Command Palette (Ctrl + Shift + P (on a PC))
Type "Preferences:Open Settings (JSON)" (this is for opening settings.json)
Add
"files.associations": {
"*.blade.php": "html",
"*.tpl": "html"
},
This will format .blade.php files as HTML, but it will not remove Blade snippets.
Is it possible to add a new custom starting tag to recognize PHP code in TextMate on Mac?
i.e. I need to use
[insert_php]
echo "Hello World"
[/insert_php]
Instead of the default code
<?php
echo "Hello World"
?>
I need that because I'm using a WordPress plugin which allows me to insert a custom php code [insert_php] code ... [/insert_php] into posts and pages but it's a bit complected code so I'm using TextMate to write it externally and then will paste it in a WP page .. so the standard PHP styling in TextMate will make my job easier.
Any help will be really appreciated.
You can adjust the grammar definitions for every language. In your case:
Open the Bundle editor
Search for PHP -> Language Grammars -> PHP
Find the block containing name = 'meta.embedded.block.php';, highlighted in this screenshot:
Duplicate it by pressing CTRL+Shift+D
Inside the new block, change begin to '\[insert_php\]'
Change end to '\[/insert_php\]', the result should look like in the second screenshot:
Now your custom begin/end tag should work.
Write your code with <? and ?> then when you're done, replace it with your custom tags. Boom.
I've just started using Komodo IDE 8.5.4 for editing a PHP project and want to use snippets to quickly type in the PHP start and end tags.
The block snippet does produce this:
<?php
?>
and the inline snippet does produce this:
<?php echo $var; ?>
The problem is these only seem to work when used within a PHP code block and not when outside one - which is precisely the opposite of how it should be.
Any suggestions for how to fix this?
Check out your toolbox (View > Tabs & Sidebars > Toolbox), you can move these snippets around there in your Abbreviations folder. Note that these are sample snippets meant for people to get started with, so please make sure you move your Abbreviations folder outside of the "Samples" folder if you intend to use it.
Also note that you can use a snippet anywhere you want, its only the auto-abbreviation part that limits it by language. You could also insert the snippet with the "Invoke" tool (Ctrl+Shift+I).
I've just started using Netbeans (7.1.2, php version) to work on a PHP project. Netbeans is really great for editing long stretches of PHP Code.
But in my view files where HTML is mixed up with short bits of PHP I am getting really tired of manually typing:
<?php ?>
In Dreamweaver you just press a button to create these tags, but in Netbeans I can't find anything like a keyboard shortcut. Surely there must be one. Does anybody know what it is?
In Netbeans I had gone to Tools -> Options -> Editor -> Code Templates and looked through all abbreviations for the PHP language. There were lots and lots of abbreviations for pieces of PHP. But the <?php ?> tags weren't there.
After typing out my question it occurred to me that the place such abbreviations would be was for the HTML language instead of PHP. I looked there and there weren't any code templates at all. But it was simple to add one of my own (abbreviation: 'p', expanded text '<?php ?>').
Now when the cursor's in the middle of some HTML, I type p[TAB] and my tags appear!
Update
Thanks to link from #chris in comment above I can change expanded text to <?php echo ${cursor} ?> and the cursor ends up where I want it.
Looked through the guide and didn't see any
http://netbeans.org/project_downloads/www/shortcuts.pdf
I don't think there is, but you can create a macro to do it yourself :)
You just have to click on Edit -> Start Macro Recording then you have to type in <?php ?> and afterwards click on Edit -> Stop Macro Recording.
There isn't one by default. The closest is typing <? and pressing ctrl+space which will suggest you <?php...
Just type php + TAB to have <?php ?> or phpe + TAB for <?php echo ?>
It's works for Netbeans 8 with default Php Package
If you want, you can change that shortcut on Tools -> Options -> Editor -> Code Templates; choose HTML on Language's select box.
I am trying to find a way to write pure PHP in my articles with Joomla (currently using 1.5). The closest I got so far was to use a plugin called Sourcerer (see link)
The issue is that the PHP code is actually being rendered as HTML. When I write the below code via the Sourcerer editor:
<?php echo "Hello world"; ?>
This is what is being written to the article:
<span><</span>?php echo "Hello world"; ?<span>></span>
I am afraid that at some point this is going to break my PHP if I come up with more complex code. Also this makes the code hardly readable. Finally the issue with Sourcerer is that you can only insert new code, you can't edit what you previously added.
Does anybody know a way to write pure PHP into articles?
I would like to insist on the fact that it has to be within articles. I already found a way to do it in modules or components, for instance with Jumi.
I have used Sourcerer a couple of times and it does allow one to insert PHP snippets into articles without issues. If your php is being rendered as html, it sounds like the php isn't getting parsed as php - first thing is to make sure that the sourcerer plugin is indeed enabled so it can allow your php scripts to get parsed. (check plugin enabled status here: extensions->plugin manager->sourcerer).
Also, use the sourcerer ("Insert Code") button at the bottom of your WYSIWYG editor when adding your PHP scripts - it keeps the formatting/syntax highlighting and ensures that it is escaped properly with the {source} {/source} tags.
An update, I have the same issue with joomla 3.x, sourcerer, I have tried tinymice, JCKEdidor and JCEEditor. I have now given up with this method.
To insert php code into an article I have used a php include file and all the html is echoed inside this php include file. The php file is then referenced from within the Article Editor.
Those more proficient than me with joomla will no doubt do this correctly using a module or the like.
You can try another plugin called Direct PHP. It's nice and simple to use. Just download and install it and make sure it is enable. After this start writing your php code in joomla article.
Example:
<?php
echo "Hello World!";
?>
it supports all most every PHP functions and you can filter the functions what you don't need it from its settings.
Here is the link for the download.
Download & import plugin sourcerer https://extensions.joomla.org/extension/sourcerer/
{source} [[?php echo "Hello world"; ?]] {/source}
use this. It fine for me.