Helpers
Helpers are utility functions, often global, which simplify syntax, particularly within templates.
Built in helpers
The following namespaced functions are available across the whole framework. Forme\getContainer()
- get an instance of the DI container, instantiating/configuring if necessary.
Forme\getInstance($className)
- get an instance of a class via the container - you should normally use this instead of new
Forme\makeInstance($className)
- get a non singleton instance of a class via the container - you sometimes need this for example when the instance should be locally (rather than globally) stateful
Forme\loadWhoops()
- loads Whoops if dev and not already loaded - used internally, you probably won't need this
Forme\loadDotenv()
- loads dotenv if file exists and not already loaded - used internally, you probably won't need this.
Forme\request()
- get an instance of the Server Request
Forme\log()
- get an instance of the Logger
Forme\arrayKeysToCamelCase($array)
- recursively convert all the keys in an array to camelCase.
The following global functions are available if you are using a Forme theme. These are mostly useful in views.
assets($path)
- shortcut for the uri of the static assets directory in the current theme. You can also pass a path relative to the assets directory.
menu()
- access the WordPress menu via a convenient Eloquent model. No more painfully weird Nav Walkers 😃
option($key)
- get an acf option value by key with shorter syntax.
view($viewName, optional $context)
- render a view with the given data. You don't need this in templates (just use the $v instance) but comes in handy in other situations e.g. when you want to render a view in another function.
The following are still available in themes, but deprecated so you should avoid using them:
render_view($viewName, optional $context)
- this is analogous to echo $v->render()
- note that it echoes out the template. It's deprecated, and you should use view()
instead.
nav_menu()
- outputs header menu using bootstrap nav walker - an opinionated wp_nav_menu
. It's deprecated, and you should use menu()
instead.
Forme plugins do not come with any built in helpers.
Custom helpers
You can add your own global theme helper functions in helpers/helpers.php
.
You can do the same in plugins, however we recommend namespacing plugin custom helpers to avoid polluting the global namespace too much. The logic being that you might have multiple plugins activated, whereas you're only ever going to have one theme, so it's less of an issue - or at least more manageable - for themes.
You should wrap function declarations in an if (!function_exists())
check, since composer does not guarantee that ad hoc files won't get included twice.
TIP
Think carefully before adding helpers for use in templates, they can seem convenient but in practise you might not really need them, and it can become a a bit of an antipattern. For example, consider whether the functionality makes more sense as a service to be called in the controller.
Third party functions
Aside from WordPress, of course, and ACF, there are also a few additional third party functions available by default.
Symfony String is available but needs to be included, so is not really useful for views.
use function Symfony\Component\String\u;
// changes all graphemes/code points to camelCase
u('Foo: Bar-baz.')->camel(); // 'fooBarBaz'
Symfony Var Dumper is available globally while you're in development mode - i.e. dump()
and dd()
- check debugging for more info.
Laravel's default and collection helpers are available, most usefully: e()
, env()
, collect()
and tap()
.