Jump to content

twig and "if" error


Manaus
 Share

Recommended Posts

Hi,

I'm having an error "Method Page::template does not exist or is not callable in this context" because I'm using {% if page.template == "project" %} in a template.

I'm passing $twigvars = array("page" => $page) to the template.

Is there any workaround for getting this conditional on a template? (started learning twig yesterday... :)

Thanks!

Link to comment
Share on other sites

Hi Manaus,

how about adding page.template to $twigvars before the call, eg.

$twigvars = array("page" => $page, "template"=>$page->template->name);

then in your twig template having:

{% if template == "project" %}

disclaimer: i haven't tried this, i'm just thinking out loud.

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

This works too:

{{ page.template }}

no way! This leads to:

Error: Exception: An exception has been thrown during the rendering of a template ("Method Page::template does not exist or is not callable in this context") in "partials/nav-sub.twig" at line 3. (in /../public/site/modules/TemplateTwigReplace/Twig-1.15.0/Twig/Template.php line 291)

I'm just able to debug this statement {{dump(page.template)}}.

See the reason above:

The error message suggests that Twig is trying to call $page->template as a method, like $page->template() -- which doesn't exist. 

 
Link to comment
Share on other sites

Well, it works for me on the dev branch (2.5.28) with the latest TemplateTwigReplace... Perhaps a magic method was recently added.

Update: This seems to be what's doing it (from core/Template.php)

/**
 * The string value of a Template is always it's name
 *
 */
public function __toString() {
	return $this->name; 
}

Not sure when it was added though... (Seems like it's there in 2.5.3 too...)

Edited by Mike Anthony
Link to comment
Share on other sites

I downloaded and installed the module yesterday so it should be up to date. But I did not notice that you be able to choose the twig version in module settings. Default is set to 1.15.0. I changed the version to 1.18.0 but I still get the same error message. Maybe it depends on the ProcessWire version!!, I use 2.5.3 in this project.

Error: Exception: An exception has been thrown during the rendering of a template ("Method Page::template does not exist or is not callable in this context") in "partials/html-end.twig" at line 25. (in /../public/site/modules/TemplateTwigReplace/Twig-1.18.0/Twig/Template.php line 304)

Edit: It works on the dev branch using twig 1.18.0  ;)

Thanks for your feedback  :rolleyes:

  • Like 2
Link to comment
Share on other sites

  • 1 year later...

I tried to use the TemplateEngineTwig in PW 3.0.22 (devns). It did not work. Then I downloaded the latest version of twig from their website and replaced the lib folder in the modules\TemplateEngineTwig folder but I still get the same error:

Error: Exception: An exception has been thrown during the rendering of a template ("Method Page::homepage does not exist or is not callable in this context") in "logspot_profile.twig" at line 20. (in K:\xampp\htdocs\logspot-pw\src\site\assets\cache\FileCompiler\site\modules\TemplateEngineTwig\TemplateEngineTwig.module line 111)

Here is what I try to do:
 

 {% if page.homepage %}
        <a href="{{ page.homepage }}" target="_blang">Homepage</a>
            {% endif %}

Anyone got a simple solution?

Link to comment
Share on other sites

Found the solution by myself. Maybe this helps others. It works if you use the so called subscript syntax

From the Twig docs: You can use a dot (.) to access attributes of a variable (methods or properties of a PHP object, or items of a PHP array), or the so-called "subscript" syntax ([]):

So instead of using

{% if page.facebook %}

Just use

{% if page['facebook'] %}

and it works :)

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...