+ Start a Discussion
James@KronosLabJames@KronosLab 

Can't use apex:repeat inside <script language="javascript">

I'd really like to populate parts of a javascript section on my page by iterating over a collection.  However, if I put the <apex:repeat> tag inside of a <script> tag, it is not evaluated.  Thus:

<apex:page controller="RepeatTestController">
{!testStrings}

<apex:repeat var="tstring" value="{!testStrings}"><br/>
var {!tstring};
</apex:repeat>

</apex:page>

Yeilds:
[A, B, C, D, E]<br/>
var A;<br/>
var B;<br/>
var C;<br/>
var D;<br/>
var E;

But:
<apex:page controller="RepeatTestController">
{!testStrings}
<script language="javascript">
<apex:repeat var="tstring" value="{!testStrings}"><br/>
var {!tstring};
</apex:repeat>
</script>
</apex:page>

Yields:
[A, B, C, D, E]
<script language="javascript">
<apex:repeat var="tstring" value="[A, B, C, D, E]"><br/>
var ;
</apex:repeat>

This is a major pain in the butt if you want to populate javascript data on the server side.

James
</script>

dchasmandchasman
This is a known and bugged unresolved issue - totally understand why you would want to do this but there is tension between autoescaping that we do to deal with issues around <script> tags and component support inside a script block that we have yet to find a workable solution to.

We've been pushing this type of javascript generation into the controller (using apex code constructs like for each loops to iterate over the collection you were exposing as a property) - which is not too far off from what you were experimenting with in the first case below.

James@KronosLabJames@KronosLab

For the moment, I'm working around it by building the entire string inside the action and just doing:

{!javascriptStuff}

Inside the script tag.  It's very clumsy though, like doing servlet programming before JSP came along.

As a side note, coding Apex is turning out to be a lot like coding Second Life.  The lack of debugging tools, the stringent resource restrictions, and the odd gaps in language functionality make you really think hard about implementation.  Not a bad thing necessarily, but very different from the J2EE morass I deal with most of the time.

James

MyGodItsColdMyGodItsCold
Are you coding SF integration in Second Life?
James@KronosLabJames@KronosLab

You could conceivably do it, since there is HTTP request support in LSL (the SL scripting language).  However, since any Web Service request to SF has to be authenticated, it would be nearly impossible.  There are no DOM parsing or constructing tools in LSL, and certainly no encryption/certificate libraries, so there would be no way to do the initial handshake.

If you could do more RESTful requests to SF, it would be possible.

James