On Wed, Jun 27, 2012 at 11:06 AM, Michael Morris <dmgx.mich...@gmail.com>wrote:

> PHP has a braceless syntax stretching back to its roots as a template
> language.  Frameworks which make use of php templating use these tags
> quite frequently since it's harder to overlook an endif statement in a
> sea of HTML tags than a brace.  But what of endfunction?
>
> I did some look ups and found in the archive two of the most unhelpful
> - bordering on rude - comments I've seen on the bug database.
>
> [2003-06-09 13:40 UTC] der...@php.net > The "endif;" stuff is old
> legacy syntax and is definitely not the recommended way of doing
> things. For my part if can be removed for PHP 5... anyway, definitely
> not something we will add.
> [2009-02-17 15:41 UTC] johan...@php.net > Derick'S response to #24100
> is still valid. We still prefer the {} syntax.
>
>
> So sure "we" (whoever we is) prefer braces.  I prefer them in straight
> code contexts - I don't want to work with them in HTML templates.
> This said, the need to do a function or class closure inside a
> template is rare, bordering on non-existent.
>
> But for functions there is one case - anonymous functions -
> specifically for recursion. Here's a live example that renders a
> nested list.
>
> <?php
> $render = function( $categories ) use (&$render) {
>        ob_start() ?>
>                <ul style="padding-left: 1em;">
>                        <?php foreach ($categories as $category): ?>
>                        <li><?= $category['name'] ?>
>                                <?php if (count($category['children']) > 0):
>                                        echo $render( $category['children']
> );
>                                endif ?>
>                        <?php endforeach ?>
>                        </li>
>                </ul>
>        <?php return ob_get_clean();
> }
> ?>
>
> This would be ever so slightly easier to read with endfunction.
>
> <?php
> $render = function( $categories ) use (&$render):
>        ob_start() ?>
>                <ul style="padding-left: 1em;">
>                        <?php foreach ($categories as $category): ?>
>                        <li><?= $category['name'] ?>
>                                <?php if (count($category['children']) > 0):
>                                        echo $render( $category['children']
> );
>                                endif ?>
>                        <?php endforeach ?>
>                        </li>
>                </ul>
>        <?php return ob_get_clean();
> endfunction
> ?>
>
> I'm not going to raise too much of a fuss over it.  At the end of the
> day, it's a small change.  But the elitist, dismissive comments to the
> idea from the team at the time were unwarranted.
>
> Also, I have to wonder if the position on this has changed any at all?
>  Particularly since <?= has been turned on at all times as of PHP 5.4.
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
If those are the most unhelpful comments you've seen in the bug database,
you haven't been looking hard enough :)

This does seem like an improvement to readability, but in an edge case that
I would try to avoid. Defining functions in templates just seems wrong.

I know your example isn't meant to be taken as the only archtype, but an
example of how I would deal with recursion:

<ul style="padding-left: 1em;">
    <?php foreach ($categories as $category): ?>
    <li><?= $category['name'] ?>
        <?php if (count($category['children']) > 0):
            $tmp = $categories;
            $categories = $category['children'];
            include "renderCategories.php"
            $categories = $tmp;
         endif ?>
     <?php endforeach ?>
     </li>
</ul>

Not sure how great an idea doing a recursive include on a template would
be, but I also prefer using a templating engine, so I may be biased to work
in a different way.

Thanks,
John

Reply via email to