EllisLab text mark
Advanced Search
     
Out from plugin getting over-written by EE?
Posted: 17 July 2009 05:59 PM
Avatar
Joined: 2007-05-04
219 posts

Hello

I have written a plugin which simply returns the first segment of a URL. Yes I know that {segment_1} would do this, but it would appear it returns the true first segment and not the first segment viewed in a browser window when using a certain RewriteRule. Anyhow, I have written a plugin which returns successfully the first segment.

The URL in my browser window is such:

http://example.com/military/blogs/test 

Here is a pared down version of my page:

{assign_variable:section="blogs"}
{assign_variable
:template_group="blogs"}
{assign_variable
:prod_division="{exp:ooc_eeurl}"}

{exp
:weblog:entries limit="1"}
{prod_division}
{
/exp:weblog:entries}

{prod_division} 

This outputs the following:

military military 

However, if I call the variable {prod_division} before the exp:weblog:entry, the output is broken while in the loop:

Here is a pared down version of my page:
[code]{assign_variable:section="blogs"}
{assign_variable
:template_group="blogs"}
{assign_variable
:prod_division="{exp:ooc_eeurl}"}

{prod_division}

{exp
:weblog:entries limit="1"}
{prod_division}
{
/exp:weblog:entries}

{prod_division} 

This now outputs:

http://dev.whitesdiving.com/military/blogs/test 

The code in my plugin is:

<?php
/* ============================================================================
pi.ooc_eeurl.php
Returns the visual url segment and not the true url segment 

INFO --------------------------------------------------------------------------
Developed by: James Riordon, Out of Control
Created:   July 17th 2009

CHANGELOG & OTHER INFO --------------------------------------------------------
See README.textile
=============================================================================== */

$plugin_info = array(
            
'pi_name'            => 'OOC Eeurl',
            
'pi_version'        => '0.1.0',
            
'pi_author'            => 'James Riordon',
            
'pi_author_url'        => 'http://outofcontrol.ca/',
            
'pi_description'    => 'Pulls the visual URL segments instead of the real ones',
            
'pi_usage'            => Ooc_eeurl::usage()
        );


Class 
Ooc_eeurl {

    
var $return_data;


    
// ----------------------------------------
    //  eexcerpt
    // ----------------------------------------

    
function ooc_eeurl()
    
{
        
global $TMPL;

        
$segment = ( ! $TMPL->fetch_param('segment')) ? '1' :  $TMPL->fetch_param('segment');
        
        if ( ! 
is_numeric($segment) || $segment 10 $segment 1;
                
        
$this->return_data $this->_pull_url_segments($segment);
    
}

    
    
function _pull_url_segments($segment 1)
    
{
        $url_array 
explode("/",rtrim($_SERVER["REQUEST_URI"],"/"));
        
        
$theCount count($url_array);

//        $clean_url = array_map(array($this, "_clean_url"), $url_array);

        
$clean_url $url_array;

        return 
trim($clean_url[$segment]); 
   
}

    
function _clean_url($data)
    
{
        
return $data;
    
}
    
// END    

    
// ----------------------------------------
//  Plugin Usage
// ----------------------------------------

// This function describes how the plugin is used.
//  Make sure and use output buffering

function usage()
{
ob_start
(); 
?>
how to 
use it
<?php
$buffer 
ob_get_contents();
ob_end_clean(); 
return 
$buffer;
}
// END
}
// END CLASS
?> 

Most likely something I am totally breaking in my plugin, but if someone could point me to a possible solution, it would be REALLY useful, and would save what little hair I do have left on my head.

Many thanks!

 Signature 

James Riordon | Out of Control®
Website Development & Maintenance | Montreal, Canada

 
Posted: 17 July 2009 06:15 PM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2007-05-04
219 posts

More info:

This produces the expected result:

{exp:ooc_eeurl}{/exp:ooc_eeurl}

{exp
:weblog:entries}
{exp
:ooc_eeurl}
{
/exp:weblog:entries}

{exp
:ooc_eeurl}{/exp:ooc_eeurl} 

Output:

military military military military military military military military 


This however doesn’t:

{exp:ooc_eeurl}{/exp:ooc_eeurl}

{exp
:weblog:entries}
{exp
:ooc_eeurl}{/exp:ooc_eeurl}
{
/exp:weblog:entries}

{exp
:ooc_eeurl}{/exp:ooc_eeurl} 

Output:

military 0 0 
0 0 
0 0 military 
 Signature 

James Riordon | Out of Control®
Website Development & Maintenance | Montreal, Canada

 
Posted: 17 July 2009 06:19 PM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2007-05-04
219 posts

Sorry for the third post here, I just discovered that this happens with any plugin:

{exp:eexcerpt}A few words{/exp:eexcerpt}

{exp
:weblog:entries}
{exp
:eexcerpt}A few words{/exp:eexcerpt}
{
/exp:weblog:entries}

{exp
:eexcerpt}A few words{/exp:eexcerpt} 

Output:

A few words 0 0 
0 0 0 
0 A few words 

Perhaps a bug in the exp:weblog:entries tag pair?

And I am running EE version ExpressionEngine 1.6.7 Build:  20090320

 Signature 

James Riordon | Out of Control®
Website Development & Maintenance | Montreal, Canada

 
Posted: 18 July 2009 04:12 PM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2004-03-22
12308 posts

OutofControl,

I am moving this to the Plugins Technical Assistance forum for you.

I would definitely look into your Parse Order though


Also I have a look at path global variables as I use them to great effect when doing multi language sites when the first real segment inst segment_1 but one created by htaccess.

EE Docs : Path.php Global Variables

 Signature 

John Henry’s Website | Follow me on Twitter

 
Posted: 18 July 2009 09:48 PM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2007-05-04
219 posts

Hi,

I looked at the parse order and is says “# Main Parsing - Process all {exp:...} tags in order encountered.”. So that in my mind does not explain the oddness I am seeing.

As for using path.php, I ended up using that awhile after I posted my original item. Wasn’t sure if that was as good way, but your post confirms it is, Thank you ! It has also made my requirement really straight forward. Now to go back and update my other site that should use the rewrite rule, but I didn’t as I didn’t know how to then.

For those interested, the rewrite rule allows me to have a site with “divisions” (that’s what we call them), and similar content in each division. For example, we have sport, commercial and military on the site we are doing. The owner wants each division to be treated like a sub site, so that the contact page, about this company, support info etc, will appear under each division. However, each of those sections are the same for each division. So instead of duplicating the templates in each division, I can now have each section (contact, about etc) as template groups, and then reference them from within my division. Using a global path variable called {division} which references the visual first segment of the URL, I can use that as a switch to assign different menus and style sheets to each page, but leave the content the same.

To clarify:

The URL /military/contact is identical to /sport/contact and commercial/contact. The template group is contact and I use the template /contact/index.

The rewrite rule I use is:

RewriteRule ^(sport|commercial|military)/(blogs|press-releases|contact|about|support)(.*)$ /index.php?/$2$3 [L]

This tells the server to let any one accessing divisions /sport, /commercial or military, have access to the template groups blogs, press-releases, contact, about and support, from with in the divisions. After that rewrite rule, I append the normal .htaccess rewrite rule to hide the index.php file.

I wrote this after relaxing with a beer, so hopefully it is coherent.

 Signature 

James Riordon | Out of Control®
Website Development & Maintenance | Montreal, Canada