Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actions and filters #69

Closed
mgmartel opened this issue Sep 30, 2013 · 2 comments · Fixed by #70
Closed

Actions and filters #69

mgmartel opened this issue Sep 30, 2013 · 2 comments · Fixed by #70

Comments

@mgmartel
Copy link

I have been thinking about what to do with actions and filters in Timber. They are everywhere in WP development, so why not leverage them in Timber templates? Even though they are an open invitation for extra logic in your views, everything in WP hangs on them, so why not make them available in your templates.

I came up with the following implementations, but am not sure which would be best, maybe you have strong feelings for functions vs. filters. Anyway, here are my suggestions:

// {% do "my_action"|action %}
// {% do "my_action"|action(param1,param2,...) %}
$twig->addFilter( new Twig_SimpleFilter( 'action', function(){
    call_user_func_array( 'do_action', func_get_args() );
}));

// {% do action('my_action') %}
// {% do action('my_action',param1,param2,...) %}
$twig->addFunction( new Twig_SimpleFunction( 'action', function(){
    call_user_func_array( 'do_action', func_get_args() );
}));

// {{ "content"|apply_my_filter }}
// {{ "content"|apply_my_filter(param1,param2,...) }}
$twig->addFilter( new Twig_SimpleFilter( 'apply_*', function(){
    return call_user_func_array( 'apply_filters', func_get_args() );
}));

// {{ "content"|apply_filters('my_filter') }}
// {{ "content"|apply_filters('my_filter',param1,param2,...) }}
$twig->addFilter( new Twig_SimpleFilter( 'apply_filters', function(){
    $args = func_get_args();
    $tag = current( array_splice( $args, 1, 1 ) );

    return apply_filters_ref_array( $tag, $args );
}));
@jarednova
Copy link
Member

Hmmm, interesting stuff. I feel like option #2 (action('my_action')) makes the most semantic sense. Filters are really meant to transform content -- where as with {% do action('my_action') %} it's really a call.

same reason I think I prefer #4 -- I feel like the apply_my_filter filter (in ex. 3) doesn't properly tell the user they're applying a WP filter instead of a Twig/Timber filter.

Have you implemented these in yr fork already?

@mgmartel
Copy link
Author

mgmartel commented Oct 1, 2013

Agreed. Added the "action"|do_action syntax to mimic the behaviour of functions, but do action feels much more natural.

As for filters, I think apply_* is quite elegant, but counter-intuitive at first. Using apply_filters mimics the WP syntax closer and is as a bonus semantically nicer than the WP way itself ( 'take "this content" and apply filters' ).

Will open a pull request!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants