git clone https://github.com/psrearick/template-components.gitcd template-componentsnpm installnpm run start
Components are containers for code. Each component corresponds to a block of web page content. Each block serves a specific purpose. For example, an "about" block or a "services" block.
Components are HTML code blocks with optional JavaScript. Each component is stored in a separate HTML file in
the src/Components directory.
Sections are containers for components. Each section corresponds to a type of block. An "about" section would be a collection of components designated as "about" components.
Sections are generated dynamically. They can, however, be overridden by a separate file. If an override file is not
specified, the section will be generated using the src/Templates/Section.html template.
Sections and components are configured in src/config/elementDefinitions.js. This file exports a sections
configuration object and a components configuration object.
Sections are elements that display sets of components. Each section is rendered using a template file. Template files are
specified in the definitions config file. If a template is not specified for a section, the page will render the default
section template, /src/Templates/Section.html.
Follow these steps to create a new section:
- If you need something custom instead of the default section template, create a section
.htmlfile in/src/Sections.- You can use the section template
/src/Templates/Section.htmlas a starting point. - This process can be done with the helper script,
bin/create-section.sh, as well.
- You can use the section template
- If you created a custom section file, modify it as needed using template variables as described in template variables.
- Add an entry for the section to the
sectionsobject in/src/js/elementDefinitions.js.- The
sectionsobject holds sections, keyed by the section name. path: the URL to thehtmlfile. UseURLconstructor withimport.meta.urlas the base url to convert the relative path in the/src/Sectionsdirectory to an absolute path to thedistdirectory, readable by the browser.properties: an object of key/value pairs used in the template, see template variables for a description of how to use this section.components: an array of component names that correspond to keys in the components object.
- The
export const sections = {
SECTION: {
path: new URL('../Sections/SECTION.html', import.meta.url),
properties: {
section: 'SECTION',
},
},
};A component is an individual page block.
Follow these steps to create a component:
- Add an
.htmlfile for the component in/src/Components/.- This can be done with
bin/create-section.shhelper script, which will copy thesrc/templates/Section.htmltemplate to thesrc/sectionsdirectory.
- This can be done with
- If needed, add a
.jsfile for the component insrc/js/Components.- if you used the helper script to create the component, adding the
--jsflag will create the JavaScript file as well.
- if you used the helper script to create the component, adding the
- Add any template variables needed to the
jsandhtmlfiles, see template variables. - Add an entry for the component to the
componentsobject in/src/js/elementDefinitions.js.
- Component definitions are split between
htmlandjs. htmlhas three values:code,properties, andbodyDefinition.code: the URL to thehtmlfile. UseURLconstructor withimport.meta.urlas the base url to convert the relative path in the/src/Sectionsdirectory to an absolute path to thedistdirectory, readable by the browser.properties: an object of key/value template variable pairs used in the template.bodyDefinitionthe definition of a div placed after the component's code. This dif is used to display additional content on the page so that you can see what the element would look like with more content. This object has two values:hasBody: a boolean value indicating if a body div should be added.bodyClasses: a set of classes to apply to the appended body div.
- If the html does not need an appended body, leave it as an empty object:
bodyDefinition: {}
jshas two values:codeandproperties.code: is the js code to be executed in string form. This can be retrieved from a file usingfs.readFileSyncusing theencodingoption to get a string instead of a buffer.- This uses two arguments, the path and encoding. For the path, use
path.jointo join the directory of the element definition,__dirname, to the relative path of the JavaScript file,/Components/{filename}. The encoding should be'utf-8'.
- This uses two arguments, the path and encoding. For the path, use
properties: is an object of key/value pairs used in the template.- If the component does not use JavaScript, leave it as an empty object:
js: {};
export const components = {
COMPONENT: {
html: {
code: new URL('../Components/COMPONENT.html', import.meta.url),
properties: {
KEY: VALUE,
},
bodyDefinition: {
hasBody: true,
bodyClasses: [
'min-h-[2000px]',
'full',
'bg-gray-100',
'bg-stripes',
'bg-stripes-primary-100',
],
},
},
js: {
code: fs.readFileSync(
path.join(__dirname, '/Components/COMPONENT.js'),
'utf8',
),
properties: {
KEY: VALUE,
},
},
},
};Variables can be used in component and section templates. This is most useful when a template is used multiple times.
Use the Mustache syntax, {{variable}}, for the placeholder. Modifier can be used by appending a | to the variable,
followed by the modifier:
|rwill add a random string to the end of the variable name that is unique to that component. So,{{variable|r}}would become something likevariable5Gu3s. This ensures that variables are unique if a component of template shows up twice on a page.|cwill make the first character of the variable's value uppercase. So,{{variable|c}}would becomeVariable.|uwill make the variable's value uppercase. So,{{variable|u}}would becomeVARIABLE.
The values for variables are configured using the properties object in each element's Element Definition. For components, the object is specific to the language, or file, being used. See the code example for the Component.
The two scripts used most often are npm run start and npm run deploy.
Both scripts start by running npm run rebuild, a script combining npm run reset and npm run build.
This will perform the following actions:
- force remove the
.parcel-cache/directory. - force remove the
.dist/directory. - rebuild the project with Parcel using
src/index.htmlas the entrypoint anddist/as the destination
From here, npm run start will run npm run dev to start a parcel development server.
npm run deploy:
- will move to the
gh-pagesbranch - replace the contents of the root directory with the contents of the dist directory
- push the contents of the new root directory to
remote/gh-pages
npm run build: compile code with Parcel usingsrc/index.htmlas the entrypointnpm run deploy: deploy thedistdirectorynpm run fix: run eslint, stylelint, and prettier and fix issuesnpm run lint: run eslint, stylelint, and prettier and report issues but don't fix them- deploying means:
- run pre-deploy script,
npm run predeploy - switch to
gh-pagesbranch, usingdist/as the project root - push to
remote/dh-pages
- run pre-deploy script,
- deploying means:
npm run dev: start Parcel development server withsrc/index.htmlas the entrypoint- optionally, add the
--openflag to open the project in your default browser
- optionally, add the
npm run predeploy: run rebuild script,npm run rebuild, the is run automatically before deploymentnpm run rebuild: run reset script,npm run reset, followed run the build script,npm run buildnpm run reset: remove the.parcel-cache/anddist/directoriesnpm run startrun rebuild script,npm run rebuild, followed by the development script,npm run devnpm run watch:css: set a css watcher onsrc/css/index.cssto compile tailwindcss helpers tosrc/css/tailwind.css- This script is not critical as building tailwind code JIT is done automatically by postCSS whenever Parcel runs
npm run test: This does nothing because there are no testsnpm run prepare: installs husky pre-commit hooks - this is run whennpm installis run, in case this is a new project
- CSS
- Fonts
- Playground
- Page Builder