Pass a PHP array to an embedded template
Posted: 29 November 2008 10:21 AM   [ Ignore ]  
Summer Student
Total Posts:  2
Joined  11-29-2008

Hello everyone. I’m trying to learn ExpressionEngine over the weekend and am having some difficulty with passing a PHP array to an embedded template. Basically, I want to be able to send an array of modules that I want to include in the left column and right column of each page, with the values of the array being the names of the modules to be embedded.

Here’s what I’ve got so far.

My index template is set to allow PHP and is set to the input parsing stage. The index looks like this:

{embed="templatename/.header" url_title="index" page_template="homepage"}
<?php
    $left_col
= array(
        
"about",
        
"spotlights");
?>
{embed
="templatename/.left-col"}
{embed
="templatename/.footer"}

Then my .left-col template looks like this:

<div id="left-col">
<?php
    
foreach ($left_col as $value)
    
{?>
{embed
="myerschang/<?php print $value; ?>"}
<?php
    }
?>
        
</div>

The problem I’m having is that the array $left_col is not being passed to the .left-col template. In my mind, I was thinking that embedding templates would be the same concept as including PHP files within other PHP files, and any included files would be able to access and manipulate variables in the parent PHP file. Is there a way to do this with either PHP or ExpressionEngine?

Profile
 
 
Posted: 29 November 2008 11:35 AM   [ Ignore ]   [ # 1 ]  
Grad Student
Avatar
Rank
Total Posts:  65
Joined  05-01-2007

In your main template you should put something like :

{embed="templatename/.header" url_title="index" page_template="homepage"}

{embed
="templatename/.left-col" left_col="about|spotlights"}
{embed
="templatename/.footer"}

Where “left_col” is a custom variable. Separate all your values by some custom trigger (ie a pipe).

Then in your “left-col” templates add this code :

<?php
$left_col
= "{embed:left_col}";
$block = explode("|", $left_col);
foreach (
$block as $value) {
?>
{embed
="myerschang/<?=$value;?>"}
<?php
}
?>

In fact I think variables (in this case array) declared in another block aren’t passed to the embed. You should pass it as parameter then define it in your embedded block.

I’m not sure it’ll work, so please try and tell me if everything is ok.

If it don’t it might be because templates variables are parsed before PHP, and so {embed} tag in your left-col templates is undefined.

Anyway, I don’t know what you really want to do but you may simply do that in your main template, it will be easier :

{embed="templatename/.header" url_title="index" page_template="homepage"}

<div id="left-col">
{embed="myerschang/about"}
{embed
="myerschang/spotlights"}
</div>

{embed="templatename/.footer"}

 Signature 

Portfolio : http://www.evasion.cc
Weblog : http://www.evasion.cc/blog

Profile
 
 
Posted: 29 November 2008 12:08 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  2
Joined  11-29-2008

Yes, your example worked as intended. Thanks!

The reason I didn’t include them individually is that I’m using the .left-col and .right-col templates to output the same HTML around any content modules within those columns. So as an example, on one page I have:

<div class="module about">
                <
h1 id="hdr_welcome" class="fir">Welcome</h1>
                <
div class="mce-module">
                    <
p>About text here</p>
                </
div>
            </
div>

            <
div class="module spotlights">
                <
h2>Featured:</h2>
                <
ul>
                    <
li>
                        <
img src="./images/img_featured-1.jpg" alt="Thumbnail" />
                        <
div class="details">
                            <
h3><a href="#">Title of the 1st news item</a></h3>
                            <
p>Description of the news item goes here here here here here here here here here here here here here here here here here here here here here.</p>
                        </
div>
                    </
li>
                    <
li class="last">
                        <
img src="./images/img_featured-2.jpg" alt="Thumbnail" />
                        <
div class="details">
                            <
h3><a href="#">Title of the 2nd news item</a></h3>
                            <
p>Description of the news item goes here here here here here here here here here here here here here here here here here here here here here.</p>
                        </
div>
                    </
li>
                </
ul>
            </
div>

Where on another page I may have two completely different modules but still need the HTML surrounding them (ie the left column HTML). What I then do is create templates for each module and then pass an array of which modules to place in each column. I’m basically trying to create a Django-like template structure, with the base template embedding either the left and right column templates or the full width template, then the column templates embed the modules that were specified in the base template.

I think I’m going to encounter issues with passing other arguments to the modules, however. As an example, if I want to have an instance of a photo module on the homepage and give it an image source different than the instance of the photo module on the about page I’d need to pass the image source and the alt text to the module. The way I’d like to do this is create an array within an array to contain variable and value pairs that are to be sent to the module. So if the photo module looks like this:

<div class="module photos">
                <
img src="{site_url}myerschang/images/{embed:photo-src}" alt="{embed:alt}" />
            </
div>

then in the index template, I’d have:

{embed="myerschang/.left-col" left_col="about|photos{variables='photo-src="img_photo.jpg"|alt="Photo alt text"'}"}

and in the .left-col template I’d have:

{embed="myerschang/<?php print $value; ?>"
<?php
    $variables
= "{embed:variables}";
    
$variables = explode("|", $variables);
    foreach(
$variables as $value)
    
{
        
print $value . " ";
    
}
?>}

Maybe there’s a much better way to do this, but I’d like to create only one version of each module type and pass it data specific to the page that is using it, rather than create a duplicate module with slightly different HTML for each page.

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: 66391 Total Logged-in Users: 44
Total Topics: 84714 Total Anonymous Users: 14
Total Replies: 454693 Total Guests: 196
Total Posts: 539407    
Members ( View Memberlist )