Enclavely / tailor

Build beautiful page layouts quickly and easily using your favourite WordPress theme.
https://www.tailorwp.com
GNU General Public License v3.0
1.05k stars 102 forks source link

Expose $args in tailor_partial filter and tailor_partial_* action #126

Closed oxyc closed 7 years ago

oxyc commented 7 years ago

It would be useful if tailor could expose the $args parameter in tailor_partial, so that users can hook in alternative template engines. Currently $partial, $slug, $name are passed, but exposing the arguments which are passed to the template is also needed to avoid having to create delegating PHP partials.

As an example, WooCommerce does this in wc_get_template.

In my case I'd like to render the templates through Twig/Timber. People using Sage and Blade would probably find it useful as well.

Open PR: #117

Example use case

A hook that would allow for twig partials using Timber, in this case overriding loop-list.php with tailor/loop--list.twig

add_filter('tailor_partial', function($partial, $slug, $name, $args = array()) {
    $theme_partial_dir = apply_filters('tailor_theme_partial_dir', 'tailor/');
    $theme_partial_dir = trailingslashit($theme_partial_dir);
    $templates = [
        "{$theme_partial_dir}/{$slug}--{$name}.twig",
        "{$theme_partial_dir}/{$slug}-{$name}.twig",
    ];
    if ($template = locate_template($templates)) {
        do_action("tailor_partial_{$slug}", $partial, $slug, $name, $args);
        $template = str_replace(TEMPLATEPATH, '', $template);
        Timber::render($template, $args);
        return false;
    }
    return $partial;
});
{% set posts = get_posts(q) %}

{% block content %}
  <div class="grid-x grid-margin-y">
    {% for post in posts %}
      <div class="cell">
        {% include ['teasers/teaser--'~post.post_type~'.twig', 'teasers/teaser.twig'] %}
      </div>
    {% endfor %}
  </div>
{% endblock %}

{% block pager %}
  {% if atts.pagination %}
    {% include 'parts/archive__pager.twig' %}
  {% endif %}
{% endblock %}
andrew-worsfold commented 7 years ago

@oxyc -

Thanks for this suggestion (and the PR!). I've merged it and this will be included in the next plugin release.