Jump to content
bernhard

How to pass all API variables to $files->render() ?

Recommended Posts

I'm working on a new markup module and want to have the API available in the included file. I know I can do that:

$code = $this->files->render('myfile', [
  'page' => $this->page,
  'pages' => $this->pages,
  ...
]);

Then I'd have $page and $pages in my file, but I'd need to list all available API variables manually and keep them in sync with the API.

Other solution:

$code = $this->files->render('myfile', [
  'that' => $this,
]);

Then I'd have all API variables available from $that->page, $that->pages etc.

Can I get best of both? Thx 🙂 

Share this post


Link to post
Share on other sites

@bernhard if you are using ProcessWire 3 and if useFunctionsApi enabled you can call all api functions in rendered files directly.

  • Like 3

Share this post


Link to post
Share on other sites

Use wire('all')

Example from our production code:

$factory = $modules->get('TemplateEngineFactory');
echo $factory->render('chunks/bodyCta.tpl', array_merge(
    (array) wire('all'),
    (array) [ "title" => $title, "text" => $text, "href" => $href ]
));
  • Like 6

Share this post


Link to post
Share on other sites
1 hour ago, Markus (Blue Tomato) said:

Use wire('all')

Awesome! Exactly what I was looking for, thx 🙂 Maybe also interesting for you @kongondo (or are you already using it like this in your module?)

Thx for the idea @ukyo but I need that feature for backend development and I can't rely on the functions API and it's also only recommended for use on the frontend.

Share this post


Link to post
Share on other sites
On 7/12/2019 at 4:16 AM, bernhard said:

I'm working on a new markup module and want to have the API available in the included file.

From the $files->render() docs:

Quote

Please note that all template files automatically receive all API variables already (you don't have to provide them).

 

  • Thanks 1

Share this post


Link to post
Share on other sites
On 7/21/2019 at 6:41 AM, Robin S said:

Please note that all template files automatically receive all API variables already (you don't have to provide them).

Thx Robin 😅 Didn't know that, just tried it and it also works for $files->include() 🙂

  • Like 1

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...