Не совсем поняла. Вот, что прописано во view/index.php: <label><? echo $message; ?></label>
В контроллере я делаю так, в функции редактирования, то есть это edit.php:
if (!isset($_POST['description'])){
$this->view->adminDescription('Отредактировано администратором!');
}
И в файл ядра core/View.php я работаю с функцией так, чтоб передать в файл index.php сообщение:
public function adminDescription($message){
$path = 'application/views/main/index.php';
if (file_exists($path)) {
require $path;
return $this->setVar('adminDescription', $message);
}
}
utkv_n, не стоит так делать, у тебя должно получиться что-то вроде этого:
<?php
namespace Kohana\Mvc;
/**
* Acts as an object wrapper for HTML pages with embedded PHP, called "views". Variables can be assigned with the view
* object and referenced locally within the view.
*
* @package Kohana\MVC
*/
abstract class AbstractView
{
/**
* @var array Global variables
*/
protected static $globalData = [];
/**
* @var array Local variables
*/
protected $data = [];
/**
* @var string View filename
*/
protected $file;
/**
* @var string Source view filename
*/
protected $sourceFile;
/**
* Captures the output that is generated when a view is included.
* The view data will be extracted to make local variables. This method
* is static to prevent object scope resolution.
*
* @param string $k_view_filename Filename
* @param array $k_view_data Variables
* @return string
*/
protected static function capture(string $k_view_filename, array $k_view_data): string
{
// Import the view variables to local namespace
extract($k_view_data, EXTR_SKIP);
if (static::$globalData) {
// Import the global view variables to local namespace
extract(static::$globalData, EXTR_SKIP | EXTR_REFS);
}
// Capture the view output
ob_start();
try {
// Load the view within the current scope
include $k_view_filename;
} catch (Throwable $e) {
// Delete the output buffer
ob_end_clean();
// Re-throw the exception
$vars = [':file' => Debug::path($k_view_filename), ':error' => $e->getMessage()];
throw new ViewException('Render error in view :file: :error', $vars, 0, $e);
}
// Get the captured output and close the buffer
return ob_get_clean();
}
/**
* Sets a global variable, similar to `set()`, except that the variable will be accessible to all views.
* Note: when setting with using iterable object we're not attaching the whole object to the view, i.e. the
* object's standard properties will not be available in the view context.
*
* @param string|iterable $key Variable name or an array of variables
* @param mixed $value Value
* @return void
*/
public static function setGlobal($key, $value = null): void
{
if (is_iterable($key)) {
foreach ($key as $name => $value) {
static::$globalData[$name] = $value;
}
} else {
static::$globalData[$key] = $value;
}
}
/**
* Assigns a global variable by reference, similar to `bind()`, except that the variable will be accessible to all
* views.
*
* @param string $key Variable name
* @param mixed $value Referenced variable
* @return void
*/
public static function bindGlobal(string $key, &$value): void
{
static::$globalData[$key] =& $value;
}
/**
* Sets the initial view filename and local data.
*
* @param string|null $file View filename
* @param iterable $data Array of values
*/
public function __construct(?string $file = null, iterable $data = [])
{
if ($file !== null) {
$this->setFilename($file);
}
if ($data) {
// Add the values to the local data
$this->set($data);
}
}
/**
* Magic method, searches for the given variable and returns its value. Local variables will be returned before
* global variables. If the variable has not yet been set, an exception will be thrown.
*
* @param string $key Variable name
* @return mixed
* @throws ViewException
*/
public function &__get(string $key)
{
if (array_key_exists($key, $this->data)) {
return $this->data[$key];
}
if (array_key_exists($key, static::$globalData)) {
return static::$globalData[$key];
}
throw new ViewException('Variable :var is not set', [':var' => $key]);
}
/**
* Magic method, calls `set()` with the same parameters.
*
* @param string $key Variable name
* @param mixed $value Value
* @return void
*/
public function __set(string $key, $value): void
{
$this->set($key, $value);
}
/**
* Magic method, determines if a variable is set.
*
* @param string $key Variable name
* @return bool
*/
public function __isset(string $key): bool
{
return isset($this->data[$key]) || isset(static::$globalData[$key]);
}
/**
* Magic method, unsets a given variable.
*
* @param string $key Variable name
* @return void
*/
public function __unset(string $key): void
{
unset($this->data[$key], static::$globalData[$key]);
}
/**
* Magic method, returns the output of `render()`.
*
* @return string
*/
public function __toString(): string
{
try {
return $this->render();
} catch (Throwable $e) {
// Display the exception message and halt script execution.
// We use this method here because it's impossible to throw an exception from `__toString()`.
ViewException::handler($e);
}
// This line will never ne reached
return '';
}
/**
* Sets the view filename.
*
* @param string $file View filename
* @return $this
* @throws ViewException
*/
public function setFilename(string $file)
{
$path = Kohana::findFile('views', $file);
if (! $path) {
throw new ViewException('The requested view ":file" could not be found', [':file' => $file]);
}
// Store the file path locally
$this->file = $path;
$this->sourceFile = $file;
return $this;
}
/**
* Gets the source view filename.
*
* @return string|null
*/
public function getFilename(): ?string
{
return $this->sourceFile;
}
/**
* Assigns a variable by name. Assigned values will be available as a variable within the view file.
*
* @param string|iterable $key Variable name or an array of variables
* @param mixed $value Value
* @return $this
*/
public function set($key, $value = null): self
{
if (is_iterable($key)) {
foreach ($key as $name => $value) {
$this->data[$name] = $value;
}
} else {
$this->data[$key] = $value;
}
return $this;
}
/**
* Assigns a value by reference. The benefit of binding is that values can be altered without re-setting them. It
* is also possible to bind variables before they have values. Assigned values will be available as a variable
* within the view file.
*
* @param string $key Variable name
* @param mixed $value Referenced variable
* @return $this
*/
public function bind(string $key, &$value): self
{
$this->data[$key] =& $value;
return $this;
}
/**
* Renders the view object to a string. Global and local data are merged and extracted to create local variables
* within the view file. Global variables with the same key name as local variables will be overwritten by the
* local variable.
*
* @param string $file View filename
* @return string
* @throws ViewException
*/
public function render(string $file = null): string
{
if ($file !== null) {
$this->setFilename($file);
}
if (! $this->file) {
throw new ViewException('You must set the file to use within your view before rendering');
}
// Combine local and global data and capture the output
return $this->capture($this->file, $this->data);
}
}
если ты хочешь встравить один шабон в другой, то просто передавай его через вложенное View:
<?php $message = (new View($_POST))->render('edit'); ?>
<label><?=$message;?></label>