Fork me on GitHub

function call inside conditional statement?  Bottom

  • I want to use the pnmodgetvar function within a template, but I need it in a conditional statement, sort of like this:

    Code

    <!--[if (pnmodgetvar module="Example" name="foobar") eq 1]-->
    show this line
    <!--[/if]-->


    will this work?

    if not, the Smarty docs say I can call PHP functions, like this:

    Code

    <!--[if count($var) gt 0]-->


    so can I call an API function instead of PHP?

    Code

    <!--[if pnModGetVar('Example', 'foobar') eq 1]-->


    what's the best way to skin this cat?
  • If this is in conjunction with a module, you could just send the variable from the function that calls the template.

    Otherwise, to include PHP functions in templates, use plugins, like how the Example Module's administration menu is handled.
  • I've got no idea if something like this would work:

    Code

    <!--[pnmodapifunc module="example" type="user" func="test" assign="var"]-->

    <!--[if $var eq 1]-->


    Otherwise, just use a plugin ;)

    --
    itbegins.co.uk - Zikula Consulting

    birtwistle.me.uk - Personal Blog


    Please read the Support Guide
  • yeah - I considered that, but if I'm going to do that, then its just as easy to assignthe var from the function. ah well... just trying to clean up the code a little.
  • Of course, you could use the plugin dedicated to the purpose....

    Code

    /**
     * Smarty function to get module variable
     *
     * This function obtains a module-specific variable from the PostNuke system.
     *
     * Note that the results should be handled by the pnvarprepfordisplay of the
     * pnvarprephtmldisplay modifiers before being displayed.
     *
     *
     * Available parameters:
     *   - module:   The well-known name of a module from which to obtain the variable
     *   - name:     The name of the module variable to obtain
     *   - assign:   If set, the results are assigned to the corresponding variable instead of printed out
     *
     * Example
     *   <!--[pnmodgetvar module="Example" name="foobar"|pnvarprepfordisplay]-->
     *
     *
     * @author       Andreas Stratmann
     * @since        03/05/19
     * @param        array       $params      All attributes passed to this function from the template
     * @param        object      &$smarty     Reference to the Smarty object
     * @return       string      The module variable
     */


    --
    itbegins.co.uk - Zikula Consulting

    birtwistle.me.uk - Personal Blog


    Please read the Support Guide
  • One little trick I find useful is to assign all module vars for a module to each template. A useful feature of the assign method is that if you don't specify a template variable name and the variable is an associative array each element will be assign as a template var based on the key name. e.g. The following code will result in the template vars $key1, $key2 etc...

    Code

    $myarray = array('key1' => $val1, 'key2' => $val2, ....);
    $pnRender->assign($myarray);


    If you don't provide a module variable name to pnModGetVar it will return an associative array of all module vars. e.g. the following code will return array('itemsperpage' => 10, 'bold' = true, ......) etc.

    Code

    $modvars = pnModGetVar('Example');


    So if we combine these as follows we automatically have all module vars available in each template

    Code

    $pnRender->assign(pnModGetVar('Example'));


    You can use PHP functions directly as you've done with count but there are, IIRC, restrictions with this should the host be running in safe mode. I believe it's also possible to use modifiers (and most PHP functions can be used as modifiers) in comparisions.

    e.g.

    Code

    <!--[if count($var) gt 0]-->


    can be re-written as

    Code

    <!--[if $var|count gt 0]-->


    This last technique i've not tried so I offer no guarentee it'll work.....

    -Mark

    --
    Visit My homepage and Zikula themes.
  • Wow, that's an awesome hint, Mark! It'll really save me some time! Thanks!!!
  • Mark-

    WRT the first example you listed - does this work with "extended" assoc. arrays? eg:

    Code

    $myarray = array ('row1' => array('key1'=>'val1', 'key2'=>'val2'), 'row2' =>array('key1'=>'val3', 'key2'=>'val4'));
    $pnRender->assign($myarray);


    then in the template:

    Code

    <!--[$row1.$key1]--> <!--[$row1.$key2]--><br />
    <!--[$row2.$key1]--> <!--[$row2.$key2]-->


    displays:

    Code

    val1 val2
    val3 val4


    ????
    I've been looking for ways to deal with extended assoc. arrays. Is this it?

    thanks!
  • HammerHead

    Of course, you could use the plugin dedicated to the purpose....

    yes - you'll see (upon closer inspection) that this was in my first code example above. icon_rolleyes wink
  • Craig,

    I think the main problem with your template code is that is not quite correct. This is actually equates to PHP code of $row1[$key1]. So unless $key1 is also assigned as a template variable this won't work. Try etc.

    Note: You can use foreach to loop through a result set with nested foreach's to loop through multi-demensional result sets.

    -Mark

    --
    Visit My homepage and Zikula themes.
  • markwest

    I believe it's also possible to use modifiers (and most PHP functions can be used as modifiers) in comparisions.

    e.g.

    Code

    <!--[if count($var) gt 0]-->


    can be re-written as

    Code

    <!--[if $var|count gt 0]-->


    This last technique i've not tried so I offer no guarentee it'll work.....

    -Mark


    Anyone get this concept to work? I am trying to count the number of messages in the admin messages template and so I am using

    Code

    <!--[if $messages|count gt 0]-->

    but it doesn't seem to be working...
  • Code

    <!--[if $var.count gt 0]-->

    is probably what your looking for

    Here is my example

    Code

    $pnRender =& new pnRender('helpdesk');
    $myarray = array ('mysettings' => array('AllowAnonSubmitTicket'=>pnModGetVar('helpdesk', 'Anonymous can Submit'), 'UserLoggedIn'=>pnUserLoggedIn()));
        $pnRender->assign($myarray);
        // Return the output that has been generated by this function
        return $pnRender->fetch('helpdesk_user_main.htm');



    Then in my template

    Code

    <!--[if $mysettings.AllowAnonSubmitTicket eq 1]-->
      Display page
    <!--[else]-->
         <!--[if $mysettings.UserLoggedIn eq 1]-->
              Display Page
         <!--[else]-->
              Display Not Authorized.
         <!--[/if]-->
    <!--[/if]-->
  • In .760 you can use

    Code

    $pnr =& new pnRender('mymodule');
    $pnr->add_core_data();


    which adds an array called $pncore to the template containing a lot of useful information like loggedin, theme colors etc. See pndebug for more about this.

    Per default all modvars of the current module are added and you can also pass some more module names as parameters like

    Code

    $pnr->add_core_data('module_a', 'module_b', '/PNConfig');


    to get those modvars too.

    /PNConfig is a special "module" that assigns all pnConfigVars to the template as set in the settings.

    Frank

    --
    "He is not dangerous, he just wants to play...."

This list is based on users active over the last 60 minutes.