We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5689
    • 289 Posts
    I often find myself in need of some quick PHP in my templates, but the only way to do that is to make a snippet. Snippets are great for reusable code, but what if I just want one line of PHP in one place only?

    For example, I like to have dynamic years in my copyright notices so I don’t have to remember to change them every year. Here’s how I’d normally do it:
    Copyright © <?php echo date('Y') == '2010' ? '2010' : '2010-' . date('Y');?> Site Name. All rights reserved.

    To make that more clear, if the year is still 2010, then I just want the copyright to show the year 2010:
    Copyright © 2010 Site Name. All rights reserved.
    However, once it’s no longer 2010, the copyright needs to show a range of years:
    Copyright © 2010-2011 Site Name. All rights reserved.
    The specifics aren’t all that relevant to this post, but I just wanted to clarify what the code does.

    Now, specifics aside, my point is that to do this in MODx, I have to make a snippet with the one line of PHP code just so I can use it in that one place and that one place only. I guess you could argue that we’re separating content from logic, but it just feels like a lot of extra work, both for writing the code and being able to read the code when I come back to look at it some other time.

    So, what I’m proposing is some sort of anonymous snippet where you define the snippet code right in the template, chunk, or resource where it’s being used. By "anonymous snippet," I’m thinking along the same lines as an anonymous function. For a quick refresher, an anonymous function is something like in this JavaScript example:
    var someFunction = function() {
        alert("Output some text.");
    };
    someFunction();

    The idea is that you can create a nameless function at runtime right where you need it. My idea for snippets is similar, but there’d be no variable involved--just the snippet. I don’t know if it’d use a snippet tag or some new tag, but it might look something like this:
    [[[<?php
    echo 'Since MODx Revolution supports multi-line snippet calls, writing the code would be just like when you're creating an actual snippet.';
    ?>]? &also=`parameters could still be used like normal`]]

    For my example at least, the snippet code goes inside a third set of square brackets, and the code surrounded by that pair of brackets replaces what would normally be the name of the snippet. That would allow everything else to be exactly the same. You would add the question mark if you had parameters, and parameters would of course work exactly the same way.

    Does that make sense? What are your thoughts? I think this could be awfully useful for people like me who want to use PHP for little enhancements like my dynamic copyright year without creating a whole new snippet every time.

    (By the way, I realize that this would probably only be feasible in Revolution.)
      I&#39;m learning more about MODx all the time and loving it.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      And that defeats the whole purpose of separating presentation from function. There is a snippet, CopyYears, to do exactly what you want, wherever you want, with no need to worry about somebody messing with your code.
      <?php
      // CopyYears - snippet for MODx
      // Automatically updating copyright date
      // License GPL
      // Written 04-2006 by scraf
      
      // &startYear - first year of notice
      //              defaults to current year
      // [!CopyYears?startYear=`2005`!]
      // will display 2005 - 2006 or 2005 - 2007, whatever the current year is.
      
      $spacer = " - ";
      $now = date("Y");
      $startYear = isset($startYear)? $startYear : $now;
      $years = ($now > $startYear) ? $startYear.$spacer.$now : $now;
      return "$years";
      ?>

      See? Four years later, it’s still usable by anybody anywhere.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 5689
        • 289 Posts
        I’m not sure how that has to do with my idea. If I wanted to write a snippet to share with the world, I’d make a snippet and share it. This isn’t about somebody messing with my code, so I’m not sure what you mean by that.

        Besides, a snippet call now leaves some connection between presentation and function. When you set parameters in your snippet call, your giving guidelines for the function, except you’re still writing it in your presentation code. It’s not black and white as it is. To reinforce that point, snippets quite frequently output presentation code, which in no way keeps the two separate. If I echo() something in a snippet, that’s presentation code. It’s probably wiser to have the snippet load a chunk and output that instead, but the possibility is there. And think about the new If snippet for Revolution. That kind of logic that’s configurable in the presentation code is far more function than you’re saying should be allowed there. So there is definitely not a strict separation of function and presentation with MODx.

        As a general rule, I’m all for keeping the functionality separate from the presentation. But like I explained in my first post, it’s kind of ridiculous to make a whole separate snippet for one line of code. Sometimes there are exceptions worth making. Even if I was interested in making code that people could use four years later, there are still going to be plenty of times when the code I write will never be useful to anybody else, and in those cases it would be entirely acceptable for me to "hardcode" some functionality if I wanted to. Heck, even if my code would be useful to other people years later, let them write their own code. You guys have explained pretty clearly that while you want the MODx core to remain open source and free, you’re all for monetizing add-ons. If I choose not to share code I write, that’s perfectly fine. Given that, I think it’s safe to say that the reusable code factor is completely irrelevant to what I’m suggesting here.

        Again, I understand your point about separating presentation and functionality, but this idea doesn’t force a connection between the two. It does leave the option, though, and that’s what’s important, especially since I think a lot of people could find some legitimate uses for this.

        If MODx is truly about doing things the way that works best for you instead of some forced way, then what’s so horrible about the possibility of an "anonymous" snippet? I understand if the idea doesn’t work for you, but I really thought it was at least worth some consideration.
          I&#39;m learning more about MODx all the time and loving it.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Actually, you could write a plugin to parse the documentObject, pull out anything between <?php and ?> and run it through the eval() function.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 14267
            • 113 Posts
            Wouldn’t the @eval binding in a TV do what you’re asking?

            http://wiki.modxcms.com/index.php/@EVAL_Binding

            E.g., make TV called "AnonymousFunction", put it your template or content, and put your line of code in the TV, e.g. "return date(’Y’) == ’2010’ ? ’2010’ : ’2010-’ . date(’Y’);".

              • 28042 ☆ A M B ☆
              • 24,524 Posts
              That’s no different than using snippet tags. It’s still not using the traditional inline php tags.
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 5689
                • 289 Posts
                Sottwell, I’m not looking to parse any and all PHP in my templates. If we could do that, it’d be way too easy to get lazy and just start throwing in PHP all over the place. I just have an issue with having to make a snippet for something really simple for a couple reasons, one being that it’s just more confusing to have little bits and pieces split up all over the place in the manager. It also just doesn’t make sense to my preferred kind of organization when I have to make a snippet that will only ever be used in one specific place. Think about just coding in PHP. If I have a line of code that just needs to be used in one spot, why would I put it in a function? It’s just extra work for no reason. That’s most of reasoning for all of this.

                jlarson, a TV really isn’t any better because it still takes that one line of code and puts it somewhere else when all I want is to have it right there where I’m writing the code around it, if that makes sense. Again, for most cases, I would agree that it’s better to keep the logic separate from the presentation, but as I think we’ve all learned, you can’t follow absolute rules in programming 100% of the time. Sometimes you let yourself break a rule when it makes sense for a certain situation.

                I’m guessing it’s possible to write a plugin to do the anonymous snippets, but it’s not really worth the effort if nobody else is with me on this. I would let to get some input from other people as well, but sottwell, judging by your thoughts, I’m guessing I can give up any hope that this would be made part of the core. :\

                Here’s another real-life example I thought of where I think this would be useful. On a site I’m working on right now, I have two divs for navigation. There’s the main one, #navigation, which contains links to each section of the site. For sections that have subpages, I also have #subnavigation, which comes after #navigation in the HTML and is completely independent of the main navigation menu. I use two Wayfinder calls to handle the two menus. The two divs look something like this (simplified):
                <div id="navigation">[[!Wayfinder]]</div>
                <div id="subnavigation">[[!Wayfinder]]</div>

                I realize that there are other ways to accomplish the same thing, but this is how I’ve chosen to do it. Now, when there’s a section with no subpages, the subnavigation menu shouldn’t show up at all. Right now Wayfinder just doesn’t output anything for the subnavigation menu, but the empty div is still there. I’d like to have the div not be in the output at all. I could pretty easily write some code that gets the parent’s children and checks if there are 0, and if so, then don’t show the subnavigation menu. But I would have to go make a snippet for it even though this is the only place ever that I will use that code. It’s not even something I would likely use in other projects because it’s specifically meant for this situation on this site.

                Do you see what I’m getting at? I think it makes sense in some instances to have code put right in there in my presentation code, if I choose to anyway.
                  I&#39;m learning more about MODx all the time and loving it.
                  • 3749
                  • 24,544 Posts
                  There are some security issues here as well. It makes things a lot safer when MODx refuses to execute any PHP at all unless it’s in a snippet, TV, or plugin that’s stored in the MODx database.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 5689
                    • 289 Posts
                    That’s true.

                    I suppose I’ll just deal with it when it doesn’t feel right and just make the snippet. tongue
                      I&#39;m learning more about MODx all the time and loving it.
                      • 17499 ☆ A M B ☆
                      • 872 Posts
                      Quote from: Compeek at Aug 14, 2010, 12:00 PM

                      Here’s another real-life example I thought of where I think this would be useful. On a site I’m working on right now, I have two divs for navigation. There’s the main one, #navigation, which contains links to each section of the site. For sections that have subpages, I also have #subnavigation, which comes after #navigation in the HTML and is completely independent of the main navigation menu. I use two Wayfinder calls to handle the two menus. The two divs look something like this (simplified):
                      <div id="navigation">[[!Wayfinder]]</div>
                      <div id="subnavigation">[[!Wayfinder]]</div>

                      I realize that there are other ways to accomplish the same thing, but this is how I’ve chosen to do it. Now, when there’s a section with no subpages, the subnavigation menu shouldn’t show up at all. Right now Wayfinder just doesn’t output anything for the subnavigation menu, but the empty div is still there. I’d like to have the div not be in the output at all. I could pretty easily write some code that gets the parent’s children and checks if there are 0, and if so, then don’t show the subnavigation menu. But I would have to go make a snippet for it even though this is the only place ever that I will use that code. It’s not even something I would likely use in other projects because it’s specifically meant for this situation on this site.

                      In that specific case you could have used an output filter:

                      [[!wayfinder:ifempty=``:else=`<div id="subnavigation">[[!wayfinder]]</div>`]]
                      


                      But i recognize that it does not answer all case.