-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple Icon Templates #149
Comments
That's an interesting idea, maybe a better idea for the templates would be to append something to 'icon' => '<i class="fa-{{type}}{{attrs.class}}"{{attrs}}></i>',
'icon-s' => '<i class="fas fa-{{type}}{{attrs.class}}"{{attrs}}></i>',
'icon-b' => '<i class="fab fa-{{type}}{{attrs.class}}"{{attrs}}></i>' And when parsing the easy-icon, you get The following regex should work for parsing the icon:
Something could be achieved by backing the |
I agree that the entry point to this functionality would be HtmlHelper's |
Options I considered:
Have you thought about reversing how easyIcon works to make the interface less ugly? Consider replacing the call: $this->_easyIcon('parent::link', 0, 2, [$title, $url, $options]); with: $this->Html->injectIcon($title, parent::link($title, $url, $options)); Now what Signature:
Advantages:
What do you think about this idea? |
This sounds interesting, I'd rather have this functionality in a You'd need to set templates for I think we would need something to disable easy icon if needed, maybe: $this->Icon->inject = false; // another name maybe But it would be quite difficult to separate "automatic" injection from manual one... Maybe something else than a simple switch? I don't mind if this is not as simple as switching a variable to true/false since this is probably not used that often, I'd rather have something clean for this (if we make the icons clean, why not make everything clean ? 😃). The regex for the pattern would need to be changed since it actually only matches beginning of string / pattern between spaces. At first glance this does not look too complex (would simply need to match |
Regarding the regex, my idea was to apply it on $title and then do a |
I am not fan of this idea because:
I'd rather remove the |
So the two issues you raise are argument duplication and how to deal with escaping.
My counter argument for parsing the whole output is that people might have weird tag options where an 'i:something' slips in (e.g. data attributes). A test for I am just putting out my thoughts here, I am happy to implement either approach, just name it. Regarding an easy way to disable it, obviously we can have $this->Form->button('i:1 j:2', ['icon' => false]); but this would lead to quite some extra work for the FormHelper (take icon option from option array, unset it in option array, pass it to injectIcon…), so it's probably not what we want after all. |
I am answering the last part of the comment first — I agree it would be great to have a way to disable the injection case-by-case, even though I think I think there is a simple way to disable some options from being parsed as an attribute: we need to override
Using a custom (documented) option name so that other options (e.g. This Regarding the parsing now, I agree that this may raise some issue, but as I said, I think we could have a "standard" rule, e.g. very generic, and then user would disable the injection when needed. Something very generic could be:
We could also allow user to set a specific pattern if they want (documenting it correcty), so they could do: // Current one:
$this->Icon->setMatchingPattern('(^|[\w\W\d]+)i:([a-zA-Z0-9\\-_]+:)?([a-zA-Z0-9\\-_]+)([\w\W\d]+|$)');
// New one?
$this->Icon->setMatchingPattern('(^|[^\p{L}\p{N}]+)i:([a-zA-Z0-9\\-_]+:)?([a-zA-Z0-9\\-_]+)([^\p{L}p{N}]+|$)');
// Disable?
$this->Icon->setMatchingPattern(false);
// Enable? Falls back to the default one?
$this->Icon->setMatchingPattern(true); |
Great idea of allowing matching pattern customization, I would add that I think for the user it is cleaner to have a setMatchingPattern() / enableMatching() / disableMatching() interface, as it is self-explanatory. |
Yes, it would probably be better, I have to check CakePHP's convention for this if there are any (maybe |
Just checked, it is |
I think we could use named capture group in the regex so that user can customize how template are chosen depending on the pattern, e.g.: // The <type> group is the special one used to select the actual icon.
$pattern = (^|[^\p{L}\p{N}]+)i:(?<type>[a-zA-Z0-9\\-_]+)([^\p{L}\p{N}]+|$);
$templatePattern = 'icon';
// New pattern, {{shape}} will be replaced by the matching:
$pattern = (^|[^\p{L}\p{N}]+)i:((?<shape>[srb]):)?(?<type>[a-zA-Z0-9\\-_]+)([^\p{L}\p{N}]+|$);
$templatePattern = 'icon-{{shape}}'; The method This way, this is extremely customizable, I could even use something like: $pattern = (^|[^\p{L}\p{N}]+)i((?<toolkit>[srb]):)?:((?<shape>[srb]):)?(?<type>[a-zA-Z0-9\\-_]+)([^\p{L}\p{N}]+|$);
$templatePattern = 'icon-{{toolkit}}-{{shape}}'; To switch between FontAwesome and e.g. Glyphicon simply by doing Maybe we would need to find a way to find the template pattern when one of the matching group is not found, but that's something to consider I think. One idea would be to force a leading hyphen |
I created a |
So this sounds like a version of So in summary:
Outside IconHelper:
If you agree with this I will start working on the code. Just as a remark I think the possibility of translating regex captures to template variables might be a bit of an obscure feature for this specific context. |
I think
Yes, and templates would be in the
You mean have
Yes, let's start with something matching
Yes, I don't know if
Yes,
Yes, this can be easily done by calling
Yes.
Yes.
Maybe, but you'd be surprised by the number of obscure feature in this plugin 😀 I don't think that much people are even aware of the easy icon feature. As long as this does not disturb the basic usage of this feature, I'm fine with adding this. |
Ok, so I will be working on this. |
@ypnos-web After closing the above-mentioned PR, I was thinking about a nice feature that we could implement using class IconHelper {
public function icon($type, $options = [], /* Extra? */) {
$options += [
'template' => $this->getConfig('defaultTemplate') // Something like this?
];
// Whatever...
}
public function __invoke() {
return call_user_func_array([$this, 'icon'], func_get_args());
}
public function __call($name, $arguments) {
// Function name -> Template
$result = $this->nameToTemplate($name);
// Update arguments...
$arguments[1]['template'] = $result;
return call_user_func_array([$this, 'icon'], $arguments);
}
}; Assuming $this->Icon->icon('home'); // Default icon
$this->Icon('home'); // Same as above, less verbose.
$this->Icon->fa('home'); // Use icon-fa template?
$this->Icon->faS('home');; // Use icon-fa-s template?
At first, I thought about using |
This is a (multiple allowed):
Since Fontawesome 5 got released, I am faced with the distinction between icon shapes 'solid', 'regular', 'light', and 'brand'. These are selected through a supplemental class 'fas', 'far', 'fab' instead of simply 'fa' (which falls back to solid). While this is mostly a Fontawesome Pro feature, it also affects free users as some regular icons are free to replace the previous '-o' icons, and then there are the brand icons.
To be able to selectively use a specific shape we would need several icon templates, not only a default one. E.g.:
Then we could enhance the magic icon syntax:
Well the first problem with this idea is that templates reside in a flat array so my nesting would not work.
But just for the general idea.
What do you think about it and what design considerations would you have?
I would be happy to prepare a pull request for this feature.
The text was updated successfully, but these errors were encountered: