Skip to content

Instantly share code, notes, and snippets.

@stagfoo
Last active May 16, 2016 22:28
Show Gist options
  • Save stagfoo/109e43bb5987a177ecbb20819f3f1870 to your computer and use it in GitHub Desktop.
Save stagfoo/109e43bb5987a177ecbb20819f3f1870 to your computer and use it in GitHub Desktop.
A simple template inclusion function to help make consistent reusable code, to use not with wordpress just remove `locate_template` on line 36. expired by http://bradfrost.com/blog/post/atomic-web-design/
<?php
/**
* Get Component for wordpress
* @link https://gist.github.com/109e43bb5987a177ecbb20819f3f1870
*
* @author Alex King (@stagfoo) & Hector Nuvarro
* @version 0.2
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/
/*=====================================
= Get a Component =
=====================================*/
//Example Use
// get_component([
// 'template' => 'components/page-header',
// 'vars' => [
// 'title Var'
// ]
// ]);
function get_component($files = Array()){
/*================================
= Varibles =
================================*/
$styleDir = ''; //where is you css dir (optional)
$compDir = 'components/'; //where are your comps (optional)
$errors = []; //empty error array what be be filled.
$return_string = false; //echo by default
if(isset($files['vars'])){
$vars = $files['vars']; //gets vars
}
if(isset($files['return_string'])){
$return_string = $files['return_string'];
}
ob_start(); //start object buffer
$component = include(locate_template($compDir.$files['template'].'.php')); //instead of echoing it, its stored
$component = ob_get_clean(); //set var to the stored buffer ( i believe this flats the vars)
/*=================================================
= Add Inline Scoped Styles (Optional) =
=================================================*/
$files['concatStyles'] = NULL;
if(isset($files['styles'])){
for ($i=0; $i < sizeof($files['styles']); $i++) {
//add into on string and add to bucket
$files['concatStyles'] .= " ".file_get_contents($styleDir.$files['styles'][$i].'.css');
}
}
/*=================================================
= Filter out tags you don't want =
=================================================*/
if(isset($files['remove_tags']) && sizeof($files['remove_tags']) > 0){
$tags = [];
for ($i=0; $i < sizeof($files['remove_tags']); $i++) {
$component = strip_tags_content($component,'<'.$files['remove_tags'][$i].'>',TRUE);
}
}
/*================================================
= Return Comp or Echo Comp =
================================================*/
if(true == $return_string){
return $component; //dont echo it allow it to be returned later;
} else {
echo $component;
}
unset($files); // unset file array
unset($vars); //unset vars so no population
}
/*==================================
= Dependency =
==================================*/
function strip_tags_content($text, $tags = '', $invert = FALSE) {
preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags);
$tags = array_unique($tags[1]);
if(is_array($tags) AND count($tags) > 0) {
if($invert == FALSE) {
return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text);
}
else {
return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text);
}
}
elseif($invert == FALSE) {
return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text);
}
return $text;
}
?>
<?php
//example component passing all vars into an array allows it to be unset after inclusion this prevents cross contamination
?>
<div class="<?php echo $vars['col']; ?>">
<div class="<?php echo $vars['class']; ?>">
<i class="<?php echo $vars['icon']; ?>"></i>
<h1><?php echo $vars['title']; ?></h1>
<p><?php echo $vars['content']; ?></p>
<?php echo $vars['buttons']; ?>
</div>
</div>
<?php
get_component([
'template' => 'molecule/transparent-box',
'remove_tags' => ['p'], //remove p because there is no content
'vars' => [
'col' => 'col-md-6',
'class' => 'trans-box text-center',
'icon' => 'icon-sushi',
'title' => 'Office <br>Catering',
'content' => NULL,
'buttons' =>get_component([
'template' => 'atom/button-list', //another component file
'return_string' => true
])
]
]);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment