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.

The EE Template parser eats JSON for breakfast

September 24, 2009 4:36pm

Subscribe [4]
  • #1 / Sep 24, 2009 4:36pm

    Aaron Gustafson

    20 posts

    We came across a weird bug in EE (specifically 1.6.8) where JSON in templates was getting axed by the template parser. Specifically, this hit us on Typedia when we were implementing the type specimens using SWFObject (lines wrapped for clarity):

    swfobject.embedSWF( "/s/holder.swf?<?php echo rand(); ?>",
      "{url_title}-specimen-small{embed:rand}", "110", "50", "8.0.0", false,
      {sampleFile: '{file}', sampleName: '{url_title}', sampleSize: 'small'},
      {menu: 'false', wmode: 'opaque'}, {id: '{url_title}-specimen-small{embed:rand}', 
      name: '{url_title}-specimen-small{embed:rand}', styleclass: 'small-specimen has-image'});

    was being sent to the browser as

    swfobject.embedSWF("/s/holder.swf?<?php echo rand(); ?>", "{url_title}-specimen-small{embed:rand}", 
      "110", "50", "8.0.0", false, , , );

    To fix the issue, we forced some line breaks:

    swfobject.embedSWF(
      "/s/holder.swf?<?php echo rand(); ?>",
      "{url_title}-specimen-small{embed:rand}", "110", "50", "8.0.0", false,
      {
        sampleFile: '{file}', sampleName: '{url_title}', sampleSize: 'small'
      },
      {
        menu: 'false', wmode: 'opaque'
      },
      {
        id: '{url_title}-specimen-small{embed:rand}', 
        name: '{url_title}-specimen-small{embed:rand}',
        styleclass: 'small-specimen has-image'
      });

    My guess is that the issue has something to do with a reprogrammed regular expression that is being a little too greedy, but that’s just a guess.


    [Mod Edit: Moved to the Technical Support forum]

  • #2 / Sep 24, 2009 4:55pm

    Ingmar

    29245 posts

    I think the problem’s that the EE parser has no way of knowing if a very long string in curly braces really is an EE variable, or not. What template type did you use? Forcing line breaks is an accepted workaround, usually.

  • #3 / Sep 24, 2009 5:38pm

    Aaron Gustafson

    20 posts

    I think the problem’s that the EE parser has no way of knowing if a very long string in curly braces really is an EE variable, or not.

    It seems like it may be a good idea to have it look for JSON syntax and just leave it along if it is encountered. I’ll look into this more later.

    What template type did you use?

    Not sure I follow. We are using template files with a little PHP sprinkled in.

  • #4 / Sep 24, 2009 5:45pm

    Ingmar

    29245 posts

    It seems like it may be a good idea to have it look for JSON syntax and just leave it along if it is encountered.

    I am not sure that’s feasible in all circumstances, but you’re certainly welcome to make a feature request to that effect.

    Not sure I follow. We are using template files with a little PHP sprinkled in.

    Does that mean you are saving your templates as files? I was referring to template types, like “Web Page” or “Static”. You might want to try “Javascript”.

  • #5 / Sep 24, 2009 6:07pm

    Lisa Wess

    20502 posts

    In addition there are a few things to check:

    1.  Add protect_javascript to your config.php

    2.  Make sure that Debugging is set to 1 in Admin -> System Preferences -> Output and Debugging

  • #6 / Sep 25, 2009 9:42am

    Aaron Gustafson

    20 posts

    Does that mean you are saving your templates as files? I was referring to template types, like “Web Page” or “Static”. You might want to try “Javascript”.

    Ok. Gotcha. We are using templates as files. We are also sending as denoting the templates as “Web Page,” sorry I wasn’t following you. If we were to change to “Javascript,” would that send text/javascript or application/javascript headers or is that type assignment only for EE’s internal purposes?

    Add protect_javascript to your config.php

    I wasn’t aware of that one… I’ll give it a shot.

    Make sure that Debugging is set to 1 in Admin -> System Preferences -> Output and Debugging

    I think we have it set to 1, but I’m curious why/how that would help. It doesn’t seem like a related property.

  • #7 / Sep 25, 2009 9:49am

    Aaron Gustafson

    20 posts

    Yup, setting

    $conf['debug'] = '1';

    Seems to trigger the JSON to be protected. How many other seemingly unrelated things does that configuration option control?

  • #8 / Sep 25, 2009 12:44pm

    Ryan M.

    1511 posts

    I have used JSON a bit on my sites (usually with the brackets on their own lines). I agree with Aaron, setting the $conf var ‘debug’ to 1 seems unrelated to the issue at hand. I just checked my config.php file for devot-ee and it is set to 1. Isn’t that the default? For the record I always set $conf[‘protect_javascript’] = “n”; as well.

  • #9 / Sep 25, 2009 12:57pm

    Ingmar

    29245 posts

    Yes, it’s the default. It’s recommended that errors always be shown to Superadmins.

  • #10 / Oct 21, 2009 9:42pm

    Cameron Corda

    31 posts

    We’ve ran into this a few times lately.  One example:

    // template code in ee
    [removed]
    jQuery(function() {         
        jQuery("a[rel]").overlay({ })              
    });
    [removed]
    
    // source in html
    [removed]
    jQuery(function() {         
        jQuery("a[rel]").overlay();
    [removed]

    So is the solution is to set protect_javascript to ‘y’?

  • #11 / Oct 21, 2009 11:43pm

    Aaron Gustafson

    20 posts

    Set $conf[‘debug’] = ‘1’; and that seems to do it.

  • #12 / Oct 21, 2009 11:50pm

    Cameron Corda

    31 posts

    Aaron, isn’t that the “Debug Preference: Enables the display of error messages, which are valuable during site development” setting?

    Seems unrelated as you and Ryan noted.

  • #13 / Oct 21, 2009 11:53pm

    Lisa Wess

    20502 posts

    It actually is related and is on my list of things to add to the documentation.  You should always have that setting to 1 for a variety of reasons.

    Thanks for chiming in, Aaron.

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

ExpressionEngine News!

#eecms, #events, #releases