Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Passing data/variables from handler to template

edited November 2012 in Miscellaneous

I am creating some handlers which I call from a customized template I want to populate some variables to display data from the handler in the template.

I can do this by setting a $_POST variable (e.g. $_POST['examplefield']='exampledata', but I suspect there is a more elegant way. Any suggestions are appreciated.

Comments

  • edited November 2012

    If you want to pass a variable to an embedded handler from a template, you can reference them in the {! !} tag like this:

    {! myapp/handler?field=[field] !}
    

    The [] is a sub-expression. Note that sub-expressions can only appear in the values and not in the names of the parameters. Here's a complete example:

    The first handler:

    <?php // apps/myapp/handlers/test.php
    
    $values = array (
        'field' => 'Some value'
    );
    
    echo $tpl->render ('myapp/test', $values);
    
    ?>
    

    And the template:

    <!-- apps/myapp/views/test.html -->
    {{ field }}<br />
    {! myapp/embed?field=[field] !}
    

    And the second handler:

    <?php // apps/myapp/handlers/embed.php
    
    echo $data['field'];
    
    ?>
    

    Cheers!

  • This should output:

    Some value<br />
    Some value
    
  • edited November 2012

    I think you misunderstood me. I want to define a value in a handler, and use/display that in the template and/or view of the app.

    To be more precise, I will be using elefantcms to create pages for a Facebook app. I have some (working) PHP code that will "check" if a visitor is logged in (into Facebook) and if so, checks if the user has 'installed' the specific app.

    E.g. I have created an app that is called "fbapp", populated code in /apps/fbapp/handlers/facebook.php

    In this file I define the variable $fbuser

    I now want to display this variable in the template (layouts/fbapp.html)

    or in the view (/apps/fbapp/views/post.html

    For using it in layouts/fbapp.html, I now populate $_POST[fbuser] in the facebook.php handler and then use {{$_POST.fbuser}} to display in the template.

    But there must be a way of using something else than the $_POST array.

    Hope I was clear. Just to add: I would prefer if the variable is usable in both the template and the post view, without requiring to run the fbapp/handles/facebook.php handler in both the template and post view.

  • Ah I think I see what you mean. You want to pass custom variables into the layout template. In that case, you can just set custom properties on the $page object, which is what gets passed to the layout template:

    <?php
    
    $page->my_field = 'Some value';
    
    ?>
    

    As long as you're calling this handler directly and not via an include in the template, then you should be good to reference that as {{my_field}} in the layout.

  • Is there a way to access the page-variables in another handler/view template as well ? I now have the issue that I created some additional dynamic objects, but require a variable that is set in the page template to be present in the handler/view of the dynamic objects. So basically I am looking for a way to set a variable globally.

  • You can access the $page object from any template via {{page.title}}. The template engine basically translates that into $GLOBALS['page']->title.

Sign In or Register to comment.