trendyminds/molecule
Grab Twig components, CSS and JS files outside the primary template folder
Maintainers
Requires
- craftcms/cms: ^3.0.0-RC1
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-06-16 23:10:13 UTC
README
π Your component structure and Craft CMS in harmony
Why Molecule?
Molecule allows you to grab Twig components from outside the template folder. This is useful if you want to keep your Twig partials, CSS, React/Vue files, etc in a single component folder. Now all of your components are nicely organized in individual folder structures!
βββ components/
β βββ ButtonPrimary/
β β βββ index.css
β β βββ index.jsx
β β βββ index.twig
β β βββ README.md
β βββ Gallery/
β βββ Hero/
β βββ VideoEmbed/
βββ public/
βββ cpresources/
βββ index.php
βββ .htaccess
Example use
Setup your ButtonPrimary/index.twig partial:
<a href="{{url}}"{% if newWindow is defined and newWindow %} target="_blank"{% endif %}> {{label}} </a>
Then include in your Twig templates using the following syntax:
{{craft.molecule.get("ButtonPrimary", {
url: "https://google.com",
label: "Visit Google.com",
newWindow: true
})
}}
You can even include components in other components!
/components/Hero/index.twig
<div class="Hero"> <img src="myImage.jpg" alt="A short description"> {{craft.molecule.get("ButtonPrimary", { url: "#", label: "Learn more" }) }} </div>
Icon Components
If you have an Icon/ component there's an additional craft.molecule.icon() helper you can use to output the SVG directly into your templates.
βββ components/
β βββ Icon/
β β βββ images/
β β β βββ arrow.svg
β β β βββ play-button.svg
β β β βββ twitter.svg
β β βββ index.css
β β βββ index.jsx
β βββ Gallery/
βββ public/
βββ cpresources/
βββ index.php
βββ .htaccess
{{craft.molecule.icon("twitter", { class: "custom_class" })}}
will compile to:
<span class="Icon Icon--twitter custom_class"> <!-- SVG contents of twitter.svg --> </span>
Testimonials
Matt Rothenberg @mattrothenberg
@aaronbushnell @CraftCMS I went all in π.
Feels so nice to be able to "compose" components in a React-y way, too. https://twitter.com/mattrothenberg/status/1094693570177654784/photo/1
π AspectRatioBox component from Matt Rothenberg
Requirements
This plugin requires Craft CMS 3.0.0-beta.23 or later.
Installation
To install the plugin, follow these instructions.
-
Open your terminal and go to your Craft project:
cd /path/to/project -
Then tell Composer to load the plugin:
composer require trendyminds/molecule -
In the Control Panel, go to Settings β Plugins and click the βInstallβ button for Molecule.
-
Configure the path to your components directory within Molecule's settings
Credits
Icon by Aaron Humphreys β Dribbble post
