PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Feb 24, 2020 8:59 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Wed Dec 14, 2011 11:07 pm 
Offline
Forum Regular
User avatar

Joined: Wed Mar 05, 2008 11:23 pm
Posts: 732
Location: Sunriver, OR
I would like to build an include handler into the core of my app, yet still have the included variables available in the scope from which it is called.

example:

Syntax: [ Download ] [ Hide ]
<?php
  class Core
  {
    /* ... */
   
    static function _include($path)
    {
      try {
        if(!empty($path) && file_exists($path))
          include($path);
        else
          throw new Exception("Unable to include file at location: {$path}.  File does not exist.");
      } catch(Exception $e) {
        //TODO: Register Error and Log to Syslog
        exit($e->getMessage());
      }
    }
   
    /* ... */
  }
?>
 


The trouble is, in my view, when I call Core::_include('include/file'); the variables within the included file are in the scope of Core::_include(), not the local view where it was called.

This is tricky where I include a "config" file for a static menu contents, and then include the decorator object.

Has anyone come up with any slick solutions to this problem?


Top
 Profile  
 
PostPosted: Thu Dec 15, 2011 2:41 am 
Offline
Site Administrator
User avatar

Joined: Sun May 19, 2002 10:24 pm
Posts: 6887
Return the path instead of including the file. e.g.

Syntax: [ Download ] [ Hide ]
include $tpl->getPath();

_________________
Image


Top
 Profile  
 
PostPosted: Thu Dec 15, 2011 2:43 pm 
Offline
Forum Contributor
User avatar

Joined: Thu May 11, 2006 8:58 pm
Posts: 305
Location: Utah, USA
If you are wanting to render a template file (e.g. MVC pattern), you could also pass on the vars you want and use compact() and extract(). For example:
Syntax: [ Download ] [ Hide ]
$fname = 'John';
$lname = 'Doe';
$viewVars = compact('fname','lname');
echo Core::renderTemplate($tplPath, $viewVars);
// and in Core class
class Core {
  public static function renderTemplate($tplPath, $viewVars) {
    // check if tpl exists
    extract($viewVars); // now $fname and $lname are available in the template file
    ob_start();
    include($tplPath);
    return ob_get_clean();
  }
}

You can also create a Template class that works similarly but then allows Template instance functions to be available in the view (e.g. $this->func()).


Top
 Profile  
 
PostPosted: Wed Dec 21, 2011 1:24 am 
Offline
Forum Regular
User avatar

Joined: Wed Mar 05, 2008 11:23 pm
Posts: 732
Location: Sunriver, OR
Thank you both for your insight. I'm not sure why I didnt think of the simple solution that Benjamin posted.

Tr0gd0rr (♫ burninating all the people ♫), thanks for suggesting an alternative approach. I will give it some thought to see if it is a good fit for the current project, or has an application in a future one.

I've been so busy lately with my mind on other vectors, but I did consider your suggestions and wanted to say thanks before too much time went by.

Happy Holidays!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group