Blade Template HTML Coloring Issue in Atom - php

When I have Laravel code within the 'carrots' of the HTML element itself, all further HTML elements become discolored.
ex. <body #php language_attributes() #endphp> discolors </body>
If I use vanilla php, all is well and fine.
ALSO:
<div class="somediv"> #php language_attributes() #endphp </div>
Works fine
My hunch is that it has something to do with the "#" symbol within the HTML element. This isn't so much as an issue, as it is an annoyance!
Screenshot provided for reference.
Code Screenshot - Note the red/orange

I would try Blade templating support

Related

php-cs-fixer doesn't indent HTML inside PHP control structures

I'm trying to use php-cs-fixer with a WordPress project, which means I (unfortunately) have files with a mix of PHP and HTML. I'm using the #PSR12 ruleset.
I'm having trouble with getting HTML within PHP control structures to indent correctly. Take this example snippet:
<?php if (!empty($related_posts)) : ?>
<div class="module--related_posts alignfull has-2-columns has-hover-state slider-on-mobile">
<h3 class="has-text-align-center">Related <?= esc_html($title) ?></h3>
</div>
<?php endif ?>
php-cs-fixer reformats it to:
<?php if (!empty($related_posts)) : ?>
<div class="module--related_posts alignfull has-2-columns has-hover-state slider-on-mobile">
<h3 class="has-text-align-center">Related <?= esc_html($title) ?>
</h3>
</div>
<?php endif ?>
Note the closing h3 tag has been moved to a new line, and the first-level indent within the if statement body has been removed.
The h3 issue I can live with, as this is resolved if I put the opening tag on its own line:
<h3 class="has-text-align-center">
Related <?= esc_html($title) ?>
</h3>
...but the lack of indent within the if statement is going to do my head in. The same thing happens with for and while statements.
Is there a rule in php-cs-fixer that I've overlooked that will resolve this?
The Answer of keradus on the "PHP code does not align with html code" issue:
PHP CS Fixer was never written with supporting mixed html/php file.
Some fixers are supporting one php part and one html part inside single file, but not big mix of them, like in template files.
If we would like to support template files, we would need to not only detect and track concrete fixers, but also provide some big integration test (like we do for Sf ruleset) that it remains to work for most important rules.
Before that happen, I would not claim that we officially support html/php mixed-files.
And in another answer:
we do not aim to fix mixed file (PHP and HTML in single file)
Not really an answer but I ended up just switching to PHP_CodeSniffer to get around this.

How to add php old code in php blade view

I started a laravel + vuejs project (fontend and backend in the same project).
It is basically a migration from a pure php-html project. I am wondering if it would be possible to migrate only a part of it, and to integrate the rest of it.
After your answers (thanks :) ), here is myview.blade.php:
#extends('template')
#section('contenu')
<div id="app">
#php
$path = public_path('myview/oldfile.php');
include($path);
#endphp
</div>
#endsection
Now I have an issue:
** Issue 1 **
I have this kind of code in the old php files:
<script type="text/javascript" src="./js/jquery-1.7.2.min.js">
but I also have scripts in my template (which contains the navbar and footer). Then of course I get this error
[Vue warn]: Error compiling template:
Templates should only be responsible for mapping the state to the UI. Avoid placing tags with side-effects in your templates, such as <style>, as they will not be parsed.
** Issue 2 **
$.post("./myview.php", "mode=activateForm&id1=<?=$id1?>&id2=<?=$_GET["id2"]?>", function() {
MethodNotAllowedHttpException
The POST method is not supported for this route. Supported methods: GET, HEAD.
The new issue is this, and I don't get why...
Any suggestions?
When you use #include in blade template it will look for the file inside of resource/views directory.
so use it like #include('posts/index').
where it will look for index.php inside of views/posts/ directory.
Do not use .php inside #include.
#extends('template')
#section('contenu')
<div id="app">
#include("yourDirectory/oldfile");
</div>
#endsection
Use the public path helper.
Put the files in your public folder and use public_path('filename.php'); in your include statement.

Blade template loads the resources in one sub view doesn't work for other

I am facing a pretty strange problem working with templates in Laravel. I will demonstrate it with the following example.
css/custom.css
.muted{ color:red }
Layout.blade.php
<html>
<head>
<title> HomePage </title>
<link href="css/custom.css" rel="stylesheet" />
</head>
<body>
<p>Layout file</p>
#yield('content')
</body>
</html>
subview1.blade.php
#extends('Layout')
#section('content')
<p class="muted"> From sub view 1 </p>
#stop
subview2.blade.php
#extends('Layout')
#section('content')
<p class="muted"> From subview 2 </p>
#stop
Actually what is happening is that for subview 2 the class muted is not applied. And console shows that it could not load the resource i.e custom/css when second view is loaded but works fine with subview1
Any pointers to the real issue and solution are highly appreciated.
EDIT
Attached is the directory structure. And I am referencing it like this.
1) dashboard_layout.blade.php (Layout File)
2) dashboard_index.blade.php ( subview1 )
3) course_index.blade.php (subview2)
dashboard_index.blade.php
#extends('teacher.dashboard.dashboard_layout')
course_index.blade.php
#extends('teacher.dashboard.dashboard_layout')
Actually, It's always confusing to reference the same assets using relative paths.You have to have an eagle eye to work out.Here comes Laravel's Html providers. Use them to reference your assets in your blade files as mentioned below rather than using simple link and script tags.
Html::style('assets/css/custom.css') // For style sheets.
Html::script('assets/js/custom.js') // For scritps.
NOTE : assets directory mentioned above is under your public folder as per Laravels app structure.
Html & Form Providers were shipped with Laravel basic app in previous versions.But in latest versions of Laravel. They are removed from the core.So above code snippet will produce an error. You can follow the minimal steps mentioned here laravelcollective to fix the issue.
Happy Learning !

Template inheritance and sections without Blade syntax

How to structure view hierarchy without using blade? What are the pure php counterparts of blade directives (i,e #section, #extend , etc)?
Perhaps, something similar to <?php extend('foo') ?>
In Phalcon framework, while it has its own template engine (Volt) all of its template engine is also available in pure PHP syntax.
Since Blade directives just compile to normal PHP, it is technically possible to use the view structuring features without actually using Blade. I don't think it's very pretty though, and I personally would think twice about this decision.
You can find all the PHP code, Blade is compiled to, in this class:
Illuminate\View\Compilers\BladeCompiler
Here are some of them:
#section('content')
<?php $__env->startSection('content'); ?>
#endsection
<?php $__env->stopSection(); ?>
#extends('layout')
This is a bit a tricky one. Usually Blade compiles it and then adds it to a footer variable which is printed at the bottom. So instead of putting it at the top (like you would with #extends) you have to place this at the end of your view:
<?php echo $__env->make('layout', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
#yield('content')
<?php echo $__env->yieldContent('content'); ?>
To put this in a pure PHP way you'll have to check out the storage/framework/cache/views and see what's happening there. Basically, is what Blade compiles to PHP code (instead of using # and with proper function calls).
One way I can think is:
In your template where you use yield:
<!-- template.php -->
<div class="container">
<!-- instead of using yield('container') -->
<?php echo "_yield:container"; ?>
</div>
In your file, instead of using section and stop
<!-- view.php -->
<!-- instead of using extend('template') -->
<?php $templatePath = 'template.php'; ?>
<?php $sections = []; ?>
<!-- instead of using section('container') -->
<?php $currentSectionName = 'container'; ob_start(); ?>
<p>This will be in my container div</p>
<!-- instead of using stop -->
<?php
// get the current html
$sections["_yield:".$currentSectionName] = ob_get_contents();
ob_end_clean();
ob_start();
require($templateName);
$template = ob_get_contents();
ob_end_clean();
echo str_replace($template,array_keys($sections),array_values($sections));
?>
Of course, this approach is simplistic at best. The code provided is not intended as a copy & paste solution, more like the concept.
Everything else is simple:
#foreach($arr as $k=>$v)
...
#endforeach
translates to
<?php foreach($arr as $k=>$v) : ?>
...
<?php endforeach; ?>
That's how it's exactly done by the BladeCompiler. The same is with if and while.
The pure PHP equivalent to Blade is to split your code in sections like header and footer (for example) and then use require in your page to blend those sections in the corresponding place.
<?php
require("template/header.php");
// Here goes the body code
require("template/footer.php");
?>
There is no pure PHP functions that i can think of, to extend a page from a main template, a you do using the yield directive.
Blade compiles into PHP every time and what it compiles is stored in to storage/framework/views/*
The following link is a list of all things blade can compile to, you should be able to extract some knowledge out of this:
https://github.com/illuminate/view/blob/master/Compilers/BladeCompiler.php
The general idea for most templating engine is that they structure your code like so:
if ($condition):
// do stuff
endif;
while ($condition):
// do stuff
endwhile;
foreach ($array as $key => $value):
// do stuff
endforeach;
For further reference, see https://secure.php.net/manual/en/control-structures.alternative-syntax.php
Neither of blade directives is a 'pure' PHP function. PHP functions cannot start with # and all blade directives do. In short, blade directives are shortcuts or synonyms to PHP built-in functions or control structures.
You are free to use any other template engine – it doesn't have to be Blade. Blade is built-in, but you are not locked to it. Just install a vendor package or make your own one, and return your HTML output with response, instead of using View facade.

Add <div> on a MediaWiki geshi syntax highlight extension

I use mediawiki to take note about the procedure that I follow, the source codes I write in mediawiki are highlighted with the expansion Genshi Syntax HighLight. I want to modify this expansion in mediawiki so it could be created a box above the source code in which it is written the programming language I used. I tried to see expansion sources in my mediawiki but I didn't find the segment in which is "sketch" the <div>. I also saw material about the creation of new expansion in mediawiki to understand how it runs, but I don't understand where the box is created.
I use syntax hightligher like this
some_code
and this is the result in html code generate from mediawiki
<div class="mw-geshi mw-code mw-content-ltr" dir="ltr">
<div class="bash source-bash">
<pre class="de1">
some_code
</pre>
</div>
</div>
I want to prepen the div to first div, like this
<div class='gsh-lang-label'>Language bash</div>
<div class="mw-geshi mw-code mw-content-ltr" dir="ltr">
<div class="bash source-bash">
<pre class="de1">
some_code
</pre>
</div>
</div>
Can you explain me if it is possible to do it and how can I face the problem?
I think ordinary jQuery will solve this problem. Something like:
$(".mw-geshi").each(function(){
$(this).before("<div class='gsh-lang-label'>" +
$(this).children().first().attr("class").split(' ')[0] +
"</div>")
})
Put this in [[MediaWiki:Common.js]], so this script will be run for every user.

Categories