I have a twig/timber function and the code doesn't shows any error but when I go to use function in my twig template there is an error saying '' is not a function.
This is what I have in my fucntions.php file in the twig template:
add_filter( 'timber/twig', function( \Twig_Environment $twig ) {
$twig->addFunction( new Twig_Function( 'get_custom_meta', 'get_custom_meta' ) );
} );
function get_custom_meta($key,$id){
if(empty($id)){
return types_render_field($key,array("raw"=>"true","separator"=>";"));
}else{
return get_post_meta( $id, 'wpcf-'.$key, true );
}
}
and this is how I'm calling the function in the template.
{{ function(get_custom_meta('facebook', event.post_id )) }}
This in in wordpress. Thanks
{{ get_custom_meta('facebook', event.post_id ) }}
I think you access functions like this rather than through function()
/**
* My custom Twig functionality.
*
* @param Twig_Environment $twig
* @return $twig
*/
add_filter( 'timber/twig', function( \Twig_Environment $twig ) {
$twig->addFunction( new Twig_Function( 'edit_post_link', 'edit_post_link' ) );
} );
when you make a function available in Twig by using Timber\Twig_Function inside the timber/twig hook like this you use it like that
{# single.twig #}
<div class="admin-tools">
{{ edit_post_link }}
</div>
{# Calls edit_post_link using default arguments #}
{# single-my-post-type.twig #}
<div class="admin-tools">
{{ edit_post_link(null, '<span class="edit-my-post-type-link">') }}
</div>
{# Calls edit_post_link with all defaults, except for second argument #}
the function() is for wordpress functions
refer to: timber twig functions