2022 Developer Survey is open! Take survey.

Hot answers tagged

20 votes
Accepted

How to replace characters in string

Twig has a replace filter for that. You can replace strings/vars with strings/vars: {% set string = "I love apples" %} {% set var = "like" %} {% set string = string|replace({'apples': 'kiwis', 'love'...
user avatar
  • 8,316
18 votes
Accepted

What's the difference between using single and double quotes in strings?

There is a small difference, single-quoted strings don't support interpolation: {% set string = 'There is a #{car}.' %} The above example won't work. If you want to use it, you need to wrap your ...
user avatar
  • 2,332
18 votes
Accepted

How can I incorporate a variable into a string?

If you‘ve got a multi-lingual site (or even if you don’t), you can pass variables into strings with the |t filter: {{ "Welcome back, {name}"|t( {'name': currentUser.name} ) }} (See http://...
user avatar
17 votes

How can I incorporate a variable into a string?

I know three ways: Using format() (look for type specifiers) {% set string = "There is an %s."|format("apple") %} Using string interpolation (assuming that apple="apple") {% set string = "There is ...
user avatar
  • 2,332
15 votes
Accepted

Is it possible to convert string to uppercase?

The upper filter is what you are looking for: {% set var = 'Hello World' %} {# Print in UPPERCASE #} {{ var|upper }} {# 'HELLO WORLD' #} Besides upper there are also some other filters available to ...
user avatar
  • 8,316
13 votes
Accepted

How can I convert text into a URL-friendly string?

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 ...
user avatar
10 votes
Accepted

What's that |t - filter for?

It's the short syntax for the translate filter. Set up a translation file as described in the docs and the string you apply that filter to is translated according to it.
user avatar
  • 35.8k
8 votes
Accepted

How to check how many characters there are in my string?

Twig has a length filter that can help you with that: {% set stringLength = "Simple String" | length %} Note that this also works on arrays to give you the number of items in the array.
user avatar
  • 2,301
7 votes
Accepted

Replace <p> with <p class="myClass">

Use the raw filter on the replaced string (output), not block.body (input): {% set str = block.body %} {{ str | replace('<p>', '<p class="myClass" data-wow-duration="1s">') | raw }} Also,...
user avatar
5 votes
Accepted

Craft.log showing Buggy iconv installed. Will fallback to mbstring

Iconv is a system C library that converts string encodings, and is generally preferable to PHP’s mb_convert_encoding() because it supports significantly more character encodings. However it’s not ...
user avatar
4 votes
Accepted

How can I convert a dropdown field value to an integer within a hash?

Give this a shot: {% set cacheTime = sidebarBlock.twitterFeedCacheTime.value %}
user avatar
  • 296
4 votes

Remove white space and parenthsis from global from entry variable

For what it's worth, you can strip out all non-numerics with this something like this: {{ globalPhoneNumber.phoneNumber|replace('/[^0-9]/', '') }} Or, as part of a tel: anchor: <a href="tel:{{ ...
user avatar
4 votes

Remove white space and parenthsis from global from entry variable

I haven't tried this but I think that you can use the twig replace function. Something like: {{ global.phone|replace({"(":"", ")":"", "-":"", " ":""}) }} Update In your case, since the prefix (1-) ...
user avatar
3 votes

How do I get model/record attributes in their proper types, and not just strings?

I ended up overriding BaseModel->setAttribute() and BaseRecord->prepAttributesForUse() in my plugin. Works well! Right now I'm just handling Bool and Number types. Posted this solution here: http://...
user avatar
  • 3,051
3 votes
Accepted

How do I get model/record attributes in their proper types, and not just strings?

Currently the only thing Craft does in BaseModel->setAttribute() and BaseRecord->prepAttributesForUse() is some normalization when the attribute in either case happens to be AttributeType::DateTime or ...
user avatar
  • 66.4k
2 votes

How to check how many characters there are in my string?

If you are attempting to get a count of the string, you can use Twigs dump function. {{ dump(myString) }}
user avatar
2 votes
Accepted

Twig result/contents into string

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 ...
user avatar
2 votes
Accepted

Remove duplicate text from query results

You might want to rethink your approach – any solution you build for this will break as soon as someone adds a new category that doesn't exactly match the naming pattern. In general, try to avoid ...
user avatar
  • 6,040
1 vote
Accepted

String interpolation in Redactor

Redactor fields are not meant to process twig both for security reasons (as they can be changed by any user) and for usability (you cannot expect authors to remember / preserve twig tags). In your ...
user avatar
  • 608
1 vote

Replace quotes in an array

So it seems that even although the Craft field is set to number it doesn't actually define a number. I changed the field from a regular Craft table field to a Super Table field and my {{ donutData|...
user avatar
  • 1,733
1 vote

How to display entries filtered from more than one category

Here's something I've got for filtering news by author and by category. You might be able to re-work it for your needs? From a page URL being: http://project.com/news?&cat=CATEGORY&author=...
user avatar
  • 1,032
1 vote
Accepted

Append a string to the end of all rich text links

I found a way to use the RetconHTML plugin for what I wanted. I just utilized the Attr filter and it worked the way I wanted. I had to also remove the way Retcon would add spaces to appended strings. ...
user avatar
  • 45
1 vote
Accepted

Normalizing Keywords Bug

This may be a bug, and is probably worth reporting to P&T. However, it's very difficult (if not impossible) for them to account for every potential ASCII character mapping for every possible ...
user avatar
  • 23.6k
1 vote

How can I convert text into a URL-friendly string?

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" ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible