I want to allow people to append to a varaible with capture or assign. Here is a syntax proposal. It was trivial to implement and it works with all new capture and assign modifications (post condition, ternary, etc).
The main problem is which operator should I use. +=
or >>
.
{% assign foo = 'yo' %} // "yo"
{% assign foo += ' man' %} // "yo man"
{% capture foo += %} what's up{% endcapture %} // "yo man what's up"
{% assign foo = nil %} // nil
or
{% assign foo = 'yo' %} // "yo"
{% assign foo << ' man' %} // "yo man"
{% capture foo << %} what's up{% endcapture %} // "yo man what's up"
{% assign foo = nil %} // nil
I like <<
better, but +=
might be more familiar to people. A downside of <<
is that it throws off syntax highlighters a bit.
I should note that if a variable isn't defined, this will fall back to a simple assignment so there's no need for users to worry about that.
I'm releasing a plugin for some kind of follow button. I've created a partial in my includes which contains the button code, but I want to inject the script at the bottom of the page. Here's what it could look like.
<a href="https://alpha.app.net/imathis/" rel='me'>follow @imathis</a>
{% capture post_scripts += %}
<script type="text/javascript">
...
</script>
{% endcapture %}
Then themes can support post script injection like this.
</footer>
</div>
{{ post_scripts }}
</body>
When a user includes the partial the script injection should just work.
{% include plugins/adn/follow.html %}
Of course users could also make use of this feature when installing scripts manually.
I like this a lot!
One concern: how will scope be dealt with? Are all variables global?