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.

Pass a PHP array to an embedded template

November 29, 2008 12:21pm

Subscribe [3]
  • #1 / Nov 29, 2008 12:21pm

    Shane Riley

    5 posts

    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?

  • #2 / Nov 29, 2008 1:35pm

    evasion

    58 posts

    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"}
  • #3 / Nov 29, 2008 2:08pm

    Shane Riley

    5 posts

    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">
                        About text here
                    </div>
                </div>
    
                <div class="module spotlights">
                    <h2>Featured:</h2>
    <p>                <ul><br />
                        <li><br />
                            ./images/img_featured-1.jpg<br />
                            <div class="details"><br />
                                </p><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.<br />
                            </div><br />
                        </li><br />
                        <li class="last"><br />
                            ./images/img_featured-2.jpg<br />
                            <div class="details"><br />
                                </p><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.<br />
                            </div><br />
                        </li><br />
                    </ul><br />
                </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">
                    {site_url}myerschang/images/{embed:photo-src}
                </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.

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

ExpressionEngine News!

#eecms, #events, #releases