Laravel Blade comment before "#if" - php

Sometimes, in HTML, I need to avoid adding extra white space, but I also need to add "#if()" blocks. In order to accomplish this, I've taken this approach:
some text{--
--}#if($someCondition){--
--}more text with no space between
#endif
And this approach has been working great. However, after upgrading my development and live environments to Laravel 5.5 (from Laravel 5.2) the Development environment works fine, but the live environment renders the above blade into the following HTML:
some text#if($someCondition)more text with no space between
<?php endif; ?>
... So the "if" statement immediately after the blade comment is left as plain text.
I know that it's possible, because both my development environments render the page correctly. I've looked at PHP versions and copied over the entire code directory, but I cannot find any difference between them at all, let alone a difference that would cause this to be processed differently.
Is there any reason why Laravel's blade engine wouldn't allow a "#" right after blade comment?
Using PHP 7.029, Laravel 5.5, CentOS.

I've discovered the issue. The view was cached on the development environments, which is why it was working.
It looks like there was a new bug introduced in Laravel somewhere between 5.3 and 5.5. The blade comments are being removed before the rest of the blade is analysed, which can cause issues as I showed in the question here. This was working previously in Laravel 5.2.
I did find a work around:
some text{{null
}}#if($someCondition){--
--}more text with no space between
#endif
Produces something that works:
some text<?php echo e(null); ?><?php if($someCondition): ?>more text with no space between
<?php endif; ?>

An alternative way to solve the original issue would be to directly write some PHP code for your if statements:
some text<?php
if($someCondition):
?>more text with no space between<?php
endif;
?>
(however, I agree this is not the sweatest way to write templates, as you will have a mix between Blade and PHP)
Laravel converts your blade templates into PHP, then use these converted files for rendering. So if you write directly some PHP code with your Blade templates, it will just work the same.

Could always use the Blade native php enclosures...
some text{{ if(condition) {do something} }}more text
This gives the the flexibility of inline php with your HTML.

Related

How can I format PHP files with HTML markup in Visual Studio Code?

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.

Why does Yii render "<?php" into every backend controller output?

Reproduceable problem
In 2.0.3 "advanced" (Yii offers basic and advanced setup of a project) create an empty controller in the backend, like
public function actionTest()
{
}
and call it in the browser. You'll see an empty page. When looking at the source code, you'll see a lonely php tag.
<?php
This looks like a bug to me, as it does not happen in the frontend folder, just in backend folder. This is critical when you render out CSV files or so, it will break the file.
I'm unsure if this is a bug on my side or a real bug inside the framework.
This is a bug in Yii 2.0.3!
... caused by a lower-level and not-so-obvious whitespace problem in naked PHP files.
Tickets & more information:
https://github.com/githubjeka/yii2-rest/issues/3
https://github.com/yiisoft/yii2-app-advanced/issues/24
http://php.net/manual/en/language.basic-syntax.phptags.php#116883
How to fix this:
The problem in a nutshell: A PHP tag is <?php[whitespace character], not just <?php. Holy! Totally makes sense, but let's be honest, who would have known this instantly ?
Yii 2.x uses (in the advanced demo application) the "empty" file config/bootstrap.php, just containing a php tag <?php WITHOUT a whitespace directly after. When bootstrap.php is now loaded via index.php it is loaded as a text file containing <?php, not parsed as a PHP file.
You can fix this by simply added a whitespace directly after the tag.

Komodo 8.5: PHP 'block' and 'inline' snippets do not work where needed

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).

Blade template vs plain php in Laravel

As I understand, Blade is simply regex parser, which will translate any Blade construction into PHP code, and then generate plain HTML from that PHP. Seems like this process makes loading files with Blade templates slower (because of the extra step Blade -> PHP). If so, why do I want to use Blade at all? Just because of the elegant syntax or because Blade files are stored in cache?
You'd use Blade because you want to use Blade. Like you've said it has a much nicer syntax and once you know its simple syntax it's very quick to use.
Regular PHP:
<?php if ($user->isLogged()): ?>
Welcome back, <strong><?= $user->name; ?></strong>
<?php endif; ?>
Blade:
#if ($user->isLogged())
Welcome back, <strong>{{ $user->name }}</strong>
#endif
Of course that's just a basic control structure. Blade has built in templating support as well.
Speed
There should be virtually no speed difference between the two as on the first load Laravel will compile any views that have changed into their PHP equivalent. Subsequent page loads will use this compiled file instead (they are stored at app/storage/views).
I guess the only extra overhead would be the initial check to see if the view has been compiled yet. Bugger all though.

HTML treat code within brackets as PHP code

I am building my website completely in PHP. I am trying to make it as much flexible as possible.
I have seen there are some softwares made in PHP that are able to get a HTML page, and before showing it, the PHP code recognizes the code inside brackets {PHP Code} as PHP code, runs it and only then shows the final page.
<h1>Hi My Name is {echo $name}</h1>
How can I achieve the same? I know there is Smarty Code. But I do not want to learn Smarty, I just want to know how to check a HTML page with PHP, find every bracket and threat that as PHP before showing the page..?
Can you point me somewhere?
Are you looking for PHP's basic syntax?
If you enable short_open_tags (it usually is enabled by default), this will work:
<h1>Hi My Name is <?=$name?></h1>
otherwise, this will always work:
<h1>Hi My Name is <?php echo $name; ?></h1>
PHP is already a templating language - there often is no need to add another layer of templating on top of it.
I want to keep the template files separated from the php engine
In fact, you don't
Your template files would behave as native PHP files in every way.
So, there is asolutely no [logical] reason to prefer such a strange solution over native PHP.
use the php tags for the echo statement.
<h1>Hi my name is <?php echo $name; ?></h1>
Well, just point apache to index.php which includes phtml templates into itself. Use <?php ?> instead of { }.

Categories