If I want to convert text into a URL friendly string for sharing text in a social media link, how would I go about that?
2 Answers
I was about to ask this question, but realized that it's closely related to this question.
The answer, like in the above referenced question lies with Twig filters.
Specifically, in this case, one needs to use the url_encode
filter to convert the text needed.
Example
<a href="http://www.linkedin.com/shareArticle?mini=true&url={{ entry.url|url_encode }}&title={{ entry.title|url_encode }}&source=Xavier%20Creative%20House&summary={{ entry.question|url_encode|striptags }}" target="_blank">
In any case, I hope that helps others with this. I know how easy it is to forget that the answers sometimes like over in the Twig docs, and not Craft’s.
It was clearly documented on Twig Official Documentation that we can use url_encode
filter to convert it to url friendly string.
Example
{{ "path-seg*ment"|url_encode }}
{# outputs "path-seg%2Ament" #}
{{ "string with spaces"|url_encode }}
{# outputs "string%20with%20spaces" #}
{{ {'param': 'value', 'foo': 'bar'}|url_encode }}
{# outputs "param=value&foo=bar" #}