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.

EE / Matrix tags within Javascript not working

June 05, 2011 6:57pm

Subscribe [2]
  • #1 / Jun 05, 2011 6:57pm

    Richard Wiggins

    60 posts

    Hi, I’ve not tried doing anything like the below before, plus rubbish at Javascript so please excuse newbie’ness!

    Here’s my code:

    {if "{bg-images:total_rows}" > 0}
        <div id="supersized"></div>
        [removed][removed]
        [removed]
            // initialise plugins
            $(document).ready(function() {
                /* Supersized background images */
                $.fn.supersized.options = {
                    startwidth: 1000,  
                    startheight: 750,
                    vertical_center: 1,
                    slideshow: 1,
                    transition: 1,
                    slide_interval: 8000,
                    slides : [
                        {bg-images}
                            {if row_count != total_rows}{image : '{img}'},{/if}
                            {if row_count == total_rows}{image : '{img}'}{/if}
                        {/bg-images}
                    ]
                };
                $('#supersized').supersized();    
                
                // Validate Plugin
                $(function() {
                    $("form").validate();
                });            
            });
        [removed]
    {/if}

    I’m basically testing to see if the bg image entry is the last item in the Matrix and if it is, don’t display the trailing comma, as this screws up in IE. If I put the following code loop outside the javascript is works fine, so I presume it’s a confliction between the JS and EE code.

    Here’s the loop/test in question:

    {bg-images}
        {if row_count != total_rows}{image : '{img}'},{/if}
        {if row_count == total_rows}{image : '{img}'}{/if}
    {/bg-images}

    If anyone could help, I’d really appreciate it.

  • #2 / Jun 06, 2011 6:31am

    John Henry Donovan

    12339 posts

    Richard,

    Add a few more line breaks in there to see if that works

    slides : [
                        {bg-images}
                            {if row_count != total_rows}
    {image : '{img}'},
    {/if}
                            {if row_count == total_rows}
    {image : '{img}'}
    {/if}
                        {/bg-images}
  • #3 / Jun 06, 2011 7:46am

    Richard Wiggins

    60 posts

    No, that didn’t work. Still getting the same result which actually prints the if statement into the JavaScript.

    This was take from the rendered page from Firebug:

    slides : [
        {if "1" != "3"}
            {image : 'http://ec2.dragonstaff.com/www.emmabrooksphotography.co.uk/images/uploads/bg1.jpg'},
        {/if}
        {if "1" == "3"}
            {image : 'http://ec2.dragonstaff.com/www.emmabrooksphotography.co.uk/images/uploads/bg1.jpg'}
        {/if}
        {if "2" != "3"}
            {image : 'http://ec2.dragonstaff.com/www.emmabrooksphotography.co.uk/images/uploads/bg3.jpg'},
        {/if}
        {if "2" == "3"}
            {image : 'http://ec2.dragonstaff.com/www.emmabrooksphotography.co.uk/images/uploads/bg3.jpg'}
        {/if}
        {if "3" != "3"}
            {image : 'http://ec2.dragonstaff.com/www.emmabrooksphotography.co.uk/images/uploads/bg2.jpg'},
        {/if}
        {if "3" == "3"}
            {image : 'http://ec2.dragonstaff.com/www.emmabrooksphotography.co.uk/images/uploads/bg2.jpg'}
        {/if}
  • #4 / Jun 07, 2011 5:45am

    John Henry Donovan

    12339 posts

    Richard,

    Ok so the issue is the curly braces being picked up as Javascript.

    What you could do is add an embed.Untested but might work

    {embed="/_includes/slides" entry_id="{entry_id}"}

    Then in the embed

    {exp:channel:entries entry_id="{embed:entry_id}" dynamic="no"}
     {bg-images}
                            {if row_count != total_rows}{image : '{img}'},{/if}
                            {if row_count == total_rows}{image : '{img}'}{/if}
                        {/bg-images}
    {/exp:channel:entries}
  • #5 / Jun 07, 2011 2:09pm

    Richard Wiggins

    60 posts

    John you top guy! That has done the trick so many thanks.

    To be honest, I should have thought of that, but haven’t done much EE stuff for a couple of months.

    Thanks again.

    P.S. the working version is now live here: http://www.emmabrooksphotography.co.uk

  • #6 / Jun 08, 2011 6:49am

    John Henry Donovan

    12339 posts

    Excellent 😊 and great looking site. Note for future reference you cannot use EE conditionals in your JS. Feel free to start a new thread if you have any more questions

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

ExpressionEngine News!

#eecms, #events, #releases