We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26104
    • 36 Posts
    As you all may have noticed, designing snippets is fairly easy. But if you want to keep it clean and simple, it can be handy sometimes to use some kind of templates. In the past I used .tpl files based on a custom simple template parser. But this week I changed my parser to work with ModX chunks grin And off course i’m going to share it with you all! It is based on a singleton based class. This is not really necessary in ModX, but it also won’t affect the performance or anything smiley

    How it works:

    • Upload the class into a directory on your server
    • Make a chunk and place your markers in it: [+markername+]
    • Include the class in your snippet
     
    include("pathtoclass/template.inc.php");
    

    • Start the class
    $tpl = &template::getInstance();
    

    • And start assigning
    $html = ''; foreach($result as $var => $status) { $tpl->assign("var", $var); $tpl->assign("status", $status ? "free" : "occupied"); // Weergeven: $html .= $tpl->parse('nameofchunk'); } $tpl->assign("rows", $html); echo $tpl->parse('nameofchunk2'); [/code
      ][list]
    • Parsing the templates
    As you can see in the last piece of code, parsing a template is done by calling $tpl->parse(’nameofchunk’);. All assigned variables will be replaced and dropped!! So all assigned vars will be gone!
    [/list]

    The class is not using blocks or something, it’s very simple and ligtweight. It is placed here "as is" and i’ll give some support through this forum.

    This explanation is very basic, but if this class is popular and people don’t understand this explanation I’ll make a new bigger one wink
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      How is this comparable to $modx->setPlaceholder? Or even using str_replace(’placeholderName’, ’placeholderValue’, $tpl) ?
        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
        • 26104
        • 36 Posts
        Well what i found in the original modx "way" is that everything just worked the other way around... So you first get a chunk, then assign... This wasn’t my way of working before so I made it more easy for myself and adjusted my old parser so i could re-use some of my old code more easy.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          From your explanation, I don’t see a difference... in the snippet set a placeholder
          $modx->setPlaceholder('name', $value);

          Then when you are ready to output the chunk, use
          $output = $modx->mergePlaceholderContent($tpl);

          The advantage to this is that you can set placeholders to use anywhere in the document, including as argument values for subsequent snippets, not just the chunk template, since it’s part of the MODx parsing process.

          You can use the $modx->setPlaceholder() function to temporarily store any value you will want to use later in the document, and use $modx->getPlaceholder(’name’) to get it back.

          I don’t mean to denigrate what you’ve done, I’m just trying to see what would be the benefit of using it instead of the native MODx functions.
            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