Log in | Register | Lost password

Bottom
Assigning value returnd by a function to a template variable
  • Posted: 15.12.2006, 14:36
     
    Converted
    rank:
    12
    registered:
     March 2009
    Status:
    offline
    last visit:
    Posts:
    0
    Hi,
    I have created a custom pagesetter function that returns a string to the function call. For example, suppose the function is written as:

    Code

    function smarty_function_pagesetter_dummy()

    {

    return "Hello World";

    }


    If this function is called from within a Pagesetter template using:

    Code

    <!--[pagesetter_dummy]-->


    The string "Hello World" is displayed in the browser.

    I was wondering if it is possible to assign the string returned by the function to a smarty variable, perhaps using something similar to the folowing:

    Code

    <!--[ assign var='name' value='Bob' ]-->

    $name = <!--[$name]-->


    but replacing the literal string 'Bob' with the function call. I have tried the following:

    Code

    <!--[ assign var='name' value=pagesetter_dummy ]-->

    $name = <!--[$name]-->


    but nothing was assigned to $name.

    Does anyone know how I could do this?

    Regards

    Robbie
  • Posted: 15.12.2006, 16:24
     
    Converted
    rank:
    12
    registered:
     March 2009
    Status:
    offline
    last visit:
    Posts:
    0
    Hi,

    You could do that with your code:

    Code

    <!--[capture assign="myvar"]--><!--[pagesetter_dummy]--><!--[/capture]-->



    But, there is a better code:

    Code

    function smarty_function_pagesetter_dummy($args, &$smarty){

    $output = "Hello World";

    if (isset($args['assign']))

    $smarty->assign($args['assign'], $output);

    else

    return $output;

    }



    And call it with:

    Code

    <!--[pagesetter_dummy assign="myvar"]-->


    chaK!
  • Posted: 18.12.2006, 09:32
     
    Converted
    rank:
    12
    registered:
     March 2009
    Status:
    offline
    last visit:
    Posts:
    0
    Thanks Chakal,
    Your solution gives me alot more flexibility and power within my Pagesetter templates. I am wondering if this type of information is available anywhere for beginner users like myself to read and learn - I don't see it included in the Pagesetter documentation?

    Regards

    Robbie
  • Posted: 18.12.2006, 10:56
     
    Converted
    rank:
    12
    registered:
     March 2009
    Status:
    offline
    last visit:
    Posts:
    0
    Look at Smarty documentation:
    http://smarty.php.net/

    There is a lot of function/modifier included in smarty core and more to download.

    chaK!

Template courtesy of Designs By Darren.