5 ms·
Something that took me a number of projects and maintenance type work to realize is to use "helper" functions wherever possible. For example, a profile link:
by ericwaller 18y ago
Something that took me a number of projects and maintenance type work to realize is to use "helper" functions wherever possible.
For example, a profile link:
<a href="/user/profile/<?=$user->id?>"><?=$user->name?></a>
Write a simple function:
function user_profile_link ($user) {
return '<a href="/user/profile/$user->profile">$user->name</a>';
}
And use it:
<?=user_profile_link($user)?>
I used to think the extra code upfront wasn't worth it, but after dealing with a bunch of 300+ line templates for a while, I can tell you that it definitely is.
Also, you can see a use of string interpolation (a common use case for sprintf), ie "count is: $count"
- jmtame 18y agothat's exactly what I'm talking about. I've been struggling with an easy way to link like this, as I use profiles. After you see it, you wonder why you never thought of it in the first place.
- pwoods 18y agoSo is anyone using Smarty anymore? Or is this short form php now all the rage? Just curious because I'd update if it was.
- ericwaller 18y agoI've used smarty extensively at work, but I've always felt like I was fighting with the iteration constructs and odd choice of syntax. I've always hated that in smarty, associative arrays are accessed with a dot. Associative array indices don't have to be valid php variable names, so what happens when you want to access $user['home-address'] in smarty?
- JoelSutherland 18y agoWith Smarty 2.x you're left needing to do something like: {assign var=foo value="some string"} {$user[$foo]} Smarty 3.x has a legitimate lexer/parser so the templates will be much more flexible. I think they finally realized that since they are compiling the templates anyway the parsing can be slow.
- JoelSutherland 18y agoSmarty is awesome, but a little dated. They are finally working on 3.0 alpha which will have much better php5 support. http://smarty.net http://smarty.net Even as is though, it is worth using.
- bprater 18y agoI could never understand the advantage of using a templating system inside another templating system. Smarty has loops. PHP has loops. Did I miss the train?
- kwamenum86 18y agoSmarty produces slightly cleaner code but is worthless otherwise IMHO.
- louislouis 18y agoI use smarty mainly for it's templating benefits. Also you can write your own custom functions to call from the template files if needed, I found this to be very handy.
- RossM 18y agoIt's adding an extra layer of work which is unnessacary unless you're handing templates to people who really can't do PHP. If your designers are working on the HTML use Smarty tbh.
- mixmax 18y agoFunny, I do the exact same thing, I do it for everything. I thought that this was standard procedure - it gets you much cleaner code and it's easy to reuse all your little functions in other projects. Maybe a simple website with little boilerplate functions would be something to do as a weekend project?
- frankus 18y agoAt the risk of running afoul of Muphry's Law, your example needs double-quoted strings and curly braces (or your identifiers need to be moved outside the quotes and concatenated in). At any rate, a downside to this approach is that you can't easily add a class or target (or any other attribute) to the A tag (at least in your simplified example). Of course you could have an optional named parameter that would allow you to pass them in, e.g.: function user_profile_link ($user, $attrs) { return build_tag('a', array_merge(array('href' => "/user/profile/{$user->profile}"), $attrs), $user->name); } function build_tag ($tag, $attrs, $content) { array_walk($attrs, create_function('$value, $name, $list', '{$list[] = htmlentities($name, UTF-8) . "=\"" . htmlentities($value, UTF-8) . "\"";}'), &$list); return "<$tag " . join(' ', $list) . '>' . (($content !== '') ? htmlentities($content, UTF-8) . "</$tag>" : '/>'); } But trying to do method-chainey sorts of things in PHP quickly runs into the language's clunky syntax, not to mention keeping track of exactly what level of quotation you're at, and so on. This is one of the reasons I like Ruby, as this kind of stuff is much more natural. Something like this: def build_tag(tag, attrs, content) attr_string = attrs.collect { |name, value| h(name) + '="' + h(value) + '"' }.join(' ') "<#{tag} #{attr_string}>" + ((content != '') ? h(content) + "</#{tag}>" : '/>') end Both of these are probably a little denser than a sane person would put into production code, but the Ruby example is much more human-parseable to my eyes. (I'm using rails helper 'h', which could easily be defined in PHP as well).
- ericwaller 18y agoI definitely prefer the ruby build_tag too, php's fine.. until you try to do something even remotely interesting. And yeah, in real code the helpers sometimes need to get a bit more advanced. Something I like to do is add a class name corresponding to the function name (which I usually grab from __FUNCTION__) -- it helps keep a nice consistency between server-side/html/js/css