UI Component Pattern for a Simple PHP website

PHP user interface

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.

Contact form section

This website is so simple, it does not use any modern framework. The contact form itself is powered by AWS SES.  I created a directory in the root folder of the website called “components”. 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’t repeat yourself) principle, and make it quicker and easier to make changes in the future. Centralizing code ensures quality and scalability.

UI component directory

Searching the code base for references to this particular HTML revealed ten instances that could be cleaned up.

searching a code base

In the new component file, I copy and paste my HTML and CSS code.  Then, I go through each of the offending files, and replace the markup with a reference:

<?php include $_SERVER["DOCUMENT_ROOT"] . '/components/contact-section.php'; ?>

I also delete any CSS and JavaScript for this section that’s on the page. At first, I tried adding the JavaScript that controls this form’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, handling UI success/error notifications, and implementing CAPTCHA to thwart bots. Since it was a lot of files were morphed, I ran a quality assurance protocol to ensure nothing broke.

Being a Web Developer in NYC

hello world

Full time employment

Six months ago I started a full time programmer position in Manhattan. The office is located in a Chelsea building, on the 11th floor. It’s a trendy neighborhood with plenty of restaurants, walking distance from art galleries.

My daily commute takes me through the Bronx on the 6 train, landing me steps away from Madison Square Park and the Flat Iron Building. The subsequent ten minute walk pumps coffee through my veins and prepares my mind for a day of meetings, thinking, and coding.

An hour long lunch break gives me a chance to explore local shops and scenery. More walking. More thinking. Then, back to the office for more meetings and more coding. The boutique atmosphere has a start-up feel. Marketing and event-planning have been woven through my responsibilities as keeper of all things tech. Each member of our small team fashions a millinery worth of metaphorical hats. Team building events and shared lunches help us to stay lockstep.

After work I’ve been attending tech meetups and happy hours. Networking is important. I’m excited to see what the city has in store for my life and my career. This blog will be a journal for my adventures in technology.

Freelance

I’ve been a freelance web designer and developer in New York City for a few years. I find new clients prospecting small and medium size businesses.

Who is Anthony Pace

Anthony Pace

I’m a software engineer and web developer. I was born in New York City, and love living here. Being creative, solving problems, and building things excite me. I believe that boredom is the enemy of happiness. Having new experiences is how I find inspiration.

Programming and computers are important to me. I spend a lot of time online. The internet has helped me to discover things that are now important parts of my reality. I love to be a part of the wave by creating content, technology, and experiences for the web. This aspect of my life lets me be artistic and scientific at once

Fitness

As a life-long athlete, fitness and health are important to me. I spend weekends running, and participating in races around NYC. This is when I have some of my best ideas. Exercise gives me the energy I need to tackle my goals. During my best workouts, I experience a sense of flow only matched when I’m absorbed deeply into a creative project. I also enjoy lifting weights and martial arts.

Media

I draw inspiration from a lot of different places. I listen to podcasts and audiobooks while commuting (but still often do read actual books). Twitter has become one of favorite resources for new content. Consuming media propels my own creative urges. My drive to turn the imagined tangible boldens as I see others do it. You can check out my blog to find out more about what excites me.

Traveling

The urge to explore precedes creative expression. Experiencing the culture of a strange place stretches the imagination. I travel a few times per year for work, attending conferences and exhibitions, doing marketing work and managing live educational events. I also find myself on long adventures during my off time. These journeys have afforded me opportunities to meet people from around the world and make great memories.

Events

Social gatherings are exciting. Concerts, meet-ups, parties, and conventions help me connect with interesting people. In connecting with others we grow. I enjoy live music and good food. I attend group meetings that focus on my professional interests. Even digital events in virtual worlds present entirely new landscapes for me to explore.