Skip to content
This repository has been archived by the owner on Jan 25, 2021. It is now read-only.

Be able to add field assets conditionally #1058

Closed
jenstornell opened this issue Jun 14, 2017 · 4 comments
Closed

Be able to add field assets conditionally #1058

jenstornell opened this issue Jun 14, 2017 · 4 comments
Labels

Comments

@jenstornell
Copy link

Now it looks like this in a field:

static public $assets = array(
  'css' => array('style.min.css'),
  'js' => array('script.min.js'),
);

Related: https://forum.getkirby.com/t/set-field-asset-with---construct/7005

As far as I can tell there is no way to add assets conditionally. Let's say it make sense to do this:

if(c::get('debug')) {
  static public $assets = array(
    'css' => array('style.css'),
    'js' => array('script.js'),
  );
} else {
  static public $assets = array(
    'css' => array('style.min.css'),
    'js' => array('script.min.js'),
  );
}

syntax error, unexpected 'if' (T_IF), expecting function (T_FUNCTION)

or...

static public $assets = array(
  'css' => array('style.' . c::get('my.plugin.color') . '.css'),
  'js' => array('script.js'),
);

Constant expression contains invalid operations

Because it's set as static variable, we can't control it. Maybe it could be used as a static asset (like it is now) OR be overwritten by the __contruct somehow?

@lukasbestle
Copy link
Member

Erm, what about this?

class MyField extends BaseField {
  static public $assets;
}

if(c::get('debug')) {
  MyField::$assets = array(
    'css' => array('style.css'),
    'js' => array('script.js'),
  );
} else {
  MyField::$assets = array(
    'css' => array('style.min.css'),
    'js' => array('script.min.js'),
  );
}

@jenstornell
Copy link
Author

jenstornell commented Jun 15, 2017

Thanks, that works brilliantly to my current project. I will probably add it as a Kirby Secret. :)

About the issue, do you think this is the solution we should use in the future?

  • It's not very straight forward and easy to figure out, I think.
  • The variable is set outside the class. It's like something fell out of the box.

So I still hope for some improvement in the panel core regarding this in the long run.

@lukasbestle
Copy link
Member

I agree that this shouldn't be used in production plugins and fields.

My recommendation: Always use minified assets. It doesn't make sense to have this conditional check only for local field development. You can always use source maps for debugging.

@jenstornell
Copy link
Author

Thanks for the suggestion about source maps.

About the issue. If we need a more dynamic css, like using a color set in the config.php, you think it should be done like the code below, outside the class? Or do you think there is a way in the future to make it a part of the class?

class MyField extends BaseField {
  static public $assets;
}

MyField::$assets = array(
  'css' => array('style.' . c::get('my.plugin.color') . '.css'),
  'js' => array('script.js'),
);

If you think it would be nicer to have it inside the class, reopen this issue?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants