1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Javascript foreach loop help

Discussion in 'Styling and Customization Questions' started by ibenick, Jan 15, 2013.

  1. ibenick

    ibenick Active Member

    I'm trying to figure out how I can take a foreach loop but run it inside of a javascript. Or if that isn't possible, rewrite it in javascript. Can anyone tell me if this is possible and maybe point me in the right direction?

    This is the foreach loop:

    Code:
    <xen:foreach loop="$items" value="$item">
             
                  <p>These are coordinates:{$item.coords}</p>
                                     
    </xen:foreach>        
    It generates a list of all the Coordinates for the Showcase items in a given category and works great.

    But what I'd like to do is be able to put that function into a piece of google maps javascript running on the same page so that it would generate a list of markers for that map, based on the coordinates in each showcase item.

    In the map javascript, a marker is written like this:

    Code:
    position: new google.maps.LatLng(40.756054, -73.986951),
    And I have maps running succesfully with the coordinates populated like this:

    Code:
    position: new google.maps.LatLng({$item.coords}),
    What I'd like to do is generate a list so that all of the items coordinates get populated in that javascript item and therefor as a pin on the map.

    Basically like this (except this obviously doesn't work):

    Code:
    <xen:foreach loop="$items" value="$item">
             
                    position: new google.maps.LatLng({$item.coords}),
                                     
    </xen:foreach>        
    Anyone know how I might be able to do this? I've searched around and found a javascript 'for-in' function but I'm totally lost on how that would apply.
     
  2. James

    James Well-Known Member

    If you put the JS inside a XenForo foreach tag, it'll do as you want.

    You'll need to wrap it in <script> tags, of course, unless it's naturally a JS template (which I don't believe allows XF syntax).
     

Share This Page