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

    is it possible to change page title in a snippet huh
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Permanently, or just temporarily?

      Permanently, you’d need to do an update of the site_content table. Temporarily you can access the $modx->documentObject array.
        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
        • 36702
        • 76 Posts

        My template includes:
        <title>[*pagetitle*] | [(site_name)]</title>

        I have a product details page that shows details of a specific product. The page title is "Product details", but I want to change the page title (html title tag) to the name of the specific product.

        This doesn’t seem to do anything:
        $modx->documentObject[’pagetitle’] = $productName;

        And neither does this:
        $o = &$modx->documentOutput; // get a reference of the output
        $o = str_replace("<title>[*pagetitle*]","<title>$productName",$o);
        (I guess the above would only work in a plugin - OnWebPagePrerender?)

        The only way I can think of doing what I want is to change the template to
        <title>[[myTitle]] | [(site_name)]</title>

        Where myTitle snippet would return the document pagetitle most of the time, but for the product details page, would look up the product name. The main snippet in the page also looks up the product name - is there any way to avoid this duplication, or am I just being precious?

          • 36702
          • 76 Posts
          The product info is stored in a separate database, not in the MODx database.
            • 7231
            • 4,205 Posts
            This doesn’t seem to do anything:
            $modx->documentObject[’pagetitle’] = $productName;

            And neither does this:
            $o = &$modx->documentOutput; // get a reference of the output
            $o = str_replace("<title>[*pagetitle*]","<title>$productName",$o);
            (I guess the above would only work in a plugin - OnWebPagePrerender?)
            If it is a different database the api does not have access to it. You need to access this in a custom snippet that queries the second database.
              [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&#39;t know what it is.
              Do you, Mr. Jones? - [bob dylan]
              • 36702
              • 76 Posts
              Thanks I did realise that - I can get the name into $productName, but I just wanted to avoid getting the name from the db once for the html title tag, and again in the body of the page (ie querying the database twice for the same page). It’s not a biggie - I think I’ll do it that way. Thanks for the replies.
                • 36702
                • 76 Posts
                setPlaceholder sounds like exactly what I need! smiley Thanks, I’ll try it out (probably tomorrow)
                  • 36702
                  • 76 Posts
                  I couldn’t wait till tomorrow - changed the document’s title to [+productTitle+] and added $modx->setPlaceholder(’productTitle’, $productTitle); to the main snippet on the page - works beautifully! Thanks for all your help.