-
- 73 Posts
I have a snippet which grown up a lot and it became messy, Also there are few things that are repeating again and again so i want to write common function for that to optimize the code and make it clean and clear.
Is there any way i can use functions in snippets ?
-
- 483 Posts
Absolutely!!
You have three basic choices:
- You may declare a function as you normally would in PHP (function nameOfFunction). If you do this, and the Snippet is used multiple times, you will want to check if the function is already defined... (if (!function_exists('nameOfFunction')) { ... function in here ... }
- You may also run a Snippet as a function (as well as output). This is for when it might provide output or values for multiple other Snippets. I highlight this in this post. It applies to more than conditions, but that was the topic of the post.
- You may also include a Snippet or function from Resources or static files. PHP include is a stream operation (thanks, @Sottwell!). Here is the post with the relevant links.
Note: There are many more possibilities, but they are typically more complex and for specialized circumstances. The three above cover most situations.
[ed. note: fuzzicallogic last edited this post 11 years, 8 months ago.]
<ahem> If I may be pedantic, it's the "include" directive that is a stream operation. And you can declare custom stream types, so anything can be included, even results direct from the database.
The one thing to keep in mind is that a function in a snippet is like any function, you must keep scope in mind. For example, you'll need to either pass $modx as a function argument, or declare it global at the beginning of your function.
-
- 483 Posts
ROFL. I forgot to say include?! Fixing now... Fixed.
For example, you'll need to either pass $modx as a function argument, or declare it global at the beginning of your function.
Provided that you use the $modx object.
-
- 73 Posts
Ohhh including .php file in snippet is simple thing i must have though about, how could i forgot it before asking in forums ? may be because i didnt had proper sleep since last few days...@fuzzical logic : your idea worked i created .php file and included it in snippet and now i am able to simplify my code. And yes i had to pass $modx as argument.
thanks friends, and sorry for wasting ur time on such a silly question but i am still new to modx so still unaware of the modx possibilities.
You can also create a "static" snippet; this will ask you for the path to the .php file, and you can edit the .php file all you please. MODx will use that path to include the .php file instead of taking the code from the database.
-
- 73 Posts
I guess this is what i have done right now. I created .php file and included it in snippet, so its not stored in database and i can edit it in editor. is this what you are talking.
Not exactly; when you create or edit the snippet, there's a checkbox for "Is Static". When that's checked, a couple of fields appear for the path to the .php file. If you edit in the snippet editing field, it saves to the file, and when you edit the file it will be updated in the snippet field. No need to have a snippet with an include ... directive.
-
- 73 Posts
just saw that functionality....its one more great feature od modx. Its impressive.
-
- 24,544 Posts
One warning about static elements:
It's easy to forget and delete the file thinking that the code is also in MODX (because you've seen it there). It's not in the DB, though and unless the code is backed up elsewhere, you'll lose your only copy of it. Don't ask me how I know.
------------------------------------------------------------------------------------------
PLEASE, PLEASE specify the version of MODX you are using.
MODX info for everyone:
http://bobsguides.com/modx.html