Jump to content

How to Add An Array to $config?


bytesource
 Share

Recommended Posts

I want to add an array variable to $config to store file paths, but is does not seem to work:

$config->minify = array();
$config->minify[] = 'hello';
$config->minify[] = 'world';
echo count($config->minify);  // => 0
 

On the other hand, adding a string works as expected:

$config->minify = '';
$config->minify .= 'hello';
$config->minify .= 'world';
echo $config->minify;  // => helloworld
 

I am still new to PHP I am probably not seeing the obvious, so I would be glad if someone could point me in the right direction.

Best regards,

Stefan

Link to comment
Share on other sites

Hi Stefan,

$config->imageSizerOptions = array(
	'upscaling' => false,
	'cropping' => true,
	'quality' => 90
);

$config->minify = array(
	'hello',
	'world',
	'some',
	'more',
	'text',
	'end'
);
should work.

it is equivalent to:

$config->minify = array(
	0 => 'hello',
	1 => 'world',
	2 => 'some',
	3 => 'more',
	4 => 'text',
	5 => 'end'
);
  • Like 1
Link to comment
Share on other sites

Hi Horst, 

Your suggested approach does indeed work:

$config->minify = array(
  'hello',
  'world');
echo count($config->minify);  // => 2
 
 
However, I would like to start with an empty array and add items dynamically. This does not seem to work with $config.
 
Also, I would like to take a look at the implementation of $config, but couldn't find any automatically created API documentation of all Processwire objects.
 
Cheers, 
 
Stefan
Link to comment
Share on other sites

$config is WireData so you could

$config->minify = new WireData();
$config->minify->set("test","hello world");
 

Or use an php array and set it to the minify config

$minify = array();
$minify[] = 'test';
$minify[] = 'world';
$config->minify = $minify;
 
  • Like 2
Link to comment
Share on other sites

I have one additional question:

 
Is there a way to add a variable to $config that behaves like $config->styles?
 
Example:
$config->styles->add('default.js');
$config->styles->add('custom.js');
$config->styles->add('custom.js');
$config->styles->add('custom.js');

foreach($config->styles as $style) echo $style . ' ';
// => default.js custom.js
// no duplicates returned
 
 
Cheers,
 
Stefan
Link to comment
Share on other sites

That works brilliantly:

$config->minify = new FilenameArray();
$config->minify->add("test.js");
$config->minify->add("test2.js");
$config->minify->add("test.js");
foreach($config->minify as $min) echo $min . ' ';  // => test.js test2.js 
 
 
Thanks a lot! That is exactly what I wanted to achieve.
 
Cheers, 
 
Stefan
Link to comment
Share on other sites

Hi Ollie, 

I am not using the Processwire minify module, but this solution: https://github.com/mrclay/minify

In the HTML the different javascript files  need to be output like this to trigger the minification (works also with CSS files):

<script type='text/javascript' src='/min/?b=sovonex/site/templates/js&f=jquery-1.9.1.min.js,tabs-accordion/index.js,tabs-accordion/jquery.ba-resize.js,tabs-accordion/tabs-accordion.js,jquery.placeholder.min.js,include.js'></script>
 

Cheers, 

Stefan

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