Divergence / framework

PHP Framework providing ActiveRecord models and out of the box CRUD controllers with versioning and ORM support
MIT License
18 stars 4 forks source link

Migrate Dwoo to Twig #7

Closed hparadiz closed 4 years ago

hparadiz commented 6 years ago

Dwoo/Dwoo is sadly an abandoned packagist project now.

themightychris commented 6 years ago

I notice it's tagged as unmaintained but I wouldn't consider it abandoned: https://github.com/dwoo-project/dwoo/pulls?q=is%3Apr+is%3Aclosed

Personally I'm more eying plates as a potential future replacement for Dwoo: http://platesphp.com/

My primary consideration though is avoiding moving to a tightly-sandboxed template system where templates can only pull in the data provided to them by controllers, as I'm more concerned with providing flexibility for developers to override templates at the instance level and be able to pull in additional data without overriding controllers too. Twig is probably the best choice if safety is a bigger concern for template developers than flexibility, I'm not really interested in the notion of a templating engine bringing some layer of security to the table

hparadiz commented 6 years ago

My primary consideration thus far has honestly been syntax cleanliness. Thus far I'm not entirely convinced that Twig's syntax is even really that good to be honest. I preferred using { } but it has a problem with javascript so to avoid having to use {explicit} everywhere when embedding javascript in a template I think this is why Twig uses {% %} for it's tags. And then there's plates which is PHP, yes, PHPLeague, awesome, but.... I'm not a big fan of raw PHP as a template engine. I don't think it's clean and the code it produces is super verbose.

And yes... I do sometimes use PHP code in my Dwoo templates. https://github.com/hparadiz/technexus/blob/master/views/admin/posts/edit.tpl#L47 https://github.com/hparadiz/technexus/blob/master/views/blog/post.tpl#L5

But honestly if I'm being honest about it, if the system forced me to use the controller, I'd end up writing cleaner code. And it's easier to unit test the output of [Controller]::respond() if the template isn't doing it's own logic.

At the moment Divergence is not tightly coupled with any templating engine per-say and I'm actually not going to remove Dwoo support at all. Why remove something already working and unit tested? In fact this repo only has a single template file in the form of https://github.com/Divergence/framework/blob/master/views/dwoo/design.tpl

I'm considering have it detect which template engine you have installed and just using what it finds. Prioritizing whatever. I just wanna remove Dwoo/Dwoo from requirements.

themightychris commented 6 years ago

@hparadiz that all makes good sense to me. I agree that Plates' syntax isn't as clean, the biggest thing I would miss in switching to it is all the syntax sugar.

BUT, when you use PHP's short and alt syntaxes it's really not that bad:

<?php $this->layout('template', ['title' => 'User Profile']) ?>

<h1>Welcome!</h1>
<p>Hello <?=$this->e($name)?></p>

<h2>Friends</h2>
<ul>
    <?php foreach($friends as $friend): ?>
        <li>
            <a href="/profile/<?=$this->e($friend->id)?>">
                <?=$this->e($friend->name)?>
            </a>
        </li>
    <?php endforeach ?>
</ul>

<?php if ($invitations): ?>
    <h2>Invitations</h2>
    <p>You have some friend invites!</p>
<?php endif ?>

PLUS you gain some major benefit from there being NO compilation layer. You can breakpoint and debug templates like normal scripts, opcache handles caching them like normal, errors can be traced back to source lines correctly and reference what you actually see. Those benefits on top of maintaining direct access to data fetching are just barely enough to sway me

hparadiz commented 4 years ago

I converted everything to Twig for version 1.1 Happy to accept pull requests for other rendering engine styles.