{% capture myvar %} is capturing unwanted extra characters

Hello all,
please endure with me, a newbie, labouring on a simple blog since some weeks…
After searching literally for hours (which does not necessarily mean much), the last resort was to open up an account here.

All I want is a clear / clean copy of a net page.name like for instance ‘dummy’ instead of ‘dummy.html’ to be able to check for identity of vars ‘==’

Now, for a page named ‘dummy.html’, aaa{{page.name}}aaa will output ‘aaadummy.htmlaaa’, which is fine.

aaa{{ page.name | remove: “.html” }}aaa will output ‘aaadummyaaa’ - even finer :smile:

To get this into a variable, using

{% capture netPageName %}
  {{ page.name | remove: ".html" }}
{% endcapture %}

I get 'aaa dummy aaa' and found no way to remove the white space.

Certainly, I am doing something - or more - things wrong, but what is it?

Very happy for some help!
Greets UliJT

Hello mods, please move this post if it is placed in an unsuitable category - did not pay attention, sorry for that)

How about you append and prepend?

{{ page.name | remove: ".html" | prepend: “aaa” | append: “aaa” }}

Hello Bill,
thanks for your instant reply!
I am afraid this does not help though, the ‘aaa’ only were a quick and dirty way to see whats the exact content of netPageName.
For comparison purposes, I do not want any spaces or whatever appended or prepended to ‘dummy’ - in this example. .
The var netPageName ought to contain only the characters: ‘dummy’ (without the quotation marks).

The above capture includes several newlines and spaces (marked with X’s above), so those are included in the captured variable. If you remove the whitespace, then you should get the result you want (untested code):

{% capture netPageName %}{{ page.name | remove: ".html" }}{% endcapture %}

Another alternative is to use Liquid’s whitespace controls to force capture to strip leading/trailing the whitespace.

However, rather then using capture, you might consider using an assign statement, which is simpler and avoids whitespace issues:

{% assign netPageName = page.name | remove: ".html" %}
1 Like

Great, this is it - thanks a lot!
Yes, whitespace can be and is an everlasting issue…

After some testing:
Whatever whitespace the user is entering before or after
the expression to be ‘captured’ (without deliberately coding these spaces), is resulting in exactly one (space-bar) whitespace. (There may be more to it though, not tested.)

For which purpose this needs to be the case is another matter.

Thanks and regards
( Edit: yes, assign is much better in this case)

I think what you are looking for is the strip filter.

From you first example, the following should work :

{% capture netPageName %}
  {{ page.name | remove: ".html" }}
{% endcapture %}

Thank you!
In this case, I am preferring a solution which avoids the creation of additional whitespace in the first place.