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.

Square brackets in templates

January 14, 2012 4:45am

Subscribe [3]
  • #1 / Jan 14, 2012 4:45am

    Fritzinger

    35 posts

    I’ve created a template that outputs JSON like this:

    [
    {exp:mylib:mytag channel="mychannel" backspace="1"}
      {"entry_id":"{entry_id}","title":"{title}"},
    {/exp:mylib:mytag}
    ]

    It works fine when mytag returns data. The response is, e.g.:

    [{"entry_id":"252","title":"mytitle1"}, {"entry_id":"218","title":"mytitle2"}]

    When the tag returns no entries, I expect the template to output just

    []

    . Unfortunately I’m not getting that, I get nothing back from EE at all: zero bytes. When I point the a web browser at the URL the same thing happens.

    EDIT: A static .html file served from the same Apache server/virtual host does send empty square brackets, so it’s not just a quirk of the web server.

    Please don’t suggest that I use one of the existing json modules (I already am for some stuff), I’d like to know the principle of why EE doesn’t treat this template the way I expect. The way one learns is by understanding, not by implementing non-fixes.

  • #2 / Jan 15, 2012 8:29am

    Sean C. Smith

    3818 posts

    Hi Fritzinger,

    I’m unfamiliar with this, but will ping one of our developers to get an answer for you.

    Sean

  • #3 / Jan 16, 2012 11:28am

    Robin Sowell

    13255 posts

    This is a custom add-on?  And are you nesting it in anything?  If it’s custom and you’re not nesting it, you should be able to return whatever you want.  But I suspect I’m not seeing the big picture. 

    What’s your mytag method look like regarding returns?

    And I may shift this over to the developer forum unless it looks like an issue with nesting.

  • #4 / Jan 16, 2012 5:13pm

    Fritzinger

    35 posts

    At the risk of making myself look stupid:

    One of the steps I took when debugging this problem was to create a template that literally consisted of just two square brackets:

    []

    I would swear that I visited the page in firefox and got nothing, and that I confirmed that the response body was zero bytes with wget and firebug. Based on that behaviour, I concluded that the problem was not my add-on, which yes, is my own code (custom add-on that gives search results ranked by relevance to the query).

    However I’ve just tried the same two-bracket template on a different machine (it’s past midnight and I promise I’m not at work 😊) and it totally works as expected there… the elusive empty brackets are printed. So I guess either it’s an environment thing, or I didn’t do what I thought I did on my work machine and it actually is my code at fault. I’ll check in the morning.

  • #5 / Jan 16, 2012 6:40pm

    Robin Sowell

    13255 posts

    Hrm- no, it’s always good to ask.  It almost sounds like you might have been throwing a php error when testing the first time- and depending on output settings you may not have seen it- hence white screen.  But yes- there’s no way the tag itself should be wiping out data outside of the tag unless something is going very, very wrong.

    In truth- I’d probably code the return to return the empty brackets upon no results (or make a no_results conditional and put them in that)- but it’s moot to the main issue.  Those brackets should be showing.  If not- make sure you’ve got errors showing on the frontend.  You can even introduce a php error in the test module to make sure you see an error rather than a white screen.

    In any case- let us know what you find once you take a closer look.  If something odd is going on- we’ll get it hashed out.

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

ExpressionEngine News!

#eecms, #events, #releases