Just a suggestion for improvement. The Twig function you have implemented uses the name counter. I think this could be a problem for two reasons:
it's close and easily confused with Twig's count filter
it's a common word in the "global scope" - what I mean by that is that generally it's good to reduce the possibility of names clashing by using prefixes. If another plugin added a Twig count function (which it shouldn't either!), or more likely Grav core did, undefined behaviour might ensue.
I don't think there is a convention for the Twig prefix format used by Grav plugins, so you could probably simply change the name of your count Twig function to ipcounter() if you want to keep it simple.
Here's a more through but annoying technique I've used in other plugins which might work for you:
listen for the event 'onTwigSiteVariables': 'onTwigSiteVariables' => ['setUpTwigVariables', 0],
define an instance of your plugin's main class under the Twig variable grav.plugins.ipcounter using this function:
public function setUpTwigVariables() {
$twig = $this->grav['twig'];
if (!isset($twig->twig_vars['grav']['plugins'])) { // in my opinion, this should be the convention, but it may not exist
$twig->twig_vars['grav']['plugins'] = [];
}
$twig->twig_vars['grav']['plugins'][$this->name] = new IPCounter();
}
By adding just a class instance, you allow yourself to expose any of its public methods in future without having to mess with Twig setup.
In your template, to get the stats counter, you can now use {{ grav.plugins.ipcount.getStats.count }}. You could also add an IP from Twig BTW, since that is a public method.
Whether you choose a simple or complex option for your Twig function name, you'll have to support your existing count() Twig function for a period at least, so that nothing breaks. Maybe consider adding a plugin config option so that developers can switch that Twig name off if it conflicts with something they are using.
And of course all of this is just feedback and suggestions and completely optional! :)
Just a suggestion for improvement. The Twig function you have implemented uses the name
counter
. I think this could be a problem for two reasons:count
filtercount
function (which it shouldn't either!), or more likely Grav core did, undefined behaviour might ensue.I don't think there is a convention for the Twig prefix format used by Grav plugins, so you could probably simply change the name of your count Twig function to
ipcounter()
if you want to keep it simple.Here's a more through but annoying technique I've used in other plugins which might work for you:
'onTwigSiteVariables' => ['setUpTwigVariables', 0],
grav.plugins.ipcounter
using this function:By adding just a class instance, you allow yourself to expose any of its public methods in future without having to mess with Twig setup.
In your template, to get the stats counter, you can now use
{{ grav.plugins.ipcount.getStats.count }}
. You could also add an IP from Twig BTW, since that is a public method.Whether you choose a simple or complex option for your Twig function name, you'll have to support your existing
count()
Twig function for a period at least, so that nothing breaks. Maybe consider adding a plugin config option so that developers can switch that Twig name off if it conflicts with something they are using.And of course all of this is just feedback and suggestions and completely optional! :)