Skip to content

Tags PHP Function

Q edited this page Dec 13, 2020 · 6 revisions

Willows are essentially PHP functions wrapped in a few additional layer of sugar, but in some cases, we do not require that sweeting and want to use the raw power of PHP in templates.

PHP Function accept Flags and have the following format:

{% function %}

PHP functions are parsed earlier that Willows, to enable them to return data to the templates for configuration.

In order to return data to the template from a PHP function, it is required to add the [r] flag - as shown in the following example:

{~ group~user_dashboard_featured {+ [array] config = post: {% [return] w__post_id_by_path{+ dashboard +} %} +} ~}

This would update the original template in the buffer map to something like the following:

{~ group~user_dashboard_featured {+ [array] config = post: 6 +} ~}

In this case, this is useful for a Willow which is shared over multiple templates, but which require the ACF lookup to be run from a specific post ID.

Here are a few more examples, showing how PHP Functions can be used inside Willow templates:

Call an unregistered public PHP class::method, passing an array of data - the [return] flag forces the returned data to render in the output:

<div class="col-12">scope_test_internal: {% [return] \q\theme\context::scope_test_internal{+ [array] a->b = sm:medium, lg:large +} %}</div>

Call a global PHP Funtion, passing a string, which is rendered as part of the returned data, thanks to the [r] flag:

<div class="col-12">scope_test_external: {% [return] scope_test_external{+ "just a string" +} %}</div>

Load an option from WordPress, passing a string argument:

<div class="col-12">get_site_option->page_on_front: {% [return] get_site_option{+ page_on_front +} %}</div>

Call the WordPress get_the_title() function, passing an integer value:

<h4 class="col-12">get_the_title(): {% [return] get_the_title{+ 6 +} %}</h4>

Notes

  • Willows are designed to supplement PHP functions, it is not realistic to replace them in all use-cases, however, we should attempt to move all logical aspects of data collection, validation and sorting into clean context->task class::methods and only use template files to render and format data.

Clone this wiki locally