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

(This post was last modified: 09-25-2015, 07:04 AM by jLinux. Edit Reason: Added results to example )

(09-09-2015, 09:25 AM)mwhitney Wrote: This is really more of an observation than a question, but I guess I was wondering if anyone else has found a use for this (or even discovered it in the first place), as it is apparently a side-effect of the way the loader passes data to views (plus the addition of closures in PHP 5.3+). Essentially, if I assign a closure to a variable which is then passed to my view, I can use the closure in the view, allowing me to use what might otherwise be more complicated functionality in my view or prevent the need to go through my data multiple times to prepare it for display.

Because I primarily use Bonfire's Template library rather than loading my views directly, the example below may have an issue or two (other than the simplification for the sake of an example), but the basic idea should work.

In the controller, I setup my data, then define a closure to handle some more complicated logic which I want available in my view, but don't necessarily want to reside directly in my view, then pass the data (with the closure) and name of the view to the loader:





PHP Code:
// $list = $query->result() returned from model
$data = ['list' => $list'listCount' => count($list)];

// Define a closure to build an img element based on the record's image_name field.
$data['imageHandler'] = function ($imageName$altText) {
    $imageDir '/path/to/public/image/dir/';
    if (empty($imageName)) {
        return "<img src='{$imageDir}placeholder.jpg' alt='image not available for {$altText}' />";
    }
    return "<img src='{$imageDir}{$imageName}' alt='{$altText}' />";
};

$this->load->view('list'$data); 


Then the relevant portion of the view looks something like this:





PHP Code:
<div class='examples'>
    <?php foreach ($list as $record) : ?>
    <div class='record'>
        <?php 
        
// Use the closure to generate the img element, echo the result.
        echo $imageHandler($record->image_name$record->alt_text); 
        echo $record->content
        ?>
    </div>
    <?php endforeach; ?>
</div> 

In theory I could go a step further and define the closure itself in another class or some other location to prevent placing code in the controller which generates HTML for the view, but this is, more or less, something I stumbled across while trying to cleanup one of the more complicated modules I developed fairly early on in my time with Bonfire/CodeIgniter/PHP.


Question though, are you sure this is a closure? I think its actually an anonymous function.


I could be wrong though, thats why im asking, you seem a lot more experienced than I am with PHP.

From what I understand, closures will encapsulate all the variables in the environment around them, while anonymous functions need to be passed variables to have access to them.

Heres my example of the difference..
PHP Code:
$foo 'test';

// Anonymous Function
$anon = function($var)
{
 
   echo "Anonymous Function: \$var is {$var}\n";
};

$anon($foo);
// Result: Anonymous Function: $var is test
 
   
// Closure  
$closure = function() use($foo){
 
   echo "Closure: \$foo is $foo\n";
};

$closure();
// Result: Closure: $foo is test 


But yes, PHP does now support both types of anonymous functions, I was just referring to your example

You can also see the difference in my example
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