Skip to content

Commit

Permalink
Add support for meta tags
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundask committed Mar 21, 2012
1 parent 9393bb8 commit c641b29
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions libraries/Twiggy.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Twiggy
private $_twig;
private $_twig_loader;
private $_module;
private $_meta = array();

/**
* Constructor
Expand Down Expand Up @@ -85,6 +86,7 @@ public function __construct()
}

$this->_globals['title'] = NULL;
$this->_globals['meta'] = NULL;
}

/**
Expand Down Expand Up @@ -220,6 +222,23 @@ public function set_title_separator($separator = ' | ')
return $this;
}

/**
* Set meta data
*
* @access public
* @param string name
* @param string value
* @param string (optional) name of the meta tag attribute
* @return object instance of this class
*/

public function meta($name, $value, $attribute = 'name')
{
$this->_meta[$name] = array('name' => $name, 'value' => $value, 'attribute' => $attribute);

return $this;
}

/**
* Register a function in Twig environment
*
Expand Down Expand Up @@ -303,6 +322,38 @@ public function template($name)
return $this;
}

/**
* Compile meta data into pure HTML
*
* @access private
* @return string HTML
*/

private function _compile_metadata()
{
$html = '';

foreach($this->_meta as $meta)
{
$html .= $this->_meta_to_html($meta);
}

return $html;
}

/**
* Convert meta tag array to HTML code
*
* @access private
* @param array meta tag
* @return string HTML code
*/

private function _meta_to_html($meta)
{
return "<meta " . $meta['attribute'] . "=\"" . $meta['name'] . "\" content=\"" . $meta['value'] . "\">\n";
}

/**
* Load template and return output object
*
Expand All @@ -312,6 +363,8 @@ public function template($name)

private function _load()
{
$this->_globals['meta'] = $this->_compile_metadata();

return $this->_twig->loadTemplate($this->_template . $this->_config['template_file_ext']);
}

Expand Down Expand Up @@ -426,6 +479,32 @@ public function get_template()
return $this->_template;
}

/**
* Get metadata
*
* @access public
* @param string (optional) name of the meta tag
* @param boolean whether to compile to html
* @return mixed array of tag(s), string (HTML) or FALSE
*/

public function get_meta($name = '', $compile = FALSE)
{
if(empty($name))
{
return ($compile) ? $this->_compile_metadata() : $this->_meta;
}
else
{
if(array_key_exists($name, $this->_meta))
{
return ($compile) ? $this->_meta_to_html($this->_meta[$name]) : $this->_meta[$name];
}

return FALSE;
}
}

/**
* Magic method __get()
*/
Expand Down

0 comments on commit c641b29

Please sign in to comment.