Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

How to create a jQuery object within the template without trailing comma


Go to End


4 Posts   1712 Views

Avatar
s_a_m_y

Community Member, 31 Posts

18 December 2014 at 5:55am

Hi everyone,

I have created a lovely project which workes perfectly on all devices and browsers BUT IE8. It seems the troublesome code snippet comes from an object, that I create within my template and pass on as js-variable. It is generated like this:

<script>
	var items = [<% loop GetSlides %>{"img": "<% with Slide %>{$Filename}<% end_with %>", "caption": "$Title"},<% end_loop %>];
</script>

and passes this on to my jQuery script-file:

var items = [{"img": "assets/Slides/ideas.jpg", "caption": "Funding Your Ideas"},{"img": "assets/Slides/ecommerce-portfolio-4.jpg", "caption": "Designing the Future of E-Commerce"},{"img": "assets/Slides/disruptive-technologies.jpg", "caption": "Redefining Markets with Disruptive Technologies"},{"img": "assets/Slides/market.jpg", "caption": "A New Generation of Marketplaces"},];

However IE8 cannot deal with the final, trailing comma and blocks all further scripting. I have tried slicing the last two characters from the sting and re-adding the ] as a quick and dirty approach with no luck. It seems to break the object. Is there a way to create the value of the object in php without the trailing comma or is there a "join"-method for the template I can use? Any ideas greatly appreciated!

Sam

Avatar
Devlin

Community Member, 344 Posts

18 December 2014 at 11:25pm

Edited: 18/12/2014 11:28pm

You can try omitting the trailing comma with $Last.

For example:

<% loop $Slides %>
$Slide.Filename
<% if $Last %><% else %>,<% end_if %>
<% end_loop %>

http://doc.silverstripe.org/framework/en/reference/templates#position-indicators

Avatar
s_a_m_y

Community Member, 31 Posts

7 January 2015 at 2:27am

Hi Devlin,

that could work. I will try it and post the result.
Thanks a lot and happy new year!

Sam

Avatar
s_a_m_y

Community Member, 31 Posts

7 January 2015 at 2:32am

That worked! Thanks again :-)