Welcome Guest, Not a member yet? Register   Sign In
Use of Closures in Views
#3

Generally speaking, I avoid creating new helper functions because they're defined globally. If I have enough use for a particular function or set of functions in multiple views, I might create a small library for them and load the library as needed. In most cases, though, I'm thinking of using this for situations in which I have to do something a little more complicated than what I would normally want done inside my view, but the code may be very specific to the view in question (of course, in the long run, I might find that I'm defining very similar functions in multiple places and decide to refactor to a more generalized function I could use in all of them).

In theory, I could even use a closure to call a helper function to prevent creating a dependency on the helper function in my view. I think my own simplification in the example is probably making a portion of the purpose of using the closures less clear. Since I'm using Bonfire, my imageHandler closure is actually calling Bonfire's Assets library to get the base path for the image(s) and to generate the img elements, so I'm isolating the use of the library from the view as well as handling the insertion of a placeholder image when an image isn't provided. So, the imageHandler is something closer to the following:

PHP Code:
public function imageHandler($imageName$displayName)
{
    if (empty($imageName)) {

        return Assets::Image(
            img_path() . 'path/placeholder.jpg',
            ['alt' => "Placeholder, image not available for {$displayName}"]
        );
    }
    return Assets::Image(
        img_path() . "path/{$imageName}",
        ['alt' => $displayName]
    );

This is still a bit of a simplification, but I don't want to diverge too much from the previous example. I'm hoping it's sufficient to just say that I'm trying to keep my view from containing more logic than necessary and from directly calling whatever functions I use in that logic, regardless of whether those functions are defined in a helper or a library. After all, I could just as easily generate similar output by loading the html_helper and calling img(img_path() . "path/{$imageName}", false, ['alt' => $displayName]);
Reply


Messages In This Thread
Use of Closures in Views - by mwhitney - 09-09-2015, 09:25 AM
RE: Use of Closures in Views - by includebeer - 09-12-2015, 10:25 AM
RE: Use of Closures in Views - by mwhitney - 09-14-2015, 10:15 AM
RE: Use of Closures in Views - by PaulD - 09-15-2015, 03:37 PM
RE: Use of Closures in Views - by kilishan - 09-16-2015, 06:31 AM
RE: Use of Closures in Views - by jLinux - 09-21-2015, 03:28 PM
RE: Use of Closures in Views - by spjonez - 09-24-2015, 06:57 AM
RE: Use of Closures in Views - by mwhitney - 09-24-2015, 07:29 AM
RE: Use of Closures in Views - by kilishan - 09-24-2015, 07:20 AM
RE: Use of Closures in Views - by spjonez - 09-24-2015, 03:42 PM
RE: Use of Closures in Views - by mwhitney - 09-25-2015, 10:03 AM
RE: Use of Closures in Views - by jLinux - 09-25-2015, 06:46 AM
RE: Use of Closures in Views - by mwhitney - 09-25-2015, 10:29 AM
RE: Use of Closures in Views - by kilishan - 09-25-2015, 07:04 AM
RE: Use of Closures in Views - by jLinux - 09-25-2015, 07:12 AM
RE: Use of Closures in Views - by jLinux - 09-25-2015, 07:17 AM
RE: Use of Closures in Views - by kilishan - 09-25-2015, 07:32 AM
RE: Use of Closures in Views - by jLinux - 09-25-2015, 08:13 AM
RE: Use of Closures in Views - by mwhitney - 09-25-2015, 08:26 AM
RE: Use of Closures in Views - by spjonez - 09-25-2015, 11:00 AM
RE: Use of Closures in Views - by mwhitney - 09-28-2015, 10:51 AM
RE: Use of Closures in Views - by kilishan - 09-25-2015, 01:24 PM



Theme © iAndrew 2016 - Forum software by © MyBB