On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Hello, I'm fairly new to MODX system and I need a little help. We have amp pages which retreive the main content of the page in the following way:
    'content' => $resource->get('content')

    The problem is, this content is meant for full desktop pages and can't get validated, so I need custom AMP friendly content retreived instead of raw "content". I've created a TV named "amp-content" and I try to get it on AMP pages instead of content. Here is the current code in AMP Template:

    Template code:
    <h1>[[+amp_ptitle]]</h1>
        <amp-img src="[[+amp_image]]" alt="[[+amp_ptitle]]" width="[[+amp_image_width]]" height="[[+amp_image_height]]" layout="responsive"></amp-img>
        <article>[[+amp_content]]</article>


    Snippet:
    $modx->setPlaceholders(array(
            'ptitle' => $resource->get('pagetitle'),
            'pdescription' => $resource->get('description'),
            'content' => $resource->get('content'),
            'canonical' => $modx->makeUrl($resource->get('id'),'','','full'),
            'published' => $resource->get('publishedon'),
            'edited' => $resource->get('editedon'),
            'image' => $image_url,
            'image_width' => $width,
            'image_height' => $height
        ),'amp_');


    So I'm trying to do something with this line:
    'content' => $resource->get('content'),

    What I want to do is: if there is amp-content TV available - fetch it, if not - get normal content.
    Obviously, options like this don't work
    'content' => $resource->get('amp-content'),

    Also, I've tried
    'content' => $tvr->get('amp-content'),

    No luck.
    So, is it possible to do something here, I'm new so this might be a noob question, thank you very much

    This question has been answered by BobRay. See the first response.

    • How are you serving amp pages currently is it via the amp pages extra, switch template extra or another way? In the first two extras you're serving a different template anyway for amp pages so you could just replace the tvs with some amp specific created ones.
        ■ email: lk@alienbuild.uk | ■ website: https://alienbuild.uk

        The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
      • Quote from: lkfranklin at Apr 27, 2018, 06:43 AM
        How are you serving amp pages currently is it via the amp pages extra, switch template extra or another way? In the first two extras you're serving a different template anyway for amp pages so you could just replace the tvs with some amp specific created ones.
        Like this:

        The amp urls structure looks like this:
        site.com/amp.html&page=page-slug
        • Look for the SwitchTemplate extra. It could switch the template to amp on the fly.
          • Is there a way to do it without installing new things?
            We already have AMP working, that's not the issue. I just need to convert the resource content into amp friendly content, that's it
            • If you really want to work with a different amp-content template variable (because you have to handle two content fields). TV content could be retrieved this way.

              'content' => $resource->getTVValue('amp-content'),
              • Thank you, Jako, it works. Could you please advise how can i combine these two lines into "if {amp-content} is not available, then put {content}"?
                That's pretty much all I need. Thank you!

                $modx->setPlaceholders(array(
                        'ptitle' => $resource->get('pagetitle'),
                        'pdescription' => $resource->get('description'),
                
                
                        'content' => $resource->get('content'),
                        'content' => $resource->getTVValue('amp-content'),
                
                
                        'canonical' => $modx->makeUrl($resource->get('id'),'','','full'),
                        'published' => $resource->get('publishedon'),
                        'edited' => $resource->get('editedon'),
                        'image' => $image_url,
                        'image_width' => $width,
                        'image_height' => $height
                    ),'amp_');
                • discuss.answer
                  I think this would do it:

                  $tvValue = $resource->getTVValue('amp-content');
                  $docContent = empty($tvValue)? $resource->get('content') : $tvValue;
                  $modx->setPlaceholders(array(
                          'ptitle' => $resource->get('pagetitle'),
                          'pdescription' => $resource->get('description'),
                   
                          'content' => $docContent,
                  
                          'canonical' => $modx->makeUrl($resource->get('id'),'','','full'),
                          'published' => $resource->get('publishedon'),
                          'edited' => $resource->get('editedon'),
                          'image' => $image_url,
                          'image_width' => $width,
                          'image_height' => $height
                      ),'amp_');
                  


                  You could do it with a little less code like this if you change the ptitle and pdescription placeholders to pagetitle and description:

                  $fields = $resource->toArray();
                  $tvValue = $resource->getTVValue('amp-content');
                  $fields['content'] = !empty($tvValue)? $tvValue : $fields['content'];
                  $fields = $array_merge($fields, array(
                      'canonical' => $modx->makeUrl($resource->get('id'),'','','full'),
                      'image' => $image_url,
                      'image_width' => $width,
                      'image_height' => $height,
                  ));
                  $modx->setPlaceholders($fields, 'amp_');
                  



                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                  • Quote from: BobRay at Apr 28, 2018, 07:28 PM
                    I think this would do it:


                    Thank you, BobRay, it works!