We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11954
    • 58 Posts
    Hi,

    I need to by default put the title of a document into a template variable and replace the spaces with an underscore "_". I am sure this is very easy to do but I’m not quite sure where to start, please help.

    Tore
      • 7231
      • 4,205 Posts
      Use a plugin to copy the data to the TV when saving the document. Take a look at this plugin for an example of how to approach this:
      http://modxcms.com/forums/index.php/topic,36496.0.html
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don't know what it is.
        Do you, Mr. Jones? - [bob dylan]
        • 15469
        • 64 Posts
        Just have a look at some string-functions of PHP.
        http://us2.php.net/manual/en/function.str-replace.php

        You could set a default value to every template variable using the @-bindings.
        http://svn.modxcms.com/docs/display/MODx096/What+are+(at)+Bindings

        Not tested, but might work this way
        @EVAL return str_replace(" ", "_", ’[*pagetitle*]’);
          $cd /pub
          $more beer
          • 11954
          • 58 Posts
          Thanks for the help on this. The string substitution should work and is kind of what I was thinking of but for some reason it ignores the substitution and just returns the pagetitle huh

          As an example I have the page title set to VC and have put in the following to test it:

          @EVAL return strtr(’[*pagetitle*]’, "V", "O");

          but the TV just returns VC, i.e it doesn’t substitute the V with the O. huh

          BTW I did also try @EVAL return str_replace(" ", "_", ’[*pagetitle*]’); but that didn’t work either. :’(
            • 15469
            • 64 Posts
            Approved... the str_replace returns the string without replacing the occurences... strange.

            What you might do now...

            Create a snippet that takes a string as an argument, modifies it and returns the modified value.

            Call that snippet via the @EVAL-binding and let it return the output of that snippet.

            Your TV might take this as a default value:
            @EVAL return $modx->runSnippet(’test_str’, array(’string’=> ’[*pagetitle*]’));

            Your snippet should look like this for example:

            <?php
            $string = isset($string) ? $string : "";
            return str_replace(" ", "_", $string);
            ?>
            


            This should work, but doesn´t. The passed string might be no "real" string consisting of single characters...
            Is it URL-encoded, HTML or somewhat?
              $cd /pub
              $more beer
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              You can’t use MODx tags in PHP code. If you have PHP code in a snippet or a TV with @EVAL you have to use one of the $modx API functions or arrays. For example:
              @EVAL return str_replace(" ", "_", $modx->documentObject['pagetitle']); 

                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
                • 15469
                • 64 Posts
                Thank you Susan. Again.
                Take the variable from the documentObject and you’re fine.

                PHP took that MODx-Tag as text, but PHP couldn´t get it as I expected it to do. Strange behaviour.
                  $cd /pub
                  $more beer
                  • 11954
                  • 58 Posts
                  Quote from: sottwell at Jul 06, 2009, 07:07 AM

                  You can’t use MODx tags in PHP code. If you have PHP code in a snippet or a TV with @EVAL you have to use one of the $modx API functions or arrays. For example:
                  @EVAL return str_replace(" ", "_", $modx->documentObject['pagetitle']); 



                  Thanks a million Susan, I was kind of gradually getting there myself but it was a bloody frustrating journey so you just saved a me a lot of headaches smiley

                  Also big thanks to Katzenfutter and dev_cw, this forum rocks grin