We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30476
    • 16 Posts
    I am playing around with modx for a few weeks, but I just can’t figure out is there is a template system in Modx?
    I am not talking about replacement of variables (str_replace by modx->setplaceholder) with I know works fine.
    But at least can I defined a block and loop in it? (I want to avoid diging in the php code to change the layout) Right now the only solution I have found is to write a snippet (with php+html) for each select box for example. This is not good.
    Does a nice solution exist?
      • 32241
      • 1,495 Posts
      It’s all depends on how you want to achieve it.

      For example, if you use a snippet, than your template will be depended on the way the snippet author do.

      As far as I understand that you already know the basic of MODx, and you’ve done a template for MODx. There is no built in solution so far for this.

      Do you have any example of what you want to achieve? Like newslisting snippet or some of my snippets, we do use chunk to be the template, instead of mixing up the html output with the php. So it’s the same way as looping a template, isn’t it? As for now, I don’t see the need to use looping template, if we can use chunk and loop through it as it’s a repeater template. So in the end, it’s all depends on the snippet author. If you have a system that needs looping, I believe it’s something that hasn’t been implemented in MODx, and I believe you know how to use php to build it. If that’s the case, than you can use chunk as the repeater template, believe me, you can’t go wrong with this.

      Sincerely,
        Wendy Novianto
        [font=Verdana]PT DJAMOER Technology Media
        [font=Verdana]Xituz Media
        • 30476
        • 16 Posts
        Thank you for the feedback.
        That’s right, I did look on the Newlisting snippet, which make a nice use of a chunk as a template. I thought it was the solution, but not really because let’s take the example of what I want to do :

        I need to generate 2 select list box with a search button
        So if I use the newlisting’s way, it would mean make a 7 chunks (and 2 or 3 snippets): 1 that bundle the 2 selectbox + the search button and for each selectbox 1 for "<select>" then one for the actual loop "<option>" then one for the "</select>".

        So you agree that this the only way around in Modx? This is really not easy way to manage things for a designor without the help of a coder, and we have no chance to edit it in dreamweaver. And the structure might remain a mystery a first glance.

        You might think I should code everything in the snippet : but my other need is that the designer might (and will) want to replace the selectbox system by direct linking for example, so I would need to code again and again...

        Did I make things clearer to understand now? Any idea?
          • 32241
          • 1,495 Posts
          Hemm, from what I can see, if you need to have 2 select box or 2 list of links or 2 tables with a list of links or etc...

          Thank you only need 3 chunks with 1 snippet.

          You will one chunk to be the main template, for example
          Here is the title...
          <select>
          {+list1+}
          </select>
          <select>
          {+list2+}
          </select>
          Here is the footer...
          


          Two chunks for each list, for example
          <option value="{+value+}">{+name+}</option>
          


          Then you need one snippet to do the logic of fetching the data and generating the data into whatever that you want. So the logic will be to generate data for listing one, call out the chunk for the first list template loop, and replace the {+value+} and {+name+} with the data that you generate in the code, and keep the output in one string variable, and appended each output from the loop to be store inside the var. Do the same thing with the second listing with a different var. Now you have 2 string variables with the generated data combined with the template loop. All you need to do now is to use the main template, and substitute {+list1+} and {+list2+} with this 2 variables. Than of you go. You can have whatever output that you want, without limited to select box.

          Here is the API that might be usefull for you. What it does is actually fetch the chunk, and replace the special tag, such as {+array_name+} with the value on the asociative array included inside the parameter.
          function parseChunk($chunkName, $chunkArr, $prefix="{", $suffix="}") {
          	if(!is_array($chunkArr)) {
          		return false;
          	}
          	$chunk = $this->getChunk($chunkName);
          	foreach($chunkArr as $key => $value) {
          		$chunk = str_replace($prefix.$key.$suffix, $value, $chunk);
          	}
          	return $chunk;
          }
          


          So what do you think? Your designer doesn’t even have to understand about template looping syntax. All that they need to know that the list1 and list2 template will be repeated and being put on the main template. Which the benefit is, you have the tag in plain simple bracket, that doesn’t require your designer to learn about the syntax logic for templting.

          Hope it helps.
            Wendy Novianto
            [font=Verdana]PT DJAMOER Technology Media
            [font=Verdana]Xituz Media
            • 30476
            • 16 Posts
            I feel like ParseChunk is not in the documentation... Too bad.
            Anyhow nice little script, it sounds like a drupal like coding style.
              • 32241
              • 1,495 Posts
              It’s in the API, but it hasn’t been documentated yet. If you do have the will in the first place, there is a tutorial to make a template with this API. It’s all up to you, we don’t ask you to like our way to do things, because not all software fulfil all the needs, but we try our best to meet most of the majority out there. So if you find something that can be improved, please do post your concern, and we will try as best as we could to help you out with whatever best solution as possible wink

              Sincerely,
                Wendy Novianto
                [font=Verdana]PT DJAMOER Technology Media
                [font=Verdana]Xituz Media
              • Don’t know about Toti, but I’m happy. That’s exactly how I need to tackle the templating for the Polls snippets I did.

                It’s great having little pointers like this when you’re still learning the system. Cheers Djamoer grin
                  Garry Nutting
                  Senior Developer
                  MODX, LLC

                  Email: [email protected]
                  Twitter: @garryn
                  Web: modx.com
                  • 30476
                  • 16 Posts
                  Ok I finaly got your solution working, here is what I did : Chunk {{searchbox}}
                  <form action="[+action+]" method="get">
                  <select name="y">
                  [+list1+]
                  </select>
                  <select name="t">
                  [+list2+]
                  </select>
                  <input type="submit" value="Ok">

                  And that’s great it can be edited in wysiwig mode (well it could since fck rewrites everything badly)
                  Chunk {{listeyear}} and {{listetitle}} like you said

                  <option value="{+value+}">{+name+}</option>

                  Snippet [[search]]
                  (... my code to get values from database)
                  reset($ayear);
                  $listyear='';
                  while (list($value, $key) = each($ayear))
                  	{
                  	$array['value']=$value;
                  	$array['name']=$key;
                  	$listyear .= $modx->parseChunk('listeyear',$array, '{+', '+}');
                  	}
                  
                  reset($atitle);
                  $listtitle='';
                  while (list($value, $key) = each($atitle))
                  	{
                  	$array['value']=$value;
                  	$array['name']=$key;
                  	$listtitle .= $modx->parseChunk('listetitle',$array, '{+', '+}');
                  	}
                  $tpl = $modx->getChunk('searchbox');
                  $modx->setPlaceHolder('list1',$listyear);
                  $modx->setPlaceHolder('list2',$listtitle);
                  $modx->setPlaceHolder('action',$listyear);
                  return $tpl;


                  This way, you are right only 3 chunks needed and 1 snippet, that’s nice!
                  - But, the designer need to write [!search!] in the template, with means it doesn’t know which template is controling appearence, in fact here the chunk {{searchbox}}) and the same applies to {{listeyear}} and {{listetitle}}. Not a big deal in this simple example that’s right, but I would rather have him link directly to the chunk. This might be possible but then I would need 2 snippets, and still have the 2 other template/chunk not linked directly. Did I got that right?

                  - I also need to have the value initialized... So here it is easy to modify the list chunks to

                  <option value="{+value+}" {+selected+}>{+name+}</option>

                  And modify the snippet accordingly. But, then, what about my designer wants to make a plain list of links where the selected link will not be an actual link, just the plain text? My snippet could return selected true or false, but how could I interpret it in the chunk? In the select box it would be (if selected=true echo selected) or in the plain list (if selected=true echo ’’ else echo [+action+]?y={+value}). (not the actual code, just the idea!)
                  Do you know about a solution? Or do we fall back in hard coding things in the snippet?
                    • 32241
                    • 1,495 Posts
                    There will be a lot of solution to this. It’s a matter about how flexible you want your system to look like.
                    Lets assume, when I’m creating a snippet, I always have a couple question that I need to ask to myself, before designing the system, which is:
                    1. What kind of output that I want the system to looks like?
                    2. How many different possibility that I want the output looks like?
                    3. What is the technical background of the end user that are going to use this?
                    4. How big is the chances for this non-technical end user to be able to redesign the whole appearance into a different look other than the one listed on the second question?

                    If you answer those question above, you will come up with a good set of mind in determining the flexibility of the templating system. Most likely the answer to number 3 will be a designer who knows about designing in html, right? If they know about html, then here comes the thing, how good are they at having a lot of different capability with the output. I will surely stated that only 75-90% of the user will be able to come up with a different looks than the one that you already tackle in question number 2.

                    If that’s the case, I would worry less about the implementation for question number 4, and I will just come up with the right and better solution for question number 2, while the rest will be left to the end user itself. If they know a lot about html coding and etc, than they will most likely figure out a way out using whatever currently provided by you.

                    That’s the reason why you need a good working system already in your mind, even before you start coding, so you can tackle all the problems before hand. My word of wisdom will be, you can have a system with 100% flexibility, no matter what there will always be something limited, and when it comes to that, your code need to dictate them on how they should find a workaround with their knowledge. Especailly in design, you’ll find a lot of other way to achieve the same look and feel, without having to sacrifice the branding or the coding of the program. A good example will be, you can see all other well-known portal or cms, can you see how creative the designer/template maker able to modify the system to looks so nice and neat with limited flexibility?

                    So anyway, the solution will be as simple as outputing just the url of the link. If it’s refered to itself, then output a javascript:;.

                    Hope my long explanation does give you an idea on how you’re going to design your system.

                    Sincerely,
                      Wendy Novianto
                      [font=Verdana]PT DJAMOER Technology Media
                      [font=Verdana]Xituz Media