Hi All,
As we now many same functions/algorithms/validators can be used by different snippets(extra), so is there some mechanism in Revo to share or collect that code in single place to use in different places?
If so, could anyone show some examples how to do this.
Thanks.
I don’t know of one, though that doesn’t mean someone hasn’t written it. Most add-ons install all the usable functions and validators they can use.
Quote from: Sedgar at Feb 20, 2011, 01:12 AM
Yes, this mean if i create several snippets with email validator, each will contain appropriate identical code fragments for that purposes 
You can "include" them from a file as you would in any php code.
You can also use FormIt, which has a built-in email validator and will let you create custom validators that can be used in other forms.
Another method is to make them separate snippets and call them with $modx->runSnippet().
Nice idea, but how to integrate it with modx ?
Just make a file and include it in your snippets.
Same file copied for all my snippets? The questions is how to share/install one instance of library utils in modx between different snippets.
The one way which I see now is to create special snippet something like to Common Utils Snippet and in remarks add it as required for all my snippets...
just use include at the top of your snippets
<?php
include (path/to/myUtils.inc.php);
echo utilFunction($string);
What Cipa meant was to put all the functions in a single file and just put this line in any snippets that need functions from the file:
As I said, you can also make a separate snippet out of each function and use $modx->runSnippet(’SnippetName’, $parameters). That way, they won’t be included unless they are needed.
TBH, MODx and PHP between them have so many built-in methods that I’ve never felt the need to create a separate collection and I’m generally pretty enthusiastic about code reuse. YMMV.