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 variables in embedded templates.

December 06, 2010 9:22am

Subscribe [3]
  • #1 / Dec 06, 2010 9:22am

    Ok, here’s a tricky one for you guys:

    I have 3 files:
    index.php (which is a template)
    language.php ( which is a php file )
    header.php ( which is a template )

    Index.php embeds header.php
    header.php includes language.php

    the objective is to read/pass a php variable from language.php to index.php
    can that be done ?


    An alternative solution would be to make header.php a normal php file ( no template ) and include
    it in the index.php ( instead of embedding ) BUT I still need to be able to use ee-tags in header.php ( to read out a weblog )

    any ideas ?

    ( I hope this post makes sense, it’s rather hard to explain )

  • #2 / Dec 06, 2010 5:07pm

    Ingmar

    29245 posts

    Sorry, I’m not following. This is using EE, yes? Which version and build? Are you naming your regular templates with a .php extension?

  • #3 / Dec 07, 2010 4:35am

    Sorry, I’m not following. This is using EE, yes? Which version and build? Are you naming your regular templates with a .php extension?

    Yes this is using EE. ( v 1.6.8 )

    Yes I name my regular templates with .php extensions, as I use quite a bit of php in them.


    Index.php is the main file of my site.
    I embed header.php which holds everything that appears on every page ( stylesheets, menu, etc )
    But since I’m using a EE weblog to take care of the SEO, I need header.php to be an EE template.
    ( so I can do this:

    {exp:weblog:entries weblog="seo"}<title>{titel}</title>{/exp:weblog:entries}

    )

    But since this is a multilingual site, I need to load some .php files that hold my translations.
    The translations are stored like this: $_home = “home”.
    I include this language file in my header ( so I can use it on every page, since I’m embedding the header template on every page )

    So now, I want to use one of my language variables ( defined in language.php ) in my index template ( and every other template where I embed the header template… which includes the language file )

    So I’m trying to pass a php variable, from language.php, through the header template, to the index template.

    ( index embeds header, header includes language.php )

  • #4 / Dec 07, 2010 6:29am

    little update:

    I got this working by splitting up my code and moving everything EE-related in 1 file and all the .php in a php file.

    Then I do this to embed/include both:

    <?php include '../themes/site_themes/TEMPLATE/init.php'; ?>
    {embed="TEMPLATE/header" language="<?php echo $language; ?>" page="<?php echo $page; ?>"}

    This is the code I’m using to embed/include both files in EE temmplates, but now I have to do the same thing in a php file.

    So far I’ve got this:

    include '../themes/site_themes/TEMPLATE/init.php';    
    $curl_handle=curl_init(); 
    curl_setopt($curl_handle,CURLOPT_URL,'http://URL/TEMPLATE/index.php/header/');
    curl_exec($curl_handle);
    curl_close($curl_handle);

    This works, except for the variables I’m passing to the embed files ( shown in the EE example above )
    So, how do I pass these variables to the EE template ?
    I’m guessing using CURL DATA might do the trick, haven’t figured it out entirely yet.

    Any ideas/suggestions ?

  • #5 / Dec 07, 2010 6:52am

    John Ryan

    51 posts

    Are the PHP files ExpressionEngine templates? Or are they just regular PHP files sitting on your server?

  • #6 / Dec 07, 2010 6:58am

    Are the PHP files ExpressionEngine templates? Or are they just regular PHP files sitting on your server?

    They are just regular php files.

    I tried the technique described in this post to render those EE tags in a php file:
    http://expressionengine.com/wiki/Google_Sitemaps/ (last code snippet in that post)

    but I understand why it isn’t working now. 😊 Just thought it might be worth giving a shot.

    So I’m still looking for a way to embed/include an EE template in a (normal) php file.

  • #7 / Dec 07, 2010 7:16am

    John Ryan

    51 posts

    Is there any reason why you wouldn’t use a regular EE embed / inline PHP in a template?

  • #8 / Dec 07, 2010 7:27am

    Is there any reason why you wouldn’t use a regular EE embed / inline PHP in a template?

    Because the projects is split into 2 big parts: an EE powered website and a custom webshop built in php.
    The only connection between the website and the webshop is that they share the same header.

    The header used to be a normal .php file, so I could include it in the EE website and the php webshop.
    But now I’m trying to move my SEO into EE.

    So I created a weblog that holds all my SEO ( title, description, keywords ) and output that data into my header. but for that my header needs to be an EE template file.

    Problem is, that now I can’t include that header EE template file in my webshop .. because that part is all php. So I’m trying to find a way around that problem and being able to embed or run an EE template from inside a normal php file would do the trick.

  • #9 / Dec 07, 2010 7:47am

    ugh, forget it.

    I just worked around it by embedding the header in the EE templates
    and including it in the .php files.

    When including it, the EE obviously don’t work, so I cant output the data from my SEO weblog,
    but I just used some PHP to check if the EE tags work, and if not set a default title, description, keywords.

    It was still tricky to include the header EE template as a normal php because I had to pass some variables but I solved it like this:

    In the php file that includes the header:

    $data = 'template'  . "=" . $template. "&" . 
        'language'  . "=" . $language
        ;        
            
    $curl_handle=curl_init(); 
    curl_setopt($curl_handle,CURLOPT_URL, 'URL/themes/site_themes/ballchairshop/header.php?' . $data);
    curl_exec($curl_handle);
    curl_close($curl_handle);

    in the EE header template:

    if($_GET['language'] != ""){
        $language = $_GET['language'];
        $template = $_GET['template'];
    }else{
        $language = "{embed:language}";
        $template = "{embed:template}";
    }


    I made this a lot harder then it should have been. 😊

  • #10 / Dec 07, 2010 4:13pm

    Ingmar

    29245 posts

    Excellent 😊 Thanks for sharing. Please post again as needed.

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

ExpressionEngine News!

#eecms, #events, #releases