-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jon Waldstein
committed
Aug 15, 2023
1 parent
8adc133
commit 1983711
Showing
4 changed files
with
100 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace Give\Framework\Views; | ||
|
||
use InvalidArgumentException; | ||
|
||
/** | ||
* Helper class responsible for loading views. | ||
*/ | ||
class View | ||
{ | ||
/** | ||
* Default domain | ||
*/ | ||
const DEFAULT_DOMAIN = 'FormBuilder'; | ||
|
||
/** | ||
* @since 3.0.0 | ||
* | ||
* @param array $templateParams Arguments for template. | ||
* @param bool $echo | ||
* | ||
* @param string $view Template name | ||
* When using multiple domains within this plugin, the domain directory can be set by using "." in the template name. | ||
* String before the "." character is domain directory, and everything after is the template file path | ||
* Example usage: View::render( 'DomainName.templateName' ); | ||
* This will try to load src/DomainName/resources/view/templateName.php file | ||
* | ||
* @return string|void | ||
* @throws InvalidArgumentException if template file not exist | ||
* | ||
*/ | ||
public static function load($view, $templateParams = [], $echo = false) | ||
{ | ||
// Get domain and file path | ||
list ($domain, $file) = static::getPaths($view); | ||
$template = GIVE_PLUGIN_DIR . "src/{$domain}/resources/views/{$file}.php"; | ||
|
||
if ( ! file_exists($template)) { | ||
throw new InvalidArgumentException("View template file {$template} does not exist"); | ||
} | ||
|
||
ob_start(); | ||
extract($templateParams); | ||
include $template; | ||
$content = ob_get_clean(); | ||
|
||
if ( ! $echo) { | ||
return $content; | ||
} | ||
|
||
echo $content; | ||
} | ||
|
||
/** | ||
* @since 3.0.0 | ||
* | ||
* @param array $vars | ||
* | ||
* @param string $view | ||
*/ | ||
public static function render($view, $vars = []) | ||
{ | ||
static::load($view, $vars, true); | ||
} | ||
|
||
/** | ||
* Get domain and template file path | ||
* | ||
* @since 3.0.0 | ||
* | ||
* @param string $path | ||
* | ||
* @return array | ||
*/ | ||
private static function getPaths($path) | ||
{ | ||
// Check for . delimiter | ||
if (false === strpos($path, '.')) { | ||
return [ | ||
self::DEFAULT_DOMAIN, | ||
$path, | ||
]; | ||
} | ||
|
||
return explode('.', $path, 2); | ||
} | ||
} |