We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32241
    • 1,495 Posts
    Current Version: Version 0.3

    Example: http://test.djamoer.net/assets/libs/chunktpl_v0.3/example/

    Hi guys, still remember the library proposal that I did a couple weeks ago?

    Here is my first official release of chunkTpl library, allowing snippets/plugins to mess around with using chunk as their template, and using a few common syntax on MODx with features support listed below:
    1. Having multiple part of template collection on one chunk, with their own template variables to be parsed on that specific part.
    2. Substituting placeholder with variable value.
    3. Substituting placeholder with processed variable on certain behaviour (achieved using extended function capability).
    4. Allowed multiple parameters to be passed on to the extended function.
    5. Helper function to generate Repeater (ASP .Net) like template with MODx.
    6. Helper function to create a conditional template with MODx.
    7. Allow several default themes being shipped with snippets/plugins with the added API on version 0.2

    Library Information
    /*
     *  Written by: Wendy Novianto (DJAMOER Solution and Design)
     *  Contact: [email protected]
     *  Created: 2/15/2006
     *  Modified: 2/15/2006
     *  For: MODx cms (modxcms.com)
     *  Name: [Library] Chunk Template (chunkTpl)
     *  Version: Version 0.3
     *  Description: Library class to display a chunk of text or MODx chunk as a template
     *               with some other features on it.
     *  Releases:
     *  - Version 0.1 - Release the first prototype of the chunkTpl library
     *  - Version 0.2 - Added themes API for easily having a theme system for snippets/plugins
     *  - Version 0.3 - Change the template syntax to use semicolon and allow multiple parameters being passed
     */
    


    The solution might not be sophisticated enough, but it’s really simple, which allowing a more extendable class for snippets coder.

    It’s adviseable to put the library under /assets/libs/chunkTpl_v0.1/
    The reason to that, if some snippets already use version 0.1, and there is a major overhaul in the syntax on version 0.2, then a users able to install this two different library classes on one MODx installation.

    Any question, suggestion or comments are welcome. I would love to discuss this further, as this is only the beginning of what’s coming next from Raymond, with his idea of GreenCore.

    Sincerely,

    ATTENTION: I just changed the zip file for version 0.1 (I did a few modification for the template system on Repeater and Conditional)
      Wendy Novianto
      [font=Verdana]PT DJAMOER Technology Media
      [font=Verdana]Xituz Media
      • 25663 MODX Staff
      • 12,272 Posts
      Wendy, can you run through a quick real-world example of this? (sounds incredibly interesting)

      Thanks. smiley
        Ryan Thrash, MODX Co-Founder
        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
        • 10487 MODX Staff
        • 1,535 Posts
        Cool stuff, Wendy! grin That’s going to be an extremely useful templating tool.

        Thanks, Garry
          Garry Nutting
          Senior Developer
          MODX, LLC

          Email: [email protected]
          Twitter: @garryn
          Web: modx.com
          • 32241
          • 1,495 Posts
          Sure thing Ryan,

          Check out this example page included in the library http://test.djamoer.net/assets/libs/chunkTpl_v0.1/example.php
          Here is the template being used http://test.djamoer.net/assets/libs/chunkTpl_v0.1/example.tpl

          Here is the PHP code on the snippet calling or on the php file (example.php). Notice that I’m using plain text as the template input instead of chunk, but you can use chunk by disabling plain text input.
          <?php
          // Include library file
          include_once('chunktpl.class.inc.php');
          
          // Read teplate file into a plain text
          $fp = @fopen('example.tpl', "r");
          if($fp) $template = @fread($fp, filesize('example.tpl'));
          else die("Can't load example template!");
          
          // Extend current chunkTpl class
          // -- Extend the class with all the necessary processing function to use in formating the output
          // -- Make sure the processing function always have 2 parameters with it, $value and $param
          class chunkTplExtended extends chunkTpl {
          	function formatDate($value, $param) {
          		$output = substr($value, 0, 4).'-'.substr($value, 4, 2).'-'.substr($value, 6, 2);
          		if($param != NULL) $output .= ' ('.$param.')';
          		return $output;
          	}
          }
          
          // Initialize extended class & parse plain text template (2nd parameter set to true)
          $tplObject = new chunkTplExtended($template, true);
          
          //
          // Set template variables and parse template
          //
          $output = '';
          
          // Parse header template
          $tplObject->setTplVar('Header', 'PageTitle', '[Library] Chunk Template (chunkTpl)');
          $tplObject->setTplVar('Header', 'PageDate', '20060214');
          $output .= $tplObject->parseTpl('Header');
          
          // Parse body template with conditional statement from system logic
          $conditionData['Condition'] = 'chunkTpl';
          if(isset($_GET['c']) && strtolower($_GET['c']) == 'false') $condition = false;
          else $condition = true;
          $output .= $tplObject->outputCondition('Condition', $condition, $conditionData, $conditionData);
          
          // Parse body template with loop from arrayData
          for($i = 0; $i < 10; $i++) {
          	$arrayData[$i]['PageData'] = "Item Listing ".($i+1);
          }
          $output .= $tplObject->outputRepeater('Loop', $arrayData);
          
          // Parse footer template
          $tplObject->setTplVar('Footer', 'Copyright', 'Wendy Novianto (<a href="http://www.djamoer.net/">DJAMOER Solution and Design</a>)');
          $output .= $tplObject->parseTpl('Footer');
          
          // Output result
          //print_r($tplObject->chunkTplData);
          echo $output;
          ?>
          


          From the code above you can see that the basic templating system is actually done, all you need is to extend the class to fit most of the snippets look and feel. For example, I already provide a helper function for repeater and conditional output to help generate an output of table or conditional tag much easier, but you can extend it further, for example to generate certain html tag on the template, such as DataGrid in ASP .Net (hope you guys don’t mind if I use .Net a lot as an example in this lib).

          You can also extend the processing function to be use on the template variable that are called using {+Var_Name:function_var:parameter+}
          Then you need to extend the class and add a method inside the extended class called like this
          <?php function_var($value, $param) { return $value.$param; } ?>

          With this, you can provide an extended functionality with the outputed values to the template. A good example will be the newslisting, where we can have this functionality to format the createdon date before being displayed, maybe the tag will look like this {+createdon:formatDate:%d-%m-%Y+} or {+createdon:formatDate:%d/%m/%Y+}.

          That’s the whole things that this libs able to do. It’s a not a full-fledge templating system, but it provides enough capability for our current snippets to be able to provide more templating usability for the designer. Maybe by the time the next release of GreenCore with the new parser come out, this library will either being deprecated slowly or keep being enchanced to work together with the next released of the new parser.
            Wendy Novianto
            [font=Verdana]PT DJAMOER Technology Media
            [font=Verdana]Xituz Media
            • 6726
            • 7,075 Posts
            The concept is awesome, if I can grasp it, I’ll most definitely use it !

            Another great project, Wendy, how do you manage to be on so many fronts ?!?
              .: COO - Commerce Guys - Community Driven Innovation :.


              MODx est l&#39;outil id
              • 32241
              • 1,495 Posts
              Quote from: davidm at Feb 15, 2006, 11:48 AM

              The concept is awesome, if I can grasp it, I’ll most definitely use it !

              Another great project, Wendy, how do you manage to be on so many fronts ?!?

              Yeah David, the only thing that I really like the most is the concept of having one chunk to handle all the templating for one snippet. If you take a look on some of my beta snippets, most of them are using several chunks to allow templating the snippet, with this approach, basically we only need one chunk for one theme, that’s all.

              On my next snippet release, I will allow a themes subdir to have the snippet shipped with different themes included in it. So user can pick and choose which one that they want to use. I’ll add this on version 0.1.1 of this library.

              Another reason is that, I want all snippets to have some kind of standard templating system, so that the end user will only need to learn one template system and have it being appliad to all the snippets that we have. Hopefully with this library, we can have most snippets maker to start recoding their snippets and use this library, and I will try to maintain the library as small as possible, so that it won’t bog down the system, as well as not conflicting with the future possibility of MODx new parser wink If the snippets maker/coder wanting to have more extensive use of the library, they can extend it further, and if I think it’s a necessary addition, I will add it to the future release of this library.

              Some of the things that can be added will be the methods begin with ’output’ and ’format’. This is not part of the core lib, but it will help coder to easilty use the provided libs to generate a better output with less-coding and less-logic.

              Any thought of adding more usability of methods begining with ’output’ will be awesome. So far I only have Repeater and Conditional helper function. I was thinking to add DataGrid or some other stuff, if you guys have any thought or idea, please do post in here and explain the logic behind the methods that you want to add, and I will code it for you guys.

              Sincerely,
                Wendy Novianto
                [font=Verdana]PT DJAMOER Technology Media
                [font=Verdana]Xituz Media
                • 32241
                • 1,495 Posts
                I just added theme API, to allow several default theme for 1 snippet to be shipped together with the snippet package, so it will help the end user to use the pick and choose the default theme that they want.

                It’s optional, but I would like to encourage all the fellow snippet/plugins coders to use this library, so we can start standardizing the snippet being produced, to allow easy to use/implement for the end user. If there is anything that I can do in this library to make your life easier as a snippet coder, please do post, and I will try to provide an easy to use solution for you guys.

                Sincerely,
                  Wendy Novianto
                  [font=Verdana]PT DJAMOER Technology Media
                  [font=Verdana]Xituz Media
                  • 6726
                  • 7,075 Posts
                  Waow, thanks for the explanation, it’s very exciting indeed : great project !
                    .: COO - Commerce Guys - Community Driven Innovation :.


                    MODx est l&#39;outil id
                    • 32241
                    • 1,495 Posts
                    Another simple update.
                    This will be the final update for the library. I will start using this library on some of my snippets.
                    So anybody on the same direction with me, please do report, if you guys encountered any difficulty or bugs down the road.

                    Sincerely,
                      Wendy Novianto
                      [font=Verdana]PT DJAMOER Technology Media
                      [font=Verdana]Xituz Media
                      • 18219
                      • 826 Posts
                      Great project Wendy.

                      I wish to use this library in the snippet MaxiGallery to return it more custom.
                      But a point remains dark for me. How to generate loops?
                      In the example given, the template takes into account 2 Themes. What does it occur with 10, 20 or 30 Themes?

                      In the case of the snippet MaxiGallery, we can have more than 30 pictures display on several pages in a table of several lines or quite simply 4 pictures on one line.

                      How to generate loops WHILE, FOR or FOREACH?

                      Thanks
                        Marc
                        I&#39;m French... Sorry for my bad English, I use &#39; Google Translator&#39; or other... but that remains that tools wink