We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40098
    • 9 Posts
    Hi,
    Me and my work colleagues were wondering if it's possible to have a server-side chunk? Hopefully this example will help explain what I'm envisioning:

    On all of our snippets, we use
    #Error Reporting
    ini_set("display_errors", 1);
    error_reporting(E_ALL);
    


    And when we go live with a project, we have to manually go through each snippet and remove/comment out those lines.

    What we were thinking about is what if we had a chunk called 'error-reporting'

    this chunk would contain the error reporting code above, but wouldn't be outputted to the screen, it's only purpose would be to be called in a snippet.
    So our snippet calls would be

    <?php
    echo modx->readChunk('error-reporting');
    ?>
    

    This wouldn't process the chunk, it would only retrieve whatever was in the chunk and use it in the snippet.

    what are your thoughts?
      • 39932
      • 483 Posts
      Yes! It is possible to include php in a chunk. Take a look at RO.IDEs which does the same for Snippets (the link is in my signature). Before you do this, consider the following:


      • Snippets and Plugins are intentionally not PHP for a reason. While RO.IDEs handles this, it does so for a very particular reason.
      • This goes against the best practices instituted by years of MODX developer experience.
      • Most desires to run other PHP code from a Snippet can be offset by the modx class method runSnippet(). This allows you to run a snippet from a snippet splitting your code like you want.
      • Requires use of the php eval function. Research eval() and learn why it can be eval. Search "eval is evil".
      • RO.IDEs uses eval (from another Snippet), but it makes sure all executable code is behind locked doors using Resource Groups. You can't place a Chunk in a Resource Group. You can only implement Element-wide security for Chunks.
      • PHP in Chunks cannot have <?php or ?>. This makes them harder to recognize as PHP. If content editors are using your site, these Chunks will be much more likely to be edited, breaking your site entirely.
      • Including and defining classes and functions is troublesome, requiring checking if the function/class was already included.

      That said, MODX is about creative freedom. If you are going to go against the grain, be sure you know what the considerations are before doing so. Every decision has its drawbacks, and this has more than most if you don't have the proper framework built around your site. This all may sound hypocritical, but before I started RO.IDEs, I made sure that I accounted for all of the above considerations (and many more). For me, the benefits far outweighed following the standard, and this does not apply to everyone. [ed. note: fuzzicallogic last edited this post 14 years, 1 month ago.]
        Website: Extended Dialog Development Blog: on Extended Dialog
        Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
        Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

        Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Actually the PHP include directive is a stream operation, and PHP has stream wrappers that can convert just about any source into a stream (or you can define your own custom stream wrapper), including a database query result.

        http://www.php.net/manual/en/wrappers.data.php

        Here's an interesting use of stream-wrappers https://github.com/teqneers/PHP-Stream-Wrapper-for-Git

        I've been considering this for some time; it could completely change the way snippets and plugins in particular are processed, since with this method, php code from any source can simply be included as if it were a file. The @EVAL TV could also use this method under the hood instead of actually using eval().

          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
          • 33968
          • 863 Posts

          On all of our snippets, we use
          #Error Reporting
          ini_set("display_errors", 1);
          error_reporting(E_ALL);

          And when we go live with a project, we have to manually go through each snippet and remove/comment out those lines.
          You could set up a system setting as follows:
          Key:   display_errors
          Value:  1   // this should be 0 for production
          


          Then in your code:
          $display_errors = $modx->getOption('display_errors',null,0);
          if ($display_errors) {
              ini_set("display_errors", 1);
              error_reporting(E_ALL);
          }
          


          Consider adding that code to a plugin that fires on OnWebPageInit (or something like that) rather than having to include the code in every snippet.
            • 39932
            • 483 Posts
            @Lucas: That is an awesome idea.

            @Susan: I was not aware that include was a stream-op (I don't know why!!). In any case, for RO.IDEs, I wrote an "include" Snippet. One quick note, eval() is particularly only evil for user-defined (specifally unvalidated) input. @EVAL TVs are equivalent to eval(), but is removed from user defined input in the same way that RO.IDEs evals are removed. I really want to start setting up streams for my Chunks now! Thanks Susan! (This is such a bad idea for me. LOL!)...
              Website: Extended Dialog Development Blog: on Extended Dialog
              Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
              Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

              Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
              • 18373 ☆ A M B ☆
              • 3,141 Posts
              Mark Hamstra Reply #6, 14 years ago
              Could also run a different snippet entirely:

              $modx->runSnippet('snippetname',array('proper' => 'ties');


              which I would then suggest to check a system setting on wether or not it sets error_reporting/display_errors smiley
                Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                • 42842
                • 12 Posts
                Hi guys!

                My Question is related to this here so I post it here:
                I want to display this content: http://heartandtruelove.com/vimeo.php?id=123456

                So i created the php into a snippet:
                <?php
                $imgid = $_GET['id'] . $id;
                $hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$imgid.php"));
                echo $hash[0]['thumbnail_medium'];
                ?>
                

                and i can load the snippet like this:
                [[!vimeo_thumb? &id=`123456`]]

                BUT, here comes the problem, I don't get the same thumbnail link like here:
                http://heartandtruelove.com/vimeo.php?id=123456

                My guess was, that I am doing something wrong in the php code, what could it be?
                thanks for fast response! =)