2

I'm trying to adjust a google maps so have pins that correspond with my posts. I'm nearly there but I'm trying to load info into the info window. I can set it's content already by hand. And I have turned the info i want in the bubble into a html result. All I need to do is alter this so that the twig commands create a string I can pass on in the function that will add the pin.

So I'm trying to create a string result from the code below, which has all the results in it.

Here's what I got (and this works to produce the HTML I want to put in the info window of the pin:

{% set resultsNum = 0 %}

<h3>{{ block.location }}</h3>
<ul>
{% set searchString = block.location %}
{% for entry in craft.entries.section('journal').find() %}
{% for block in entry.locations %}
    {% if block.type == "location" %}
        {% if block.location == searchString %}
            {% set resultsNum = resultsNum+1 %}
            <li><a href="{{ entry.url() }}">{{ entry.title }}</a></li>
        {% endif %}
    {% endif %}
{% endfor %}
{% endfor %}
</ul>

The resulting string would go here (this code works already if i set my own string of html:

    {% set marker = {
    lat: result.geometry.location.lat,
    lng: result.geometry.location.lng,
    content: infoHTML 
    } %}

Thanks in advance for any help!

  • Which variables are you using to create a string? – Aaron Berkowitz Apr 24 '15 at 21:07
  • None yet, but this generates the HTML I need to put into a string, which will go (see 2nd code block in first post) – Kitsune Apr 24 '15 at 21:26
  • So you need that entire HTML chuck as a string and not as rendered HTML? – Aaron Berkowitz Apr 24 '15 at 21:41
  • Yes, exactly that! – Kitsune Apr 24 '15 at 22:01
2

You can wrap your HTML in a {% set myString %} block and use myString however you need too.

{% set myString %}
{% set resultsNum = 0 %}
<h3>{{ block.location }}</h3>
<ul>
{% set searchString = block.location %}
{% for entry in craft.entries.section('journal').find() %}
{% for block in entry.locations %}
    {% if block.type == "location" %}
    {% if block.location == searchString %}
        {% set resultsNum = resultsNum+1 %}
        <li><a href="{{ entry.url() }}">{{ entry.title }}</a></li>
    {% endif %}
{% endif %}
{% endfor %}
{% endfor %}
</ul>
{% endset %}
| improve this answer | |
  • Thank you so much. It's not entirely working for me yet. When I test out the string with {{ myString }} plainly on the page, it renders the same HTML. But passing it in the array, it goes awry. But when I copy the HTML from inspector and examine it, it contains some kind of hard enters? If that's a thing. What I see is couple of screen height empty pages and some spaces at the end. I just enclosed what I had with {% set infoWindowHTML %} and {% endset %} (so no copy/paste done) what could be causing these enters to be added? (Not talking about <br> just type enters of some sort) thanks again! – Kitsune Apr 24 '15 at 23:15
  • 1
    Maybe try Twig's escape filter ('e' for short). {{ myString|e('html') }} (html is the default I believe). – Douglas McDonald Apr 25 '15 at 1:06
  • Figured it out by using string | replace({"\n":"<br>"}) Thanks! – Kitsune Apr 25 '15 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.