ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Accessing this->EE->upload in Templates

February 25, 2012 3:35am

Subscribe [1]
  • #1 / Feb 25, 2012 3:35am

    Geoff Shannon

    6 posts

    So I’ve been hired by a client to upgrade their site from EE 1.x to 2.x.

    As part of the upgrade, I’ve got to fix some legacy code in the templates.  This works just fine in EE 1.x but I don’t really know how to approximate it in EE 2.x

    <?PHP
    
    global $IN;
    $parameters = "";
    foreach ($_GET as $key => $value) { $key = $IN->clean_input_data($value); }
    if ($category = $IN->GBL('category', 'GET')) { $parameters = $parameters . " category=\"" . $category . "\""; }
    if ($author = $IN->GBL('author', 'GET')) { $parameters = $parameters . " author_id=\"" . $author . "\""; }
    if ($search = $IN->GBL('search', 'GET')) { $parameters = $parameters . " search:text=\"" . $search . "\" search:title=\"" . $search . "\""; }
    if (is_numeric($IN->fetch_uri_segment('2')) && is_numeric($IN->fetch_uri_segment('3'))) {
      $url_title = " url_title=\"{segment_4}\"";
    }
    
    ?>

    I first tried to solve it in the most straightforward way I could think of:

    <?php
    /* This is gnarly.  Needs to be replaced ASAP
        Basically all that it does is build a parameter list for the channel display tag */
    $thisone =& get_instance();
    $parameters = '';
    if ($category = $thisone->input->get('category', TRUE)) {$parameters .= ' category="' . $category . '"';}
    if ($author = $thisone->input->get('author', TRUE)) {$parameters .= ' author_id="' . $author . '"';}
    if ($search = $thisone->input->get('search', TRUE)) {$parameters .= ' search:text="' . $search . '" search:title="' . $search . '"';}
    ?>

    I have also tried all sorts of variations on the above code:

    $this->EE =& get_instance();
    
    ...$thisone->EE->input->get…

    I’ve also tried omitting the call to get_instance() and then attempting to access the global object as though it has been set already:

    ...$this->EE->input->get…

    Or treating the current $this object as the get_instance() value:

    ...$this->input->get…

    etc.

    So now, I’m thinking that I should either try to accomplish the same things that this code is doing by CREATING a plugin that returns the data I want.  Other than that I have no ideas.  The problem with this idea for me is that the variables $search, $category, $author are occasionally used in other <?php ?> snippets throughout the templates, especially as part of the test in conditionals.

    Any advice on how to move forward with this would be much appreciated.

  • #2 / Feb 26, 2012 11:34am

    Dan Decker

    7338 posts

    Hi earthlingzephyr,

    Welcome to the Forums!

    I’ll do what I can to help you improve on this!

    So now, I’m thinking that I should either try to accomplish the same things that this code is doing by CREATING a plugin that returns the data I want.

    This is the generally preferred approach. A good rule is if you need to more than the most basic things with PHP, it’s a cleaner and safer approach to write a plugin. Especially if the code is going to be repeated throughout the site.

    Now, the EE object is already assigned by the time templates come into play, and you can verify this by running this in a test template with PHP set on input:

    <?php
    echo '<pre>';
    var_dump($this->EE);
    echo '</pre><p>‘;<br />
    ?>
    </pre>
    With that, you *should* be able to get to the input object with this:

    $this->EE->input->get('search');
    /*do a quick test*/
    echo $this->EE->input->get('search');

    If you only need to set some channel tag parameters dynamically, have a look at dynamic_parameters=

    Cheers,

     

  • #3 / Feb 27, 2012 8:57pm

    Geoff Shannon

    6 posts

    Thanks a lot Dan, that was exactly the information I was looking for!

    I would have sworn that I tried directly accessing $this->EE before, but apparently not.

  • #4 / Feb 28, 2012 3:10pm

    Shane Eckert

    7174 posts

    Hello earthlingzephyr,

    I am happy to see that Dan was able to help you out. He is a PHP Ninja.

    Thank you for posting your question. I hope you have a great week!

    Cheers,

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases