Results 1 to 3 of 3

Thread: contao gives me unusable variable

  1. #1
    New user
    Join Date
    08-12-12.
    Posts
    6

    Default contao gives me unusable variable

    I'm having problems with a php variable I am getting from Contao.



    Code:
    $myPageVar = '{{page::id}}'; 
    echo $myPageVar // returns 17
    echo gettype($myPageVar); //returns string

    This seems OK but whenever i convert to an integer I get a value of 0. When I try to use the variable in a condition it also returns false.



    Code:
    if ($myPageVar == "17")
    {
          //returns false
    }

    What's going on with the value I get from Contao? How do I convert $myPageVar to a useable variable with value = 17? Thanks in advance if anyone can help.

  2. #2
    New user
    Join Date
    08-12-12.
    Posts
    6

    Default Re: contao gives me unusable variable

    I tried various ways of converting to an INT with no success. Now I know the problem. My variable, although it outputs "string" when type tested, is actually an array starting with two curly braces and that is what is confusing php. I've got it sorted now. In case this helps any Contao users trying to write php with contao variables.....

    Don't do this:

    Code:
    $myPageVar = '{{page::id}}';//gives you something php doesn't understand
    Do this:

    Code:
    $myPageVar = $this->replaceInsertTags( '{{page::id}}' ); //for a php friendly variable

  3. #3
    Experienced user
    Join Date
    08-21-09.
    Posts
    563

    Default Re: contao gives me unusable variable

    You're on the right track, but the insert-tags are not meant to be used that way. They're more of a convenience tool for the backend, as that's the only way to insert dynamic data in form fields (you can't put PHP code directly in your "title" field for example, and for good reason).

    So your first example is interpreted as just a string by PHP -- Contao doesn't have access to something like a local variable that you define (I have no idea why it even returned '17'. Is that the correct ID?)

    Your second example works because you're manually calling a method that Contao uses to replace those tags in content that gets entered in the backend. So it works, but it's very roundabout, and it also makes the server do more work than it has to.

    For what you're trying to do, plain 'ol PHP does the trick. You can access any page variable from just about anywhere as it's a Global variable. You can get to it in two ways:

    Code:
    <?php
        global $objPage;
        $myPageVar = $objPage->id;
    
    // or just...
        $GLOBALS['objPage']->id;
    ?>
    
    // Peak at what's available;
    <pre><?php print_r($GLOBALS['objPage']); ?></pre>
    Pretty much anything you want is available, it's just a matter of knowing where to find it. A couple are global variables, many are through the framework and library objects.
    Brian

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •