langleyfoxall / technologies

Technical overview of Langley Foxall, related technologies, frameworks, style guides and more.
https://technologies.langleyfoxall.co.uk/
Other
9 stars 8 forks source link

PHP control structures in Blade (and similar templating engines) #69

Open dextermb opened 5 years ago

dextermb commented 5 years ago

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

<ul>
    @foreach($items as $item)
        <li>{{$item->name}}</li>
    @endforeach
</ul>

Standard syntax

<ul>
    <?php foreach($items as $item) { ?>
        <li>{{$item->name}}</li>
    <?php }; ?>
</ul>

Alternative syntax

<ul>
    <?php foreach($items as $item): ?>
        <li>{{$item->name}}</li>
    <?php endforeach; ?>
</ul>

As the code gets more complex so does trying to understand it instantly.

Built in control structure

<ul>
    @foreach($categories as $items)
        <li>
            <p>{{$items->type}}</p>
            <ul>
                @foreach($items as $item)
                    <li>{{$item->name}}</li>
                @endforeach
            </ul>
        </li>
    @endforeach
</ul>

Standard syntax

<ul>
    <?php foreach($categories as $items) { ?>
        <li>
            <p>{{$items->type}}</p>
            <ul>
                <?php foreach($items as $item) { ?>
                    <li>{{$item->name}}</li>
                <?php }; ?>
            </ul>
        </li>
    <?php }; ?>
</ul>

Alternative syntax

<ul>
    <?php foreach($categories as $items): ?>
        <li>
            <p>{{$items->type}}</p>
            <ul>
                <?php foreach($items as $item): ?>
                    <li>{{$item->name}}</li>
                <?php endforeach; ?>
            </ul>
        </li>
    <?php endforeach; ?>
</ul>

Checklist

Sources

DivineOmega commented 5 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:

DivineOmega commented 5 years ago

Fun fact: WordPress uses this alternative syntax for its theme files.

dextermb commented 5 years ago

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.

DivineOmega commented 5 years ago

@dextermb

dextermb commented 5 years ago

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

AlexCatch commented 5 years ago

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.

DivineOmega commented 5 years ago

@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}; ?>";
});