0

The goal

Pass $title for my view _Layout.phtml.

The scenario

Home.phtml

$title = 'My application';

[...]

MasterPage::Invoke('Layout');

Layout.phtml

<?php echo $title; ?>

Invoke's method of MasterPage

[...]

public static function Invoke($layout)
{
    include_once File::Get($layout)
}

The problem

Notice: Undefined variable: title in [...] on line 32

Guilherme Oderdenge
  • 4,935
  • 6
  • 61
  • 96
  • Probably scoping issue - when include is called within function, included variables won't be introduced to global scope. You can debug using var_dump(get_defined_vars()); - call it within your function and within global scope and see what is going on. – Mikk Nov 29 '13 at 19:05
  • Seems like the variable doesn't exist in my function. In my `Home.phtml` I can see it and in `Layout.phtml` I just see the same error that I said before. – Guilherme Oderdenge Nov 29 '13 at 19:18

1 Answers1

1

Include directives are limited to variables in local scope, and the only variable in this scope:

public static function Invoke($layout)
{
    include_once File::Get($layout)
}

is $layout.

To "import" variables into this scope you can do something similar to: https://stackoverflow.com/a/10144260/925196

Community
  • 1
  • 1
Grzegorz Adam Kowalski
  • 5,243
  • 3
  • 29
  • 40