Skip to content
This repository has been archived by the owner on Aug 10, 2022. It is now read-only.

Tuned up Vegas\Mvc\View partial rendering #7

Merged
merged 2 commits into from
Sep 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Mvc/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct($options = null, $viewDir = null) {
if (isset($options['partialsDir'])) {
$this->setPartialsDir($options['partialsDir']);
} else {
// $this->setPartialsDir($options['layoutsDir'] . 'partials/');
$this->setPartialsDir($options['layoutsDir'] . 'partials/');
}

if (isset($options['layout']) && !empty($options['layout'])) {
Expand Down
89 changes: 81 additions & 8 deletions src/Mvc/View/Engine/Volt.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,27 @@ private function getClassInstance($className)
* ...
* </code>
* Usage:
* - Relative partial
* <code>
* {# somewhere in module view #}
* {{ partial('../../../layouts/partials/header/navigation') } # goes to APP_ROOT/app/layouts/partials/header/navigation.volt
* </code>
*
* - Global partial
* <code>
* {{ partial('header/navigation') }} # goes to APP_ROOT/app/layouts/partials/header/navigation.volt
* </code>
*
* - Local partial in module Test, controller Index (app/modules/Test/views/index/)
* <code>
* {{ partial('./content/heading') }} # goes to APP_ROOT/app/modules/Test/views/index/partials/content/heading.volt
* </code>
*
* - Absolute path
* <code>
* {{ partial(constant("APP_ROOT") ~ "/app/layouts/partials/header/navigation.volt") }}
* </code>
*
* NOTE
* name of 'partial' directory inside of module must be the same as name of global 'partial' directory:
* APP_ROOT/app/layouts/partials => ../Test/views/index/partials
Expand All @@ -139,16 +150,78 @@ private function getClassInstance($className)
*/
public function partial($partialPath, $params = null)
{
if (strpos($partialPath, './') === 0) {
$viewPartialsDir = sprintf('%s%s%s%s',
dirname($this->view->getActiveRenderPath()),
DIRECTORY_SEPARATOR,
basename($this->view->getPartialsDir()),
DIRECTORY_SEPARATOR
);
if (strpos($partialPath, '../') === 0 || strpos($partialPath, '/../') === 0) {
$this->renderRelativePartial($partialPath, $params);
} else if (strpos($partialPath, './') === 0) {
$this->renderLocalPartial($partialPath, $params);
} else {
$viewPartialsDir = $this->view->getPartialsDir();
$this->renderGlobalPartial($partialPath, $params);
}
}

/**
* Renders partial from local directory
*
* @param $partialPath
* @param null $params
*/
private function renderLocalPartial($partialPath, $params = null)
{
$partialsDirPath = sprintf('%s%s%s%s',
dirname($this->view->getActiveRenderPath()),
DIRECTORY_SEPARATOR,
basename($this->view->getPartialsDir()),
DIRECTORY_SEPARATOR
);

$this->renderPartial($partialsDirPath, $partialPath, $params);
}

/**
* Renders partial from global directory
*
* @param $partialPath
* @param null $params
*/
private function renderGlobalPartial($partialPath, $params = null)
{
$partialsDirPath = $this->view->getPartialsDir();
//allows absolute path
if (strpos($partialPath, $partialsDirPath) !== false) {
$partialPath = str_replace($partialsDirPath, '', $partialPath);
}

$this->renderPartial($partialsDirPath, $partialPath, $params);
}

/**
* Renders partial from relative path
*
* @param $partialPath
* @param null $params
*/
private function renderRelativePartial($partialPath, $params = null)
{
$partialsDirPath = realpath(sprintf('%s%s%s',
dirname($this->view->getActiveRenderPath()),
DIRECTORY_SEPARATOR,
dirname($partialPath)
)) . DIRECTORY_SEPARATOR;

$partialPath = $partialsDirPath . basename($partialPath);

$this->renderGlobalPartial($partialPath, $params);
}

/**
* Renders partials using prepared path
*
* @param $viewPartialsDir
* @param $partialPath
* @param null $params
*/
private function renderPartial($viewPartialsDir, $partialPath, $params = null)
{
$this->render($viewPartialsDir . $partialPath . $this->extension, $params);
}
}
11 changes: 11 additions & 0 deletions tests/Mvc/View/Engine/VoltTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,16 @@ public function testPartial()
$volt->partial('test/sample');
$this->assertEquals('2', ob_get_contents());
ob_end_clean();

ob_start();
$volt->partial(APP_ROOT . '/app/layouts/partials/test/sample');
$this->assertEquals('2', ob_get_contents());
ob_end_clean();

$content(array('test', ''));
ob_start();
$volt->partial('../../../layouts/partials/test/sample');
$this->assertEquals('2', ob_get_contents());
ob_end_clean();
}
}

This file was deleted.