Skip to content

Instantly share code, notes, and snippets.

@benpolinsky
Created February 23, 2015 01:24
Show Gist options
  • Save benpolinsky/84cba7b845c8e7dd6b54 to your computer and use it in GitHub Desktop.
Save benpolinsky/84cba7b845c8e7dd6b54 to your computer and use it in GitHub Desktop.
Quick and Dirty Rails Style HTML Tag Helpers
<?php
/*
*
* link_to(url, text, options)
* url and text are strings, options is an object with any additional attributes you might want on a link
* Simple. Rustic, yeah? | Use it like this:
* link_to('http://www.google.com', "Google", object ["id" => "user_link_132930482", "class" => "my_link", "alt" => "alt-text"]);
*
*/
function link_to($url='', $text='', $options = ""){
// iterate through options if defined
$option_list = "";
if (!empty($options)){
foreach ($options as $key => $value) {
$option_list .= $key . "='" . $value . "'";
};
};
$html = "<a href='" . $url . "' " . $option_list . ">" . $text . "</a>";
echo $html;
}
/*
*
* img_tag(src, options)
* src is a string, options is an object with any additional attributes
*
*/
function img_tag($src, $options=""){
$option_list = "";
if (!empty($options)){
foreach ($options as $key => $value) {
$option_list .= $key . "='" . $value . "'";
};
};
$html = "<img src='" . $src . "' " . $option_list . " />";
echo $html;
}
/*
*
* bp_tag(tag_name, text, options)
* src and text are strings, options is an object with any additional attributes
*
*/
function bp_tag($tag_name, $text="", $options=""){
// iterate through options if defined
$option_list = "";
if (!empty($options)){
foreach ($options as $key => $value) {
$option_list .= $key . "='" . $value . "'";
};
};
$html = "<" . $tag_name . " " . $option_list . ">" . $text . "</" . $tag_name . ">";
echo $html;
}
?>
@benpolinsky
Copy link
Author

Can definitely use some refinement...

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