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.

PHP to EE variable and across multiple templates?

November 26, 2008 5:25am

Subscribe [3]
  • #1 / Nov 26, 2008 5:25am

    tr309

    24 posts

    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

  • #2 / Nov 26, 2008 9:01am

    Erdal Demirtas

    84 posts

    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.

  • #3 / Nov 26, 2008 10:06am

    tr309

    24 posts

    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-", " & ", $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.

  • #4 / Nov 26, 2008 11:35am

    Erdal Demirtas

    84 posts

    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"}
  • #5 / Nov 26, 2008 11:45am

    Erdal Demirtas

    84 posts

    Victor, thank you for the clarification.

    Now I learned it, too 😊

  • #6 / Nov 26, 2008 12:04pm

    tr309

    24 posts

    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

    /
  • #7 / Nov 27, 2008 12:00pm

    tr309

    24 posts

    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:

    /

    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}
  • #8 / Nov 27, 2008 12:25pm

    tr309

    24 posts

    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!

  • #9 / Nov 27, 2008 3:16pm

    stef25

    268 posts

    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.

  • #10 / Nov 28, 2008 4:41am

    tr309

    24 posts

    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.:

    <a href="http://www.mydomain.com/index.php/clients/portfolio/isas-and-sipps/isas/">http://www.mydomain.com/index.php/clients/portfolio/isas-and-sipps/isas/</a>

    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.

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

ExpressionEngine News!

#eecms, #events, #releases