• <h1>[*longtitle*]</h1> and PHP if#

  • Morten Reply #1, 3 years ago

    Reply
    Hi
    If I write <h1>[*longtitle*]</h1> in my template and don´t use the longtitle my site will print an empty <h1></h1>
    Can I write something like : php if longtitle ........

    I don´t know anything about PHP!

    Hope you understand my English!


  • mrhaw Reply #2, 3 years ago

    Reply
    This can be done with PHx - http://modxcms.com/PHx-1006.html
    [*longtitle:isnot=``:then=`<h1>[*longtitle*]</h1>`:else=``*]


  • dev_cw Reply #3, 3 years ago

    Reply
    This may work for you as well without using PHx (although PHx is great if you do not know PHP)
    <?php
    /* [!myField? $field=`[*pagetitle*]`] */
    
    $field = isset($field) ? "<h1>".$field."</h1>" : "";
    return $field;
    
    ?>


    There are more sophisticated methods but this should work and is easy to understand since it does not use the modx api. Simply use the snippet [!myField? $field=`[*pagetitle*]`] and change the field placeholder to be whatever you want. You can edit the output but changing "<h1>".$field."</h1>".

    Note: We are using the Ternary Operator to do the logic.


  • BobRay Reply #4, 3 years ago

    Reply
    Quote from: dev_cw at Feb 02, 2009, 08:40 PM
    This may work for you as well without using PHx (although PHx is great if you do not know PHP)

    Here's a slightly simpler (but less flexible) version:

    Put this in your template where the longtitle would go:

    [!LongTitle!] 


    Then create a snippet called LongTitle and paste in this code:

    <?php
    $field = $modx->documentObject['longtitle']
    return empty($field)? "" : '<h1>'.$field.'</h1>'; 
    ?>


    The last line is equivalent to this:

    if (empty($field)) {
       return "";
    } else {
       return '<h1>'.$field.'</h1>';
    }

    And here's another way using a placeholder (just to show how flexible MODx is):
    In your template:

    [!LongTitle!]
    [+my_longtitle+]


    In the snippet:

    <?php
    $field = $modx->documentObject['longtitle']
    $modx->setPlaceholder('my_longtitle', empty($field)? "" : '<h1>'.$field.'</h1>'); 
    ?>



    I hope this doesn't confuse you too much. Feel free to ask questions if you run into trouble.





  • Morten Reply #5, 3 years ago

    Reply
    wauw .... almost too much information for me!
    Tkanks

    "This can be done with PHx" ....... great i works for me


  • dev_cw Reply #6, 3 years ago

    Reply
    Quote from: Morten at Feb 03, 2009, 08:01 AM
    wauw .... almost too much information for me!
    Be carefull for what you ask around here, the community takes answering questions seriously


  • rthrash Reply #7, 3 years ago

    Reply
    Also be careful about making suggestions, unless you're ready to step up an volunteer.