Skip to content

Instantly share code, notes, and snippets.

@trk
Last active December 21, 2015 16:20
Show Gist options
  • Save trk/b365f47793927006b1fb to your computer and use it in GitHub Desktop.
Save trk/b365f47793927006b1fb to your computer and use it in GitHub Desktop.
AvbMarkupOutput is a helper for use less html elements on your php codes!
<?php
/**
* AvbMarkupOutput
*
* @param array $configs
* @param null $page
* @return string
*/
function AvbMarkupOutput($configs=array(), $page=null) {
$output = "";
$attributeKeys = array('attributes' => '', 'data-attributes' => '');
if(!empty($configs)) {
// echo "<pre>" . print_r($configs, true) . "</pre>";
if(isset($configs['page']) && $configs['page']) {
$page = $configs['page'];
} elseif(is_null($page)) {
$page = wire('page');
}
// Set quick attributes
$_attributes = array('id', 'class', 'rel', 'href', 'title', 'src', 'style', 'lang', 'width', 'height', 'disabled', 'type');
foreach($_attributes as $k_ => $v_) {
if(isset($configs[$v_]) && $configs[$v_] != '') {
$configs['attributes'][$v_] = $configs[$v_];
unset($configs[$v_]);
}
}
// Set Tag
$tag = (isset($configs['tag']) && $configs['tag'] != '') ? $configs['tag'] : null;
// Is tag self closed ?
$tagSelfClosed = '';
if(strpos($tag, ':/') !== false) {
$tagSelfClosed = ' /';
$tag = str_replace(':/', '', $tag);
}
// Check and Set Field Value
$field = null;
$labelAndNotesField = null;
if(isset($configs['field']) && $page->{$configs['field']} && $page->{$configs['field']} != '') {
$labelAndNotesField = $configs['field'];
$field = $page->{$configs['field']};
}
if(isset($configs['label']['field_name']) && $configs['label']['field_name'] != '') {
$labelAndNotesField = $configs['label']['field_name'];
} elseif(isset($configs['note']['field_name']) && $configs['note']['field_name'] != '') {
$labelAndNotesField = $configs['note']['field_name'];
}
// Check and Set Values
$value = (isset($configs['value']) && $configs['value'] != '') ? $configs['value'] : null;
// Check is label active ?
if(isset($configs['label']) && is_array($configs['label'])) {
$label = $configs['label'];
if(!is_null($labelAndNotesField)) {
$labelPrefix = wire('user')->language->isDefault() ? "label" : "label" . wire('user')->language->id;
$label['value'] = wire('fields')->get($labelAndNotesField)->get($labelPrefix);
if(isset($label['separator_prepend']) && $label['separator_prepend'] != "") {
$label['value'] = $label['separator_prepend'] . $label['value'];
}
if(isset($label['separator']) && $label['separator'] != "") {
$label['value'] = $label['value'] . $label['separator'];
}
$output .= AvbMarkupOutput($label);
}
}
// Check is note active ?
if(isset($configs['note']) && is_array($configs['note']) && !is_null($labelAndNotesField)) {
$note = $configs['note'];
$notePrefix = wire('user')->language->isDefault() ? "notes" : "notes" . wire('user')->language->id;
$note['value'] = wire('fields')->get($labelAndNotesField)->get($notePrefix);
if(isset($note['separator_prepend']) && $note['separator_prepend'] != "") {
$note['value'] = $note['separator_prepend'] . $note['value'];
}
if(isset($note['separator']) && $note['separator'] != "") {
$note['value'] = $note['value'] . $note['separator'];
}
$output .= AvbMarkupOutput($note);
}
// Check is there a prepends ?
if(isset($configs['prepends']) && is_array($configs['prepends']) && !empty($configs['prepends'])) {
foreach($configs['prepends'] as $kpr => $vpr) {
$output .= AvbMarkupOutput($vpr);
}
}
// Check is there a append ?
if(isset($configs['prepend']) && is_array($configs['prepend']) && !empty($configs['prepend'])) $output .= AvbMarkupOutput($configs['prepend']);
// Set Attributes and Data Attributes as String
if(!is_null($tag)) $tagAttributes = getAttributesAsString($configs, $attributeKeys);
// Set Tag with attributes
if(!is_null($tag)) $output .= "\n<{$tag}{$tagAttributes['attributes']}{$tagAttributes['data-attributes']}{$tagSelfClosed}>";
// Set Field Value
if(!is_null($field)) $output .= "\n\t{$field}";
// Set Value
if(!is_null($value)) $output .= "\n\t{$value}";
// Check loop ?
if(array_key_exists('loop', $configs) && is_array($configs['loop']) && !empty($configs['loop'])) {
$loop = $configs['loop'];
// Set loop tag
$loopTag = (isset($loop['tag']) && $loop['tag'] != "") ? $loop['tag'] : null;
// Set loop fields
$loopFields = (isset($loop['fields']) && is_array($loop['fields']) && !empty($loop['fields'])) ? $loop['fields'] : null;
// Set loop values
$loopValues = (isset($loop['values']) && is_array($loop['values']) && !empty($loop['values'])) ? $loop['values'] : null;
if(!is_null($loopTag) && (!is_null($loopFields) || !is_null($loopValues))) {
$looper = $loop;
// Check loop fields exist
if(!is_null($loopFields)) {
unset($looper['fields']);
foreach($loopFields as $lfk => $lfv) {
if($page->{$lfv} && $page->{$lfv} != '') {
$looper['field'] = $lfv;
$output .= AvbMarkupOutput($looper);
}
}
}
// Check loop values exist
if(!is_null($loopValues)) {
unset($looper['values']);
foreach($loopValues as $lfv => $lvv) {
$looper['value'] = $lvv;
$output .= AvbMarkupOutput($looper);
}
}
}
}
// Check is there child item ?
if(isset($configs['child']) && is_array($configs['child']) && !empty($configs['child'])) $output .= AvbMarkupOutput($configs['child']);
// Check is there children items ?
if(isset($configs['children']) && is_array($configs['children']) && !empty($configs['children'])) {
foreach($configs['children'] as $key => $child) {
$output .= AvbMarkupOutput($child);
}
}
// Set end tag
if(!is_null($tag) && $tagSelfClosed == '') $output .= "\n</{$tag}>";
// Check is there a append ?
if(isset($configs['append']) && is_array($configs['append']) && !empty($configs['append'])) $output .= AvbMarkupOutput($configs['append']);
// Check is there a appends ?
if(isset($configs['appends']) && is_array($configs['appends']) && !empty($configs['appends'])) {
foreach($configs['appends'] as $kap => $vap) {
$output .= AvbMarkupOutput($vap);
}
}
}
return $output;
}
/**
* Get Attributes and Data-Attributes as String
*
* @param array $attributes
* @param array $keys
* @return array
*/
function getAttributesAsString($attributes=array(), $keys=array()) {
if(!empty($attributes) && !empty($keys)) {
foreach($keys as $attrKey => $attrValue) {
if(isset($attributes[$attrKey]) && is_array($attributes[$attrKey]) && !empty($attributes[$attrKey])) {
foreach($attributes[$attrKey] as $key => $value) {
$keys[$attrKey] .= ($attrKey == 'data-attributes') ? " data-{$key}='{$value}'" : " {$key}='{$value}'";
}
}
}
return $keys;
}
return array(
'attributes' => '',
'data-attributes' => ''
);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment