Last active
February 19, 2016 11:26
-
-
Save sergks/10ffacca28ee1f69c110 to your computer and use it in GitHub Desktop.
Helper для вывода данных для старых версий php
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
<?php | |
/** | |
* Вывод различных данных по шаблону | |
* | |
* require_once('path_to_file/RenderHelper.php'); | |
* RenderHelper::renderFile($filename, $params); | |
* RenderHelper::renderTemplate($template, $params); | |
* | |
* @author sergKs | |
*/ | |
class RenderHelper | |
{ | |
/** | |
* Возвращает вывод данных шаблона с именем файла $filename | |
* @param string $filename имя файла | |
* @param array $params | |
* @return string | |
*/ | |
public static function renderFile($filename, $params = array()) | |
{ | |
if (!file_exists($filename)) { | |
throw new InvalidArgumentException("Файл $filename не найден."); | |
} | |
ob_start(); | |
ob_implicit_flush(false); | |
extract($params, EXTR_OVERWRITE); | |
require($filename); | |
return ob_get_clean(); | |
} | |
/** | |
* Возвращает вывод данных шаблона $template | |
* @param string $template текст шаблона | |
* @param array $params | |
* @return string | |
*/ | |
public static function renderTemplate($template, $params = []) | |
{ | |
$data = array(); | |
foreach ($params as $key => $value) { | |
$data['{' . $key . '}'] = $value; | |
} | |
return strtr($template, $data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment