johnsmith153 Posted July 26, 2010 Share Posted July 26, 2010 I have created my own basic framework using a proper MVC structure, and want to design my next site properly, especally ensuring code is not duplicated without reason. How would I best do code reuse for 'HTML elements'. I do of course have a header.php page that does all the nav bar stuff and this is the same on every page. What I want to do is look at the best way to reuse for example a rounded corners box? Imagine it was used a few times on a lot of pages but always with different content / background colour etc. Would I?? (1) Use a function (which may have the HTML included into it): I could then just do this on the page displaying: <?php displayRoundedBox(400, 100, "Title", "#CCCCCC", "Content goes here"); ?> (2) Do it in a class, but I do't really see the point. You certainly wouldn't instantiate it. (3) Just include a file $height = "400"; $width = "100"; include("roundedBox.php" echo "content here</div>" I would say (1) by a long way but just want to see what the experts say. Please only respond if you are certain as I 'think' I know too. Link to comment https://forums.phpfreaks.com/topic/208945-html-elements-code-reuse-using-php/ Share on other sites More sharing options...
AbraCadaver Posted July 26, 2010 Share Posted July 26, 2010 Many will post what they "think" because there is no concrete "certain" way. I would use a combination of 2 and 3, with a HTML_Elements class and methods for each element and some shared methods that can parse an array into id, class, style, etc. But these elements would be generic. If this is just HTML, why not define a style class .roundedBox and apply it to the table or div instead of drawing it with a function or include? Then all you need are some utilities for drawing a "box" whether rounded or not and the ability to assign a style class to it. Link to comment https://forums.phpfreaks.com/topic/208945-html-elements-code-reuse-using-php/#findComment-1091387 Share on other sites More sharing options...
simshaun Posted July 26, 2010 Share Posted July 26, 2010 I typically try to avoid making "generators" for common HTML elements, but if I had to I would create classes for each individual element, letting attributes such as width, height, background color be properties of the class. <?php class Html_Rounded_Box { protected $attribs = array(); public function __construct($attribs = array()) { if (is_array($attribs)) $this->attribs = $attribs; } /** * Returns an attribute's value. * @param string $name * @return mixed|null Mixed if value exists, null if value does not exist. */ public function __get($name) { return isset($this->attribs[$name]) ? $this->attribs[$name] : NULL; } /** * Set an attribute's value. * @param string $var * @return mixed|null Mixed if value exists, null if value does not exist. */ public function __set($name, $value) { $this->attribs[$name] = $value; } /** * Set an attribute's value. * @param string $name * @param mixed $value * @return Html_Rounded_Box */ public function set($name, $value) { $this->attribs[$name] = $value; return $this; } /** * Generates the HTML for this element. * @return string */ public function generate($content = NULL) { if ($this->content === NULL && !is_string($content)) trigger_error('The rounded box requires content.', E_USER_ERROR); $html = '<div class="rounded-box"'; // Opening tag. if ($this->width !== NULL || $this->height !== NULL) { $html .= ' style="'; if ($this->width !== NULL) $html .= "width: {$this->width}px;"; if ($this->height !== NULL) $html .= "height: {$this->height}px;"; // Build in more fixed properties here, or create an enter-anything-you-want attribute. $html .= '"'; } if ($content === NULL) $content = $this->content; $html .= '>'; // Close the opening tag. $html .= $content; $html .= '</div>'; // Closing tag. return $html; } } // Usage example 1: $roundedBox1 = new Html_Rounded_Box(array('width' => 200, 'height' => 300, 'content' => 'Foo bar')); echo htmlentities($roundedBox1->generate()); // For example separation only. echo '<br /><br /><br />'; // Usage example 2: $roundedBox1 = new Html_Rounded_Box(); $roundedBox1 ->set('width', 100) ->set('height', 200) ->set('content', 'Hello world!'); echo htmlentities($roundedBox1->generate()); If you didn't want to instantiate it, you could create a simple static method. Link to comment https://forums.phpfreaks.com/topic/208945-html-elements-code-reuse-using-php/#findComment-1091392 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.