Jump to content

Twig, accessing page values


Manaus
 Share

Recommended Posts

In my template I have set a variable called 'noindex'.

Now, in my Twig template I cannot seem to use a conditional like this:

{{ if page.noindex }}
<do this>
{{ endif}}

because "Method Page::noindex does not exist or is not callable in this context"

Should I put all the values within a `view` object, and pass it to the template? Is there a shortcut? I remember that within Smarty it is permissible to use `{ page->index }` within a template, and it works.

Thank you very much

Link to comment
Share on other sites

2 hours ago, Manaus said:

In my template I have set a variable called 'noindex'.

like:

{% set noindex = 'via twig' %}

or in a noindex field in your template?

 

So... {{ page.noindex }} would look for a field called noindex in your page template and would output it's value.

I use TemplateEngineFactory / Twig and it would throw this kind of error message in case the field/value does NOT exist - either in page context or via $view.

So the questions is:

What kind of field/fieldtype is noindex or is it a value you get from somewhere else?
If it was a select/option you might have to look for {{ page.noindex.value }} or maybe {{ page.noindex.label }}.
If it was a boolean you would work with your kind of of setup and would just output the value you need.

My best guess actually is, that the field/value doesn't exist in page context because either the field is not in that template or you try to render a component/include, which might be out of scope.

Can you please tell me a bit more about your setup of fields and templates.

 

Link to comment
Share on other sites

Thank you all, I checked and the value does not exist. Since the code that gives problems is used inside an included file (a header) I should foresee whatever case arises: existence or non-existence, etc.
Da2's solution works, but I wonder if Twig crashes so easily on a typical conditional case, and if there isn't a pure Twig approach to use.

Thanks

 

  • Like 1
Link to comment
Share on other sites

I reported this behavior to the dev who created the Twig module for PW and the given solution is to use get().

I hate some of the Twig behaviors and consider switching to another solution in the future.

Another thing to know is that, when you call page.noIndex, if not found Twig will also call method page.getNoIndex(), property page.getNoIndex and a bunch of other calls like that...

  • Like 1
Link to comment
Share on other sites

There is a difference between:

  1. field does NOT exist in template
  2. field does exist but has NO value

You can check a lot with is defined - see here: https://twig.symfony.com/doc/3.x/tests/defined.html

{% if page.field is defined %}
    ...
{% endif %}

Another thing in TemplateEngineFactory / Twig you can uncheck Strict variables to make life a bit easier.

twig.png.c88717cf8af8fd3696160d9c028de72b.png

Yet another thing could be your include is out of scope and you might need to include with instead - see here: https://twig.symfony.com/doc/3.x/tags/include.html

{% set vars = {'foo': 'bar'} %}
{% include 'template.html' with vars %}

 

  • 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...