Jump to content

Recommended Posts

Posted

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!

Posted

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
Posted

I never used twig but I think that the problem is that $page->template itself.

So you could try: 

 {% if page.template.name == "project" %} 

Posted

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

  • Like 1
  • 1 year later...
Posted

I know, this topic is not up to date, but I got the same problem and here is my solution:

{{page.get('template').get('name')}}
  • Like 1
Posted

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. 

 
Posted (edited)

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
Posted

Perhaps this has got to do with the version of Twig being used? In your case, Twig 1.15 is being used, whereas the latest TemplateTwigReplace comes with 1.18, which is what I'm using.

Posted

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
  • 1 year later...
Posted

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?

Posted

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

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
×
×
  • Create New...