We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22533
    • 41 Posts
    Hello everybody,

    It’s been ages since I had some free time, so now that I have, I decided to go deeper into MODx, which I hadn’t touch in months. smiley

    I’m trying to create a unique template, which could be either :
    - single column
    - 2 columns
    - 3 columns

    As I’d prefer not to have 3 different templates for this, I created 3 TVs :
    - "nbCols" : dropdown list to select the class of my main container
    - "Col2" : textarea for column 2 content
    - "Col3" : textarea for column 3 content

    What I’d like to achieve, in my template is this type of code :

    <?php
    $ColsType = $modx->getTemplateVar('nbCols', "", $modx->documentIdentifier);
    ?>
    
    <div class="[*nbCols*]">
    	<div class="col1">
    		[*content*]
    	</div>
    	<?php if ($ColsType['value']<>'onecol') {echo '<div class="col2">
    		[*Col2*]
    	</div>'}?>
    	<?php if ($ColsType['value']=='threecol') {echo '<div class="col3">
    		[*Col3*]
    	</div>'}?>
    </div>
    


    I precise that I don’t know PHP language, even if I can read it, so I’m not sure that the syntax is correct or relevant... (I’ve seen a simplier way to tell "if(test);true;false" but... tongue)

    The thing is, even the first code line (calling the API function) doesn’t work as it is displayed on my screen...

    So a first question would be : is it possible to call API functions in templates, or do I have to create a special snippet for it?

    Also, I’ve figured that there would be other ways to do that, like telling : If [*Col2*] is empty, don’t display it
    (but I’d still need my class TV for the CSS to work)

    But my second point will be : could I do that with PHx, and will it be better according to you?

    Something like that (I didn’t test) :
    [+phx:if=`[*nbCols*]`:is=`twocol`:then=`<div class="col2">[*Col2*]</div>`+]


    I’d really appreciate if somebody could advise me and correct the code I try to implement, I’ve spent too much time on that problem, and I’d like to move on to other issues... undecided

    Thank you in advance.

    Regards,

    Greg
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Why can’t you just have your TV, and in your multi-purpose template have
      <link href="assets/templates/[*whichtemplate*].css" ... />

      In other words, let the TV provide the actual css file name.

      I would be inclined to have the default be @INHERIT, then you’d only need to set it for parent/container documents.
        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
        • 22533
        • 41 Posts
        In fact, my CSS file is the same for all 3 possibilities, and I’d rather not duplicate it.

        I’ve just tested the PHx method, it works fine! smiley

        However, I’m struggling to use the API, none of my tests works! huh

        I’ve chosen the easyest example : http://wiki.modxcms.com/index.php/API:documentIdentifier

        Test : a template like this
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
        <head><title>test</title></head>
        <body>
        <?php
        echo "Now viewing :".$modx->documentIdentifier;
        ?>
        </body></html>


        I’ve opened several snippets written by other members of the community to check them out and take them as an example.
        How come my simple test doesn’t work ?
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          You cannot have raw php code like that. You have to use snippets and MODx tags. And if this is coming from an external file, it won’t have access to the $modx object.
            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
            • 22533
            • 41 Posts
            Quote from: sottwell at Dec 12, 2007, 04:20 AM

            You cannot have raw php code like that. You have to use snippets and MODx tags. And if this is coming from an external file, it won’t have access to the $modx object.
            OK, thank you scottwell for your answers.

            Snippet works indeed, I must have messed with the code when doing the test. (Test 2 from previous post edited)

            I think I’ll go on with PHx, as I would like to view the conditions / outputs directly in my template.

            E.g. : if TV is not empty, output within <li></li>