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 Conditionals in JavaScript?

July 29, 2010 10:11am

Subscribe [3]
  • #1 / Jul 29, 2010 10:11am

    S_Park

    4 posts

    Hi,

    Can you use EE conditional logic in <script> blocks in EE templates?

    I have the following code tested and outputting fine;

    {exp:weblog:entries weblog='image-collection' orderby='random'}
    {if featured == 'Yes'}
    {related_entries id='image-collection-01'}'{image_url}': { caption: '{caption}' },{/related_entries}
    {/if}
    {/exp:weblog:entries}

    The output is as I want;

    'http://www.mydomain.com/images/uploads/galleries/image-01.jpg': { caption: 'Caption test' },
    'http://www.mydomain.com/images/uploads/galleries/image-02.jpg': { caption: 'Caption test' },
    'http://www.mydomain.com/images/uploads/galleries/image-03.jpg': { caption: 'Caption test' },

    However, when I put the code in a JavaScript <script> block it doesnt seem to parse the conditionals and the output is;

    {if "No" == 'Yes'}'http://www.mydomain.com/images/uploads/galleries/image-01.jpg': { caption: 'Caption test.' },{/if}
    {if "Yes" == 'Yes'}{/if}
    {if "No" == 'Yes'}'http://www.mydomain.com/images/uploads/galleries/image-03.jpg': { caption: 'Caption test.' },{/if}
    {if "No" == 'Yes'}'http://www.mydomain.com/images/uploads/galleries/image-05.jpg': { caption: 'Caption test.' },{/if}
    {if "Yes" == 'Yes'}'http://www.mydomain.com/images/uploads/galleries/image-07.jpg': { caption: 'Caption test' },{/if}
    {if "Yes" == 'Yes'}'http://www.mydomain.com/images/uploads/galleries/image-11.jpg': { caption: 'Caption test' },{/if}
    {if "Yes" == 'Yes'}'http://www.mydomain.com/images/uploads/galleries/3527077024.jpg': { caption: 'Caption test' },{/if}

    Thanks,
    Steven

  • #2 / Jul 29, 2010 10:40am

    S_Park

    4 posts

    Also, a similar thing happens when using PHP in temlates.

    This works fine…

    <?php $tester = "{exp:weblog:entries weblog='image-collection'}{related_entries id='image-collection-01'}'{image_url}': { caption: '{caption}' },{/related_entries}{/exp:weblog:entries}";
    ?>

    But not this…

    <?php $getEntries = "{exp:weblog:entries weblog='image-collection'}{if featured == 'Yes'}{related_entries id='image-collection-01'}'{image_url}': { caption: '{caption}' },{/related_entries}{/if}{/exp:weblog:entries}";
    ?>

    I thought all EE code gets parsed before the PHP and JavaScript output, no? (The PHP Parsing Stage is set to output).

  • #3 / Jul 29, 2010 8:06pm

    Brandon Jones

    5500 posts

    Try adding this to config.php:

    $conf['protect_javascript'] = 'n';

    Details here. You can try with ‘y’ as well just to be sure.

  • #4 / Jul 30, 2010 6:17am

    S_Park

    4 posts

    Excellent, thanks for the help 😊

  • #5 / Jul 30, 2010 9:23am

    Ingmar

    29245 posts

    Glad to see Brandon was able to help. Please post again as needed.

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

ExpressionEngine News!

#eecms, #events, #releases