We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51106
    • 39 Posts
    Hello i have a question about Modx how can I use PHP code in modx because I have tried to use snippet but it doesn't work even doesn't save php code thank you.
      • 51106
      • 39 Posts
      Hello i have a question about Modx how can I use PHP code in modx because I have tried to use snippet but it doesn't work even doesn't save php code thank you.
        • 46886
        • 1,154 Posts
        Make sure you are using it right. Snippets do work.

        For snippets you have to make them and then call them in your templates.

        First, go into elements or whatever section its called and make a new snippet, the ? or <? at the beginning of the code is there for you already. That's where you put the php and save the code there.

        Then, go into your template or a resource page and put in the *call* to that snippet, I am not handy with these but its something like [[$mySnippet]], pls check around for the proper code for snippet call.

        Anyway that's how they work, you can use php at that part of your page and get an output according to some variables. Now your snippet is available to stick anywhere you want, at the template level or in individual pages. smiley

        If you say it isn't saving then you are probably sticking the php in the wrong place, and the system is stripping it out. That could be your html editor which can be a different issue.
          • 46886
          • 1,154 Posts
          Ah, I see everyone is talking on the other thread. My comments are pretty simple by comparison.

          I would say that you will be best off accepting the separation of content rule. Its not just php but also for html and css, presentation and logic, and even js.

          One person made an argument that the *system* should allow putting everything together, and that's fine, it doesn't mean any developer really will or should do that. Not if you want to get real code working well ;-)

          The basic argument was that you can't debug your code well like this, but its also that you can't change anything easily, either. Changing any part of the code could affect other parts and could break stuff easily.

          Php of the front end is a huge security risk, but its also better to separate css clearly from html, that way changes can be done easily, in one place. For js, there is no sense in loading a lot more than needed.

          Its the same logic with snippets and chunks, they are letting you build a collection of tools that can go where you like, across templates, across domains, multiple times on every page, or whatever. Call them wherever you like, and adjust them in just one place. laugh
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Indeed. Learn the principles of DRY, separation of content, presentation and function, and the micro-services architecture principles since that's the future of MODX.
              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