{"id":2412,"date":"2013-11-22T17:56:49","date_gmt":"2013-11-22T17:56:49","guid":{"rendered":"https:\/\/www.antpace.com\/blog\/?p=2412"},"modified":"2025-08-25T13:50:49","modified_gmt":"2025-08-25T13:50:49","slug":"ui-component-pattern-for-a-simple-php-website","status":"publish","type":"post","link":"https:\/\/www.antpace.com\/blog\/ui-component-pattern-for-a-simple-php-website\/","title":{"rendered":"UI Component Pattern for a Simple PHP website"},"content":{"rendered":"<p>Reusable components are a staple of modern front-end web development. On my simple PHP website, I wanted to build user interface pieces, and reuse them across multiple pages. When I was creating a new page for a newsletter signup form, I realized that I was repeating a lot of code for a contact form section that is displayed on almost every page.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2413\" src=\"https:\/\/www.antpace.com\/blog\/wp-content\/uploads\/2024\/01\/Screen-Shot-2024-01-22-at-1.03.35-PM.png\" alt=\"Contact form section\" width=\"1849\" height=\"940\" \/><\/p>\n<p>This website is so simple, it does not use any modern framework. The contact form itself is powered by AWS SES.\u00a0 I created a directory in the root folder of the website called &#8220;components&#8221;. There, I put files containing HTML, CSS, and JavaScript code that would otherwise be repeated. Implementing this pattern will help my code adhere to the DRY (don&#8217;t repeat yourself) principle, and make it quicker and easier to make changes in the future. Centralizing code ensures quality and scalability.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2414\" src=\"https:\/\/www.antpace.com\/blog\/wp-content\/uploads\/2024\/01\/Screen-Shot-2024-01-22-at-1.09.07-PM.png\" alt=\"UI component directory\" width=\"1038\" height=\"500\" \/><\/p>\n<p>Searching the code base for references to this particular HTML revealed ten instances that could be cleaned up.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2415\" src=\"https:\/\/www.antpace.com\/blog\/wp-content\/uploads\/2024\/01\/Screen-Shot-2024-01-22-at-1.11.19-PM.png\" alt=\"searching a code base\" width=\"890\" height=\"879\" \/><\/p>\n<p>In the new component file, I copy and paste my HTML and CSS code.\u00a0 Then, I go through each of the offending files, and replace the markup with a reference:<\/p>\n<pre>&lt;?php include $_SERVER[\"DOCUMENT_ROOT\"] . '\/components\/contact-section.php'; ?&gt;\n<\/pre>\n<p>I also delete any CSS and JavaScript for this section that&#8217;s on the page. At first, I tried adding the JavaScript that controls this form&#8217;s functionality to that same file. It failed because it relies on a jQuery reference that is not loaded until lower in the document. Separating the JS code into its own file, similarly named as `contact-section-js.php`, and calling it below the library reference solved the issue. That code is responsible for passing the message along to the back-end, <a href=\"https:\/\/www.antpace.com\/blog\/custom-ui-notifications\/\">handling UI success\/error notifications<\/a>, and implementing CAPTCHA to thwart bots. Since it was a lot of files were morphed, I ran a <a href=\"https:\/\/www.antpace.com\/quality-assurance\/\">quality assurance protocol<\/a> to ensure nothing broke.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reusable components are a staple of modern front-end web development. On my simple PHP website, I wanted to build user interface pieces, and reuse them across multiple pages. When I was creating a new page for a newsletter signup form, I realized that I was repeating a lot of code for a contact form section &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.antpace.com\/blog\/ui-component-pattern-for-a-simple-php-website\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;UI Component Pattern for a Simple PHP website&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":3129,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[54,55,133,139,140],"class_list":["post-2412","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-front-end","tag-frontend","tag-ui","tag-web-dev","tag-web-development"],"_links":{"self":[{"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/posts\/2412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/comments?post=2412"}],"version-history":[{"count":1,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/posts\/2412\/revisions"}],"predecessor-version":[{"id":3130,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/posts\/2412\/revisions\/3130"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/media\/3129"}],"wp:attachment":[{"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/media?parent=2412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/categories?post=2412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/tags?post=2412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}