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).

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...