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

    I'm currently working on building a site that houses several pages, each of which is rather long and contains multiple, vertically-stacked subsections. Each subsection has varying content and layout - for example, one subsection might house an image with description and some copy, the next might contain a form and a sidebar, the next might contain copy only, etc. These subsections never get linked to, and don't exist as 'pages' on their own; they're just varying pieces of content within a larger page.

    What I'm looking for is a method that allows me to create individual resources for each subsection and then render the subsection inline on the page using the resource's template itself. I have achieved something similar in the past by using getResources and the value of a template variable within each resource to conditionally determine which tpl chunk to use, but this method is a little cumbersome as it uses one template (and set of template variables) for all resources, and necessitates editing chunks to alter the formatting of each resource type, which is much less convenient than simply specifying different templates for each resource and having those determine the formatting of the output.

    Is there some way to force getResources to display content using the resource template itself rather than rendering using a tpl chunk? If not, is there another snippet that allows this, or some simple PHP-based method of achieving the same?
      • 40706
      • 128 Posts
      Iv´e had an Website where we used an Plugin to render the document on save with the template into the Introtext variable. This one was hidden for the user via the Form Customization.

      The introtext itself could then easily used with getResouces to list a lot of them. Maybe this in another approach to the solution.


      and here the Plugin

      OnDocFormSave
      if($resource->get('template') != '13')
         return true;
      
      
      $template = $resource->getOne('Template');
      $content = $template->getContent();
      
      $templateVars =& $resource->getMany('TemplateVars');
      foreach ($templateVars as $tvId => $templateVar) {
      	$content = str_replace("[[*{$templateVar->get('name')}]]", $templateVar->renderOutput($resource->get('id')), $content);
      }
      $resource->xpdo->getParser();
      $resource->xpdo->parser->processElementTags('', $content, false, false, '[[', ']]', array(), 5);
      
      
      $resource->set('introtext',  $content );
      $resource->save();
        • 36521
        • 9 Posts
        Interesting approach. It seems a little circuitous, but definitely a clever way to solve the problem. I'll give that a try, thank you.
          • 36521
          • 9 Posts
          Although, thinking about it, won't this necessitate re-saving each resource after every change to the template?
            • 40706
            • 128 Posts
            Yes, it would... for us its not important because the markup is very small and won`t change at all. But If its known that it will change, you could use an similar code as an snippet with getResouces in the row Chunk to render the resource, and return it.

            We also did an very small page with the same behavior, subpages which are rendered to an large one.
            This is http://www.verpoorten-mall.de/maedelsparty/ Here we used the Chunk variant
              • 40706
              • 128 Posts
              Yes, it would... for us its not important because the markup is very small and won`t change at all. But If its known that it will change, you could use an similar code as an snippet with getResouces in the row Chunk to render the resource, and return it.

              We also did an very small page with the same behavior, subpages which are rendered to an large one.
              This is http://www.verpoorten-mall.de/maedelsparty/ Here we used the Chunk variant
                • 33976 ☆ A M B ☆
                • 571 Posts
                Hi guys,

                i guess renderResources is doing extractly what you are after smiley
                  • 36521
                  • 9 Posts
                  Ah, perfect, thank you! I knew there had to be something out there to meet such a basic need.