Created
February 23, 2015 01:24
-
-
Save benpolinsky/84cba7b845c8e7dd6b54 to your computer and use it in GitHub Desktop.
Quick and Dirty Rails Style HTML Tag Helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can definitely use some refinement...