We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5689
    • 289 Posts
    Hmm. That should work for what I need. It’s a bit repetitive because I’m going to be typing the same Wayfinder call twice, but it’s better than making a separate snippet to handle it. Thanks!
      I'm learning more about MODx all the time and loving it.
      • 32674
      • 101 Posts
      Quote from: Compeek

      I often find myself in need of some quick PHP in my templates
      Quote from: Compeek at Aug 15, 2010, 08:16 AM

      Hmm. That should work for what I need. It’s a bit repetitive because I’m going to be typing the same Wayfinder call twice.....
      Quote from: Compeek

      I realize that there are other ways to accomplish the same thing, but this is how I’ve chosen to do it

      Well, as Susan noted already -- the entire design, and point of a snippet, is to type code in once, and use it everywhere.

      That would be perfect for your Date example above, as the same snippet could then be used across 1000’s of different site templates without change, and without typing in the same php code on every single one of them.

      "Once and only once" - http://www.phppeanuts.org/site/index_php/Menu/377/pntScd/d/Once+and+only+Once.html - is a simple but profound development principle in software. (It’s a.k.a. ’Don’t repeat yourself’ http://en.wikipedia.org/wiki/Don%27t_repeat_yourself ; & ’Duplication is Evil’ http://www.joshisgross.com/blog/php-best-practices-1-duplication-is-evil are the same principle’s too)....

      ....but if typing the same php code in, in more than one place, is the coding style youre looking for--- you can do it that way too with modx. wink Just keep in mind that Modx was conceived and designed to try to avoid such things, so it’s probably going to be quite a bit more work to do it your way. smiley

      I personally would NOT like to see this implemented in any way in modx, as it would mean we would be back to html & php code mixed on the same page. Lord knows I’ve already inherited and fixed far too many of those kinds of websites in my lifetime. sad YMMV depending on how sick you are of fixing duped code and trolling through the chaos of combined html + code.
        • 5689
        • 289 Posts
        I’ve made it as clear as possible that I would only use this for code that I just need in one specific place in one specific project. tongue That doesn’t violate the "once and only once" principle or whatever other fancy name you want to give it. Like I said, I’m all for keeping logic separate from the UI, and I’m all for writing reusable code.

        I suppose the copyright date wasn’t the best example because it is something that could be used more than once. And again, I’m with you on avoiding a mix of PHP and HTML. But I still think there are a few instances when it would be beneficial. If you disagree, that’s fine since we all have a different way of doing things, but I didn’t write up this suggestion to throw away what’s been proven to work in programming. tongue
          I'm learning more about MODx all the time and loving it.
          • 32674
          • 101 Posts
          Well the problem is: How can you make sure your Anon snippet feature is only allowed once per template? or once per site?

          ’Cause once you open up the can of worms that is ’mixing php code & html’ to users, it will surely be abused. That’s because it’s always easier to learn to ’copy+paste’ some code into the exact place where you want it, rather than to learn to use a snippet and separate code from html. (After all, isn’t the very reason you want that feature available in modx: because it’s easier for you? wink )
            • 5689
            • 289 Posts
            Look, I’m not here to argue. I just thought it might be useful, but it’s pretty clear that there are more reasons not to have it than to have it. That’s fine, I was just throwing it out there in the first place.
              I'm learning more about MODx all the time and loving it.
              • 32674
              • 101 Posts
              Not trying to argue or be an a-hole -- just trying to point out, that in the long run, your feature request will void all the work the modx designers have done to cleanly separate html from php code in Revo.

              IMO, that’s a pretty clear step backwards, as lots of other CMSs already allow you to combine your code + html as you want to do (and those are a total P.I.T.A. to work with-- again, IMO)



                • 5689
                • 289 Posts
                I get what you’re saying. Really, it’s just kind of an OCD kind of thing for me since I like everything to be in order and make sense, and having to put a simple piece of code in some other part of the manager just so I can have it in this one spot kind of bugs me.

                But I agree with you. Going along with what MODx is all about, I don’t think this would be a good idea after all because like you said, it’d make it too easy for people to become dependent on it.
                  I'm learning more about MODx all the time and loving it.
                  • 32674
                  • 101 Posts
                  LOL. That’s funny, Im pretty OCPD too, and seeing code mixed in with the html annoys the crap outta me, every time. laugh

                  Thus proving ’One man’s pain, is another man’s pleasure’, huh? laugh

                  Maybe stepping back, and up, a level of abstraction, and thinking of the whole snippet thing as: ’all your php code’ in ’one and only one’ place -- the modx database -- helps? Prolley not though, ;o)~
                    • 5689
                    • 289 Posts
                    Haha, probably not, but it usually does help me if I can just change how I think about something.

                    I tend to think of snippets as having proper names and being standalone libraries, if you know what I mean. A snippet serves some broad purpose, pretty much like the reusable-code point everybody was making earlier. That’s why I kept saying I was with you all because I completely agree.

                    But when I just make a quick snippet for some PHP that I need, I’m not going to give it a formal name or think of it like a software library, so to me it doesn’t seem like a snippet.
                      I'm learning more about MODx all the time and loving it.
                      • 32674
                      • 101 Posts
                      Alright then, maybe think of it this way. (I know I won’t convince you, but just for grins.... laugh) ---

                      Say you were designing another modx website, if you were to copy over just your snippets table, to the new database, you’d instantly have all the php code you’ve written for modx--- all available and ready to be reused in the new website’s pages. That’s because you’d have all your code in OAOO (one and only one) place in modx. (makes it easy to backup all your php code in one shot, too, btw.)

                      Even more productive, would be to have situation where you admin 10-20 websites, all sharing common snippet code. When you need a new snippet for them -- just create your new snippet in one of the 10-20 site’s database. Then run a SQL update statement on all the rest of the sites to sync them all. Manually add in your tags to your templates, and youre done.


                      (of course, if you wrote plugins code, or other custom modx code, you’d obviously have to include bringing over those tables too, not just the snippets table, to have all your code in the new sites. And you’d also need to alter the foreign key values in any imported table to match the db it’s in, too.)

                      But you get the gist of it. Blah. Blah. Yadda. Yadda. Blah.... ...no need to rewrite any code, just import table(s) data, reset FKs, adjust tags -- so long as you keep it all code in snippets, and not on pages, or in templates.