Open dextermb opened 6 years ago
I'd suggest PHP is simply outright banned from template files. :no_entry_sign:
If the project does not have a templating system, it should do. Both Blade and Twig can be used in a framework agnostic way and can be quite easily implemented.
If using a templating system if not immediately feasible, the alternative syntax is reasonable. :+1:
Fun fact: WordPress uses this alternative syntax for its theme files.
I'd suggest PHP is simply outright banned from template files. 🚫
If the project does not have a templating system, it should do. Both Blade and Twig can be used in a framework agnostic way and can be quite easily implemented.
If using a templating system if not immediately feasible, the alternative syntax is reasonable. 👍
What would you suggest for a project where there are already hundreds of files, such as YFB? It is written in Yii2 and currently does not make use of a template engine.
@dextermb
@dextermb
- Short term: Use alternative syntax.
- Long term: Aim to implement a templating engine. Blade might be good here as it permits inline PHP, so could be easier to transition to.
Going off topic, but inline with your long term suggestion I hope that we move everything to Laravel which would solve that.
I agree with Jordan on this one, php code does not belong in the views full stop.
Either use Blade's built in directives or abstract out to helper functions.
I believe there is even a way to register custom directives in Blade, don't quote me on it though.
@AlexCatch Custom Blade directives are possible, and reasonably easy to implement.
You can just add something similar to the following in a service provider boot
method.
Blade::directive('hello', function ($expression) {
return "<?php echo 'Hello ' . {$expression}; ?>";
});
What you would like to change/add
I'd like to put forward that, if you aren't going to use the built in control structures in a templating languages, to use the alternative control structure syntax in PHP.
That or if not too time consuming you should convert PHP control structures to that of the built in ones.
Why you would like to change/add this
I think it can become hard to follow branches when mixed in with HTML.
Examples
Here are some basic examples.
Built in control structure
Standard syntax
Alternative syntax
As the code gets more complex so does trying to understand it instantly.
Built in control structure
Standard syntax
Alternative syntax
Checklist
Sources