3 ms·
Call me insane, but I actually like using the Heredoc syntax in my own PHP templates. The client loads a page, it passes through index.php to determine a class
by Jach 16y ago
Call me insane, but I actually like using the Heredoc syntax in my own PHP templates. The client loads a page, it passes through index.php to determine a class to create, a function is determined to be called, function may display a template (or may be a simple json-getter/poster) in which case I load up some options (like what template, vars to pass, etc.), send them to a function which displays the template and I'm done. Inside the .tpl file, I might have something like:
<?php
foreach ($users as $user) {
$userinput = htmlentities($user['userinput']);
echo <<<EOD
<div>
ID: {$user['id']} <br />
Name: {$user['user_name']} <br />
Input: $userinput
</div>
EOD;
}
?>
I've used Smarty in the past though, and I didn't mind it.
- DanHulton 16y agoI prefer something a little more like: <? foreach ($users as $user): ?> <div> ID: <?=$user['id']?> <br /> Name: <?=$user['user_name']?> <br /> Input: <?=htmlentities($user['userinput'])?> <br /> </div> <? endforeach; ?> That said, I know I'm INSTANTLY going to piss off the "NEVER USE SHORT TAGS EVER" camp, but if it's on a server I control (which is 99% of the time), or I can modify PHP init settings via .htaccess or ini_set (which is the other 1% of the time), it literally doesn't matter. The main takeaway is the alternate control structure (foreach: endforeach; instead of foreach {}). I find that way cleaner than heredocs.
- Jach 16y agoWell, you could use USERS_FOREACH as your delimiter instead of EOD... I don't mind the short tags (and agree you should be able to enable/disable them in almost all cases) but just don't find them worth it, what I do dislike is the abundance of angle brackets and question marks and calling a function at the moment of output. (It just looks odd, and when people use their own h() function it just muddies the water. I liked Smarty for removing this.) I think PHP and Perl got it right when they allowed embedding of normal vars in strings without any special syntax thanks to the $ sigil, we should take advantage of it where possible. When you have a bunch of non-PHP I think it makes sense to drop out into normal HTML, but otherwise, PHP strings are fine and your editor should support syntaxifying them as HTML.