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

Not able to use custom function in TWIG template!! #2576

Closed
neilnonstop opened this issue Dec 7, 2019 · 2 comments
Closed

Not able to use custom function in TWIG template!! #2576

neilnonstop opened this issue Dec 7, 2019 · 2 comments

Comments

@neilnonstop
Copy link

Hello!! I'm currently developing a Gantry 5-based site and need to implement breadcrumb navigation into the TWIG files. To create the breadcrumbs, I'm using Yoast SEO's built-in function for this.

In order to implement this in Gantry/TWIG, I'm attempting to follow your instructions for adding functions using theme.php. Here is the code I am attempting to add:

class karma {

	public function getBreadcrumb() {
	if ( function_exists('yoast_breadcrumb') ) {
			$breadcrumb = yoast_breadcrumb( '<p id="breadcrumbs">','</p>' );
			echo $breadcrumb;
		}
	}

}

$gantry['karma'] = new karma();

Whenever I try to call this new object/method in my TWIG file, by adding {{ gantry.karma.getBreadcrumb() }} to the template, I get this error message:

Fatal error: Uncaught Twig_Error_Syntax: Unexpected "gantry" tag (expecting closing tag for the "block" tag defined near line 4). in /Applications/MAMP/htdocs/karmacoop/wp-content/themes/g5_hydrogen-child/custom/views/partials/content.html.twig:19 Stack trace: #0 /Applications/MAMP/htdocs/karmacoop/wp-content/plugins/gantry5/src/vendor/twig/twig/lib/Twig/TokenParser/Block.php(40): Twig_Parser->subparse(Array, true) #1 /Applications/MAMP/htdocs/karmacoop/wp-content/plugins/gantry5/src/vendor/twig/twig/lib/Twig/Parser.php(190): Twig_TokenParser_Block->parse(Object(Twig_Token)) #2 /Applications/MAMP/htdocs/karmacoop/wp-content/plugins/gantry5/src/vendor/twig/twig/lib/Twig/Parser.php(103): Twig_Parser->subparse(NULL, false) #3 /Applications/MAMP/htdocs/karmacoop/wp-content/plugins/gantry5/src/vendor/twig/twig/lib/Twig/Environment.php(692): Twig_Parser->parse(Object(Twig_TokenStream)) #4 /Applications/MAMP/htdocs/karmacoop/wp-content/plugins/gantry5/src/vendor/twig/twig/lib/Twig/Environment.php(750): Twig_Environment->parse(Ob in /Applications/MAMP/htdocs/karmacoop/wp-content/themes/g5_hydrogen-child/custom/views/partials/content.html.twig on line 19

Can you please explain what I'm doing wrong?? I could swear I'm following all of the instructions in the Gantry Docs. If I could get some guidance I would be eternally grateful!! Besides this issue, I'm really enjoying developing with Gantry! :)

@mahagr
Copy link
Member

mahagr commented Dec 17, 2019

Sorry about the delay in my response.

You need to make sure you get Gantry first by:

$gantry = \Gantry\Framework\Gantry::instance();

Depending on the CMS you're using, when you need to do this differs, unless you do it when the theme initializes inside theme/includes/theme.php.

@ghost
Copy link

ghost commented Jun 18, 2020

I am leaving this code here just for the records, since it is an old issue.

This can be done in a simple way: {{ function('yoast_breadcrumb', '<p id="breadcrumbs">', '</p>') }}
Check Timber functions: https://timber.github.io/docs/guides/functions/
Yoast Breadcrumb can be further customized with filters: https://yoast.com/help/implement-wordpress-seo-breadcrumbs/

@mahagr mahagr closed this as completed May 13, 2021
@mahagr mahagr added this to the 5.5.0 milestone Sep 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants