Skip to content

Commit

Permalink
refactor(components): Load using include_once and call in loader
Browse files Browse the repository at this point in the history
Instead of expecting each component to call itself, this change calls 
component functions directly in the component loader, simplifying 
component writing. This change also switches to use `include_once` to 
load the component files, potentially speeding things up.
  • Loading branch information
delucis committed Dec 2, 2018
1 parent e944ec5 commit 323493d
Show file tree
Hide file tree
Showing 14 changed files with 11 additions and 32 deletions.
13 changes: 6 additions & 7 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ Besides the templates described above, WordPress themes provide much of their cu

To break up the large page templates described above, some layouts that are re-used are stored in the [`components`](components) directory and included in the main templates using the `component()` method declared in [`functions/component-loader.php`](functions/component-loader.php).

For example, `footer.php` includes a copyright string that updates its date range automatically and is defined in `components/hgnm_copyright.php`. To include a component call `component()` with the component file name as the first argument:
For example, `footer.php` includes a copyright string that updates its date range automatically and is defined in `components/hgnm_copyright.php`. To include a component, call `component()` with the component file name as the first argument:

```php
<?= component('hgnm_copyright') ?>
Expand All @@ -258,7 +258,7 @@ If the component needs to be passed additional data, this can be achieved via th

### Writing a component

A component is a PHP file declaring a function that renders the component and a call to that function. While technically a component is any function, best practice is for a component to return an HTML string. This allows you to pass a component around, for example handing it to another component for use as a child element, only printing its output when needed.
A component is a PHP file declaring a function that renders the component. While technically a component is any function, best practice is for a component to return an HTML string. This allows you to pass a component around, for example handing it to another component for use as a child element, only printing its output when needed.

The basic skeleton of a component looks like this:

Expand All @@ -277,18 +277,17 @@ if (!function_exists('pretty_p')) {
return $html;
}
}

// Call the component function and return it
return pretty_p($opts);
```

The second argument passed to `component()` is injected into your component’s scope as `$opts`, so by saving the component as `components/pretty_p.php` we can then use it as follows:
The function name _must_ match the filename you save the component as, so the above must be saved as `components/pretty_p.php`.

Your component function will be called with the second argument passed to `component()`, so we can then use the above as follows:

```php
<?= component('pretty_p', array( "text" => 'Paragraph text' )); ?>
```

**N.B.** the `<?= '...' ?>` PHP tag is shorthand for `<?php echo '...' ?>`
ℹ️ The `<?= '...' ?>` PHP tag is shorthand for `<?php echo '...' ?>`



Expand Down
2 changes: 0 additions & 2 deletions components/analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ function analytics($tracking_code)
<script async src='https://www.google-analytics.com/analytics.js'></script>";
}
}

return analytics($opts);
2 changes: 0 additions & 2 deletions components/button_link.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,3 @@ function button_link($opts)
return "<a $attribute_string>$inner</a>";
}
}

return button_link($opts);
2 changes: 0 additions & 2 deletions components/colloquium_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,3 @@ function colloquium_list($opts)
return $html;
}
}

return colloquium_list($opts);
2 changes: 0 additions & 2 deletions components/colloquium_list_item.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,3 @@ function colloquium_list_item($ID)
return $html;
}
}

return colloquium_list_item($opts);
2 changes: 0 additions & 2 deletions components/colloquium_location_link.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,3 @@ function colloquium_location_link(array $opts)
return $html;
}
}

return colloquium_location_link($opts);
2 changes: 0 additions & 2 deletions components/concert_list_item.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,3 @@ function concert_list_item(array $opts)
return $html;
}
}

return concert_list_item($opts);
2 changes: 0 additions & 2 deletions components/edit_button.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,3 @@ function edit_button()
return '';
}
}

return edit_button();
2 changes: 0 additions & 2 deletions components/hgnm_copyright.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,3 @@ function hgnm_copyright()
return $output;
}
}

return hgnm_copyright();
2 changes: 0 additions & 2 deletions components/icon.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,3 @@ function icon($opts)
return $html;
}
}

return icon($opts);
2 changes: 0 additions & 2 deletions components/member_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,3 @@ function member_list(array $opts)
return $html;
}
}

return member_list($opts);
2 changes: 0 additions & 2 deletions components/member_list_item.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,3 @@ function member_list_item($opts)
return $html;
}
}

return member_list_item($opts);
2 changes: 0 additions & 2 deletions components/responsive_embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,3 @@ function responsive_embed($media)
return $html;
}
}

return responsive_embed($opts);
6 changes: 5 additions & 1 deletion functions/component-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ function component($name, $opts = array())
if (empty($template)) {
throw new Exception("Template not found at $template_path");
}
return include($template);
include_once($template);
if (!is_callable($name)) {
throw new Exception("$template_path does not declare a function (or function is not named '$name')");
}
return $name($opts);
}
}

0 comments on commit 323493d

Please sign in to comment.