Jump to content

Global variable or pass variable through render()


combicart
 Share

Recommended Posts

I'm currently building a website where I inline some SVG icons directly into the template. I'm using https://github.com/oscarotero/inline-svg for inlining the icons.

I've added the code below to my _init.php file to set where all the icons are stored:

$icons = Collection::fromPath('path/to/svg/files');

Inlining an actual SVG icon can be done with the code below. In the example below we are adding an SVG icon with the name 'edit':

echo $icons->get('edit');

So far everything is working as it should. For bigger parts of the template i'm using delayed output and i'm passing the 'icons' variable into the separate template files, like below:

$content = wireRenderFile('partials/homeFacilities', ['icons' => $icons]);

This outputs the icons inside the partials. For setting up details of a page i'm using the ProFields Repeater Matrix module and this is where i'm getting an error. I have to pass the 'icons' variable into the Repeater Matrix template files, however I can't seem to set it correctly so that the template files have access to this variable.

The Repeater Matrix fields is setup like below:

foreach ($value as $item) {
    echo $item->render();
}

Is there a way to pass the 'icons' variable into the Repeater Matrix template files? Or would it e.g. be beter to make this variable accessible globally?

Link to comment
Share on other sites

Since the icons seem to be used globally and everywhere, its probably best to go through something global to distribute it.

I'd probably go through $config:

$config->icons = Collection::fromPath('path/to/svg/files');

So you're able to access it everywhere from $config->icons or wire('config')->icons inside a class.

  • Like 3
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...