PHP to EE variable and across multiple templates?
Posted: 26 November 2008 03:25 AM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  32
Joined  09-06-2008

I’ve written some PHP to create breadcrumbs depending on what is in the address bar. This code is contained in a template called ‘breadcrumbs-top’ which is embedded at the top of all of my page templates.

Is there a way to get the variable out of the breadcrumbs-top template and accessible by the template which breadcrumbs-top is embedded in?

Thanks,

Rich

Profile
 
 
Posted: 26 November 2008 07:01 AM   [ Ignore ]   [ # 1 ]  
Grad Student
Avatar
Rank
Total Posts:  91
Joined  01-30-2008

Hi,

I think “Plugin:Cacher” can solve your problem.

Read this related case and the thread.

I did not try it my self but it seems to solve your problem:
If your template has PHP-Input-Parsing and then if you cache the php-generated text with the plug in you can use it the parent template.

Profile
 
 
Posted: 26 November 2008 08:06 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Avatar
Rank
Total Posts:  32
Joined  09-06-2008

Really trying hard to get this to work. No luck so far. I’ve tried adding this to my embedded template at the top of my main (index) template:

<?php
$site_url     
= "http://test.mydomain.com";
$url         = $_SERVER['PHP_SELF'];
$segments     = split("\/", $url);

$breadcrumbs = "";
$output = "";
$crumb_path = "";
$crumb_url = "";
$crumb_title = "";

for(
$i=1; $i<count($segments); $i++)
{
    
if($segments[$i] != "index.php" && $segments[$i] != "" && $i < count($segments))
    
{
        $crumb_title     
= str_replace("-and-", " &amp; ", $segments[$i]);
        
$crumb_title     = str_replace("-", " ", $crumb_title);
        
$crumb_title     = ucwords($crumb_title);
        
        
$crumb_path     .= $segments[$i]."/";
        
$crumb_url     = $site_url."/index.php/".$crumb_path;
        
$output     .= " >> "."<a >".$crumb_title."</a>";
    
}
}

$breadcrumbs
= "<a >Home</a>".$output;
?>
{exp
:cacher:put key="crumbs"}
<?php
print $breadcrumbs; ?>
{
/exp:cacher:put}

And in the main (index) template I’ve got the following to try and retrieve and print it out:

{exp:cacher:get key="crumbs"}
{cacher
:crumbs}

Any ideas?

Thanks,

Rich

P.S. The href attributes in my PHP code are getting eaten when I press ‘Submit Post’ on this forum.

Profile
 
 
Posted: 26 November 2008 09:35 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  91
Joined  01-30-2008

The answer is in this post of the thread that I sent to you.

Try this one in the main (index) template:

{exp:cacher:get key="crumbs" parent_template="yes"}

Profile
 
 
Posted: 26 November 2008 09:36 AM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  745
Joined  07-02-2007

Here is how it works.

Your main Index template should contain:

{cacher:crumbs}

Your “embedded” breadcumb template should contain

... Lots of php ...

{exp:cacher:put key="crumbs"}
<?php
echo $breadcrumbs; ?>
{
/exp:cacher:put}

{exp
:cacher:get key="crumbs" parent_template="yes"}

As you see, the “get” function must be in the same template as the embedded template.

 Signature 

Fielder Module ( Mass Custom Fields )
Entry Gallery ( Image Gallery per entry )
reCAPTCHA Extension
Rewrite Module


See all my EE Addons (9)

Profile
 
 
Posted: 26 November 2008 09:45 AM   [ Ignore ]   [ # 5 ]  
Grad Student
Avatar
Rank
Total Posts:  91
Joined  01-30-2008

Victor, thank you for the clarification.

Now I learned it, too smile

Profile
 
 
Posted: 26 November 2008 10:04 AM   [ Ignore ]   [ # 6 ]  
Grad Student
Avatar
Rank
Total Posts:  32
Joined  09-06-2008

Fantastic. Thanks Victor. And thanks Erdal for pointing me in the right direction.

Just need to figure out how to decode the HTML entities. Forward slashes are coming through as

&#47;

Profile
 
 
Posted: 27 November 2008 10:00 AM   [ Ignore ]   [ # 7 ]  
Grad Student
Avatar
Rank
Total Posts:  32
Joined  09-06-2008

Hmm.. been scratching my head on this one. I’ve tried htmlentities() and html_entity_decode() but the forward slashes in my cached string keep coming through as:

&#47;

Is there a way to prevent the cached data from being converted into html entities?

The data I want to cache contains html links and doesn’t get parsed properly when outputting using:

{cacher:crumbs}

Profile
 
 
Posted: 27 November 2008 10:25 AM   [ Ignore ]   [ # 8 ]  
Grad Student
Avatar
Rank
Total Posts:  32
Joined  09-06-2008

I customised the cacher plugin on my installation Victor - hope you don’t mind. Changed line 165:

$TMPL->final_template = str_replace(LD . 'cacher:' . $TMPL->fetch_param('key') . RD, $SESS->cache['vg_cacher'][$TMPL->fetch_param('key')], $TMPL->final_template);

To:

$TMPL->final_template = str_replace(LD . 'cacher:' . $TMPL->fetch_param('key') . RD, html_entity_decode($SESS->cache['vg_cacher'][$TMPL->fetch_param('key')]), $TMPL->final_template);

It does what I want it to now. Marvellous!

Profile
 
 
Posted: 27 November 2008 01:16 PM   [ Ignore ]   [ # 9 ]  
Lab Assistant
RankRank
Total Posts:  157
Joined  09-18-2007

i also wrote some php to create breadcrumbs based on what’s in the addressbar. i included that php file in my header template which contains logo and main horizontal nav in addition to the breadcrumbs. i got this working without any plugins or other stuff you’re talking about, straight “outta the box”. dont understand what’s so hard about it but im probably missing something

if you’d like to see the code feel free to drop me a mail.

Profile
 
 
Posted: 28 November 2008 02:41 AM   [ Ignore ]   [ # 10 ]  
Grad Student
Avatar
Rank
Total Posts:  32
Joined  09-06-2008
stef25 - 27 November 2008 01:16 PM

i also wrote some php to create breadcrumbs based on what’s in the addressbar. i included that php file in my header template which contains logo and main horizontal nav in addition to the breadcrumbs. i got this working without any plugins or other stuff you’re talking about, straight “outta the box”. dont understand what’s so hard about it but im probably missing something

if you’d like to see the code feel free to drop me a mail.

I had my reasons for taking an alternative approach.. carry on reading if you are interested to hear what they were.

On the site that I’m working on there were instances where the address bar didn’t relate to an actual physical expression engine page e.g.:

http://www.mydomain.com/index.php/clients/portfolio/isas-and-sipps/isas/

Breadcrumbs for that would look like:

Home > Clients > Portfolio > Isas & Sipps > Isas

You’d expect that clicking on the ‘Isas & Sipps’ crumb would take you to the Isas & Sipps index page. There isn’t one! (Clients wanted to retain URLs exactly as per existing static site - even after my recommendations.)

So I wanted to use PHP header() function to redirect the user to the relevant existing page and to do that requires calling it before any HTML is output to the browser. The site is very large and the pages had already been created so I created a ‘breadcrumb-top’ template to embed at the top of all my page templates to look for any URLs which needed to be redirected. Because the breadcrumb links were created inside this template, I needed a way to get them out hence the use of Victor’s Cacher plugin.

I’m relatively new to EE but have been developing PHP apps for nearly 5 years now so I tend to still approach EE problems from an experienced PHP developer’s point of view.

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 66388 Total Logged-in Users: 40
Total Topics: 84713 Total Anonymous Users: 18
Total Replies: 454686 Total Guests: 189
Total Posts: 539399    
Members ( View Memberlist )