0

i want to use different objects inside the template. different objects are created on different section of the site.

currently my code is

public function notify ($template, $info)
{
    ob_start();
    include $template; 
    $content = ob_get_clean();
    //... more further code
}

as you see $info parameter. i dont want to use $info inside templates, but i was to use $photo, $admin or whatever is passed to it.

which i use like

// for feed
$user->notify('email_template_feed.php', $feed);

// for new photo - i would also like to use $user inside templates
$user->notify('email_template_photo.php', $photo); 

how can i do that? cant use global, because is inside function and function is getting called dynamically on different locations/sections of the site, which can further extend.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Basit
  • 16,316
  • 31
  • 93
  • 154

1 Answers1

3

You can't.

Solution 1

Instead you could use an array and extract its values:

public function notify ($__template, array $info)
{
    ob_start();
    extract($info);
    include $__template; 
    $content = ob_get_clean();
    //... more further code
}

Example 1

if you call it with:

$user->notify('email_template_feed.php', array('feed' => $feed));

and inside the template email_template_feed.php:

...
<?=$feed?>
...

it will print:

...
FEED
...

Solution 2

You could also pass the name of the variable as third parameter:

public function notify ($template, $info, $name)
{
    ob_start();
    $$name = $info;
    unset($info);
    include $template; 
    $content = ob_get_clean();
    //... more further code
}

Example 2

You could then call it via:

$user->notify('email_template_feed.php', $feed, 'feed');
Community
  • 1
  • 1
Shoe
  • 74,840
  • 36
  • 166
  • 272