Skip to content

Instantly share code, notes, and snippets.

@crisu83
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crisu83/9135907 to your computer and use it in GitHub Desktop.
Save crisu83/9135907 to your computer and use it in GitHub Desktop.
<?php
/**
* @param string|array $content
*
* When passed as array:
* - heading: string|array
* - subtext: string
*
* @param array $options
*
* @return string
*
* Usage:
*
* Html::pageHeader(
* [
* 'heading' => [
* 'tag' => 'h2',
* 'content' => 'Page header',
* 'options' => ['class' => 'heading'],
* ],
* 'subtext' => 'Subtext',
* ]
* );
*/
public static function pageHeader($content, array $options = [])
{
if (is_array($content)) {
$content = static::content(
$content,
[
'heading' => [
'tag' => 'h1',
'append' => [
'subtext' => [
'tag' => 'small',
],
],
],
]
);
}
static::addCssClass($options, 'page-header');
return static::tag(ArrayHelper::popValue($options, 'tag', 'div'), $content, $options);
}
/**
* Parses elements from the given content according to the given structure.
*
* @param string|array $content
* @param array $structure
*
* @return array the parsed elements.
*/
public static function parseContent($content, array $structure = [])
{
$result = [];
foreach ($structure as $name => $element) {
$result[$name] = static::parseElement($name, $content, $structure[$name]);
}
return $result;
}
/**
* Parses the a specific element from the given content according to the given structure.
*
* @param string $name
* @param string|array $content
* @param array $structure
*
* @return array the parsed element.
*/
public static function parseElement($name, $content, array $structure = [])
{
if (isset($content[$name])) {
$element = is_array($content[$name]) ? $content[$name] : ['content' => $content[$name]];
} else {
$element = [];
}
$element = static::normalizeElement($element, $structure);
if (!empty($structure['prepend'])) {
$element['prepend'] = static::parseContent($content, $structure['prepend']);
}
if (!empty($structure['append'])) {
$element['append'] = static::parseContent($content, $structure['append']);
}
return $element;
}
/**
* Normalizes the given element using the given default values.
*
* @param array $element
* @param array $defaults
*
* @return array the normalized element.
*/
public static function normalizeElement(array $element, array $defaults = [])
{
return array_merge(
[
'tag' => 'div',
'content' => '',
'options' => [],
'prepend' => [],
'append' => [],
],
$defaults,
$element
);
}
/**
* Renders the elements in the given content.
*
* @param array $content
*
* @return string the rendered elements
*/
public static function renderContent(array $content)
{
$out = '';
foreach ($content as $element) {
$out .= static::renderElement($element);
}
return $out;
}
/**
* Renders the given element.
*
* @param array $element
* - tag: string
* - content: string
* - options: array
* - prepend: array
* - append: array
* - allowEmpty: bool
*
* @return string the rendered element.
*/
public static function renderElement(array $element)
{
$prepend = !empty($element['prepend']) ? static::renderContent($element['prepend']) . ' ' : '';
$append = !empty($element['append']) ? ' ' . static::renderContent($element['append']) : '';
$content = $prepend . $element['content'] . $append;
if (!empty($content) || (isset($element['allowEmpty']) && $element['allowEmpty'])) {
if (isset($element['formatter']) && is_callable($element['formatter'])) {
return call_user_func($element['formatter'], $content, $element);
} else {
return static::tag($element['tag'], $content, $element['options']);
}
}
}
/**
* Parses and renders elements from the given content according to the given structure.
*
* @param string|array $content
* @param array $structure
*
* @return string the rendered elements.
*/
protected static function content($content, array $structure = [])
{
return static::renderContent(static::parseContent($content, $structure));
}
/**
* Parses and renders a specific element from the given content according to the given structure.
*
* @param string $name
* @param array $content
* @param array $structure
*
* @return string the rendered element.
*/
protected static function element($name, $content, array $structure = [])
{
return static::renderElement(static::parseElement($name, $content, $structure));
}
@cebe
Copy link

cebe commented Feb 22, 2014

@crisu83 whats the benefit of writing php arrays instead of plain HTML?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment