0

My page city.htm

title = "city"
url = "/data/:countryslug/:cityslug"
layout = "default"
is_hidden = 0

[cities]
==
function onEnd()
{
    $this->page->title = '?';
}
==
{% set city = cities.getCity %}

<div class="content">
</div>

Considering I'm getting the data from the component "cities" and that the actual data is retrieved when I call the getCity method in twig how do I dynamically pass a variable to the onEnd() function to inject a custom title based on the content?

Update #1: @Hardik Satasiya I want to set the title from the component but I don't want to call a specific method in onRender() since that assumes that every time I need that component I need to call getCity which is not always the case.

However, your example (method 2) returns

Trying to get property 'name' of non-object

for

function onEnd()
{
    $this->page->title = $this['city']->name; //error
}

Because it's probably not passed to the page like my first attempt. I think there's something missing. I want in this order to

  • set the component and method in twig (NOT in the PHP section)
  • set in the component a var that will be available to the page's PHP section
  • set the title from the PHP section of the page based on the comportment output

Probably not possible due to how the page cycle is designed?

Sandro Antonucci
  • 1,683
  • 5
  • 29
  • 59
  • 1
    due to `page cycle design` TWIG code can not run before `PHP Section` and i guess its obvious template code will run at the end. so you have to make some other choices. – Hardik Satasiya Jul 20 '20 at 18:47

1 Answers1

2

There are 2 ways of accessing city.

  1. Very easy one if you don't want other features
function onEnd()
{
    $city = $this->components['cities'].getCity();
    $this->page->title = $city->name;
}

2. If you need more control and reuse variable at other places

In your cities component onRender method

public function onRender()
{
    // to just share variable to page
    $this->page['city'] = $this->getCity();

    // to share variable top page and component partial
    $this->city = $this->page['city'] = $this->getCity();
}

Now, from page code section

function onEnd()
{
    // $this->page['city'] <- we do this in component 
    // So, here we can access it directly   
    $this->page->title = $this['city']->name;
}

This should solve your issue.

If any doubt please comment.

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40
  • Thanks! I totally forgot you could pass data from the component. I'm just starting with October :) – Sandro Antonucci Jul 18 '20 at 11:46
  • Wouldn't this the call *getCity()* twice? When you call it from the onRender() method and from twig with % set? – Sandro Antonucci Jul 18 '20 at 13:52
  • Anyway your suggestion is not working for me. Every variable I try to set with *$this->page['var']* is not to passed to the page. Wether if I set it inside the getCity() directly or in the onRender(). – Sandro Antonucci Jul 18 '20 at 18:56
  • no if we are passing `city` from component then you don't need to call `getCity`. city will be already there. can you share your code how you are setting it as it should work according to October CMS's doc – Hardik Satasiya Jul 19 '20 at 14:07
  • I selected this as best answer cause in the end I had to call the component from like the "uncool option" since the second method assumes I always call getCity which is not true. Also onRender() in the component is called when you use {% component "myComponent" %} which I'm not using. – Sandro Antonucci Jul 27 '20 at 12:13
  • 1
    sorry I should be removing that `uncool` word as you are correct some time you dont want call methods from `onRender` and you dont have option so it does not amke it uncool ;). both things are COOL :) – Hardik Satasiya Jul 27 '20 at 12:39