|
1 |
| -# atom |
| 1 | +# Atom |
2 | 2 | Adding enhanced modularity to Craft CMS Twig templating
|
| 3 | + |
| 4 | +## Installation |
| 5 | + |
| 6 | +```shell |
| 7 | +$ composer require ether/atom |
| 8 | +``` |
| 9 | + |
| 10 | +## Usage |
| 11 | + |
| 12 | +Create a folder called `_atoms` in your `templates` directory (this can be |
| 13 | +customised, see [Config](#config)). |
| 14 | + |
| 15 | +### Basic |
| 16 | + |
| 17 | +In this folder you can create re-usable twig templates, or atoms (a.k.a. |
| 18 | +modules, components, molecules). You can access the atoms in twig using the |
| 19 | +following syntax: |
| 20 | + |
| 21 | +```twig |
| 22 | +{% x:my-atom %} |
| 23 | +``` |
| 24 | + |
| 25 | +The above will include `_atoms/my-atom` in your template. If `my-atom` |
| 26 | +doesn't exist then nothing will be output. |
| 27 | + |
| 28 | +### Parameters |
| 29 | + |
| 30 | +You can pass parameters to your atom which will be exposed within the atom. The |
| 31 | +current template context is NOT passed to the atom, so any global variables will |
| 32 | +have to be passed manually. |
| 33 | + |
| 34 | +```twig |
| 35 | +{% x:my-atom { heading: "Hello world!" } %} |
| 36 | +``` |
| 37 | + |
| 38 | +In the above example, `my-atom` will be given access to the `heading` variable. |
| 39 | +If `heading` isn't passed then the variable will be undefined. You'll want to |
| 40 | +check variable availability with `is defined` or `|default`. |
| 41 | + |
| 42 | +### Children |
| 43 | + |
| 44 | +Children can also be passed to atoms: |
| 45 | + |
| 46 | +```twig |
| 47 | +{% x:my-atom { heading: "Hello world!" } %} |
| 48 | + <p>This is my atom</p> |
| 49 | + <p>There are many like it, but this is mine</p> |
| 50 | + <p>{{ myVariable }}</p> |
| 51 | +{% endx %} |
| 52 | +``` |
| 53 | + |
| 54 | +Children are rendered in the parent context, not the atoms. This means any |
| 55 | +variables you pass to the atom will not be available in the children (unless |
| 56 | +they are also available in the parent context). |
| 57 | + |
| 58 | +Children are rendered within the atom using the `children` variable, which will |
| 59 | +contain the rendered contents of the children or `null` if no children are |
| 60 | +defined. |
| 61 | + |
| 62 | +```twig |
| 63 | +{# Contents of `_atoms/my-atom` #} |
| 64 | +<div> |
| 65 | + <h1>{{ heading }}</h1> |
| 66 | + {{ children }} |
| 67 | +</div> |
| 68 | +``` |
| 69 | + |
| 70 | +### Nested |
| 71 | + |
| 72 | +Atoms can be nested inside other atoms! |
| 73 | + |
| 74 | +```twig |
| 75 | +{% x:my-atom %} |
| 76 | + {% x:another-atom %} |
| 77 | +{% endx %} |
| 78 | +``` |
| 79 | + |
| 80 | +### Sub-folders |
| 81 | + |
| 82 | +You can store atoms in folders within your `_atoms` directory. For example, if |
| 83 | +you had an atom at `_atoms/cards/news`, you could access it using the following |
| 84 | +syntax: |
| 85 | + |
| 86 | +```twig |
| 87 | +{% x:cards/news %} |
| 88 | +``` |
| 89 | + |
| 90 | +### Dynamic Atoms |
| 91 | + |
| 92 | +You can render atoms with dynamic names by wrapping the atom name in square |
| 93 | +brackets: |
| 94 | + |
| 95 | +```twig |
| 96 | +{% set myVar = 'example-atom' %} |
| 97 | +
|
| 98 | +{% x:[myVar] %} |
| 99 | +``` |
| 100 | + |
| 101 | +## Config |
| 102 | + |
| 103 | +You can configure Atom by creating a `atom.php` file in your `config` folder. |
| 104 | +See [config.php](./src/config.php) for the available settings. |
0 commit comments