We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6705
    • 79 Posts
    I’m writing a page whose main ’content’ will consist only of a snippet which extracts data from a database (the data values returned vary according to the value of a GET parameter passed to the page) and outputs the data. So far, so good, but in particular I need to use one of the variables extracted from the database as the ’page title’, to appear in the "<title>..</title>" tag.

    Can anybody offer any advice on how I can do this?

    My snippet will extract all of the data that I need from the database, and assign the data to variables for output as it assembles the main part of the page. Is it possible to somehow have one of my variables ’overwrite’ the [*pagetitle*] ’resource field’ in the page template, or use a ’placeholder’ in the template instead and assign the variable to that ’placeholder’ somehow, even though the "<title>" part of the HTML occurs before my snippet call in my template?

    (Using MODx Evolution)
      Please don't PM me unless it's absolutely essential: if a technical question is worth asking, it's worth asking in public, so that others can share their experience, and so that all can learn from the answers.
      • 4172
      • 5,888 Posts
      you can try to use
      $modx-setPlaceholder('mypagetitle',$mypagetitle);
      in your snippet and place this placeholder with
      [+mypagetitle+] 
      on place of
       [+pagetitle+]


      if this will not work in evo, and you don’t want to get your data-object twice, you can get your data-object in an extra snippet in the head and store your data-object for later use or store them also in placeholders to put them out where you need them.

      I think its also possible to use snippet-tags in the pagetitle-input-field if this helps
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 36404
        • 307 Posts
        Hi,

        actually, as you snippet is going to be in the content part of the page, the simplest way i can think of making what your looking for is to write a specific snippet that outputs your title part and call it in the title tag
        you could even write it to output your database specific title if there is or MODx native longtitle (e.g) if not

        have swing
          réfléchir avant d'agir
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Indeed, your <title> ... </title> can be a TV, a snippet, or whatever you want. If your page’s content is totally built from a database query, you might even want to use a "blank" template and generate the page’s template as part of processing the query results. That way you could use the single snippet with minimal database queries to generate your page’s metadata as well as the content.

          Another way to accomplish this would be to have a template for that page, and no content. Put the snippet at the beginning of the template, and use placeholders.
            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
            • 6705
            • 79 Posts
            Quote from: Bruno17 at Apr 18, 2011, 10:58 AM

            you can try to use
            $modx->setPlaceholder('mypagetitle',$mypagetitle);
            in your snippet and place this placeholder with
            [+mypagetitle+] 
            on place of
             [+pagetitle+]


            Many thanks, that’s exactly what I was after! It has worked perfectly. I wasn’t sure whether you could use a placeholder that you weren’t actually defining until within a snippet call later in the page, but it looks as though MODx must parse through the page more than once in order to Do What I Mean..? smiley



            if this will not work in evo, and you don’t want to get your data-object twice, you can get your data-object in an extra snippet in the head and store your data-object for later use or store them also in placeholders to put them out where you need them.

            Ah, I think I’m starting to get a twinkling of enlightenment.. Up until now, I’ve been writing snippets essentially as ’thinking teletypes’: just appending all output to a string variable and then printing the eventual result (to create the main ’content’ part of a page), varying according to the data processing within the snippet; but I’m starting to get the idea that if I were to make more sensible use of placeholders this might help me write more flexible code.. (Right now my data output is rather intertwined with the layout/presentation code, which I gather is somewhat non-recommended.)

            What do you mean by "stor[ing] the data-object for later use", by the way? Using placeholders I think I’m now starting to comprehend, but I’m afraid I’ve not yet really delved into those aspects of how MODx works technically.



            I think its also possible to use snippet-tags in the pagetitle-input-field if this helps

            I didn’t realise that, that’s very useful to know. However, in this case, I was hoping to avoid having to have more than one snippet call as this would mean having to query the database more than once for my data, which seemed unnecessary (and possibly also would introduce a risk of inconsistency if the data were changed in the short interval between the two snippets being called).
              Please don't PM me unless it's absolutely essential: if a technical question is worth asking, it's worth asking in public, so that others can share their experience, and so that all can learn from the answers.
              • 6705
              • 79 Posts
              Quote from: sottwell at Apr 18, 2011, 04:55 PM

              Indeed, your <title> ... </title> can be a TV, a snippet, or whatever you want. If your page’s content is totally built from a database query, you might even want to use a "blank" template and generate the page’s template as part of processing the query results. That way you could use the single snippet with minimal database queries to generate your page’s metadata as well as the content.

              Thanks, the blank template idea is an interesting one, but I think I’m too far along with this particular project to change things. I’m trying to port an existing website into MODx and the original site uses a lot of slightly-different templates for various parts of the site, so I’ve ended up with a rather complicated(!) system of templates involving different ’templates’ made up of mix-and-match ’chunks’ (the chunks used in a given template varying depending on which chunks are needed for a given page type). The idea behind using chunks was to try to minimise the rewriting needed if/when we change the site design.

              Your suggestion has given me the idea that if instead I was creating the appropriate templates on-the-fly via a snippet, it might have been easier to manage..!



              Another way to accomplish this would be to have a template for that page, and no content. Put the snippet at the beginning of the template, and use placeholders.

              A-ha! Yes, that sounds like it could be quite a neat and tidy way to do it. Thanks for that idea! One to bear in mind for the next project, perhaps..
                Please don't PM me unless it's absolutely essential: if a technical question is worth asking, it's worth asking in public, so that others can share their experience, and so that all can learn from the answers.