Hot answers tagged

18

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': var}) %} {{ string }} {# Prints "I like kiwis" #} Your example: {% set rgb = "Red; Green; Blue" %} {% set rgb = rgb|replace({';': ','}) %} {{ rgb }} {# ...


18

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 string in double quotes: {% set string = "There is a #{car}." %}


18

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://buildwithcraft.com/help/static-translations for details on how you can use the |t filter to translate your text.)


17

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 an #{apple}." %} Using string concatenation (assuming that apple="apple") {% set string = "There is an " ~ apple %}


15

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 modify the capitalization style of your strings. Of course there is the lowercase equivalent lower and additionally there are some Craft exclusive filters ...


12

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|...


10

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.


8

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.


7

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, some friendly advice: I'd like to point out that rewriting HTML in this way is pretty risky. Shameless plug alert, but I wrote a plugin called Retcon HTML ...


5

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 guaranteed that it will be available, and if it is, some versions of it have a bug that could put it into an infinite loop in certain circumstances. So Craft’s ...


4

Give this a shot: {% set cacheTime = sidebarBlock.twitterFeedCacheTime.value %}


4

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:{{ globalPhoneNumber.phoneNumber|replace('/[^0-9]/', '') }}">{{ globalPhoneNumber.phoneNumber }}</a>


4

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-) is required, and "-" are allowable, the replace string for "(555) 555-5555" would be: {{ global.phone|replace({"(" : "1-", ") " : "-"}) }}


3

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://craftcookbook.net/recipes/324


3

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 AttributeType::Mixed. All other attribute types will be returned directly as the database returned them (as a string). However, I agree with you that we ...


2

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


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" %...


2

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 redundancies. If the whole category is called Stages or Key Stages, calling each category Key Stage 1, Key Stage 2 etc is redundant. If you instead call your ...


1

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 specific example, the site url is probably not necessary (starting a URL with / will link to the current domain just fine) - so you would not actually need twig. ...


1

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|json_encode }} corrects spits out an array of numbers.


1

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=NAME {% set query = {} %} {% set categoryParam = craft.app.request.getParam('cat') %} {% set authorParam = craft.app.request.getParam('author') %} {% set ...


1

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. {{ block.paragraph | retconAttr('a', { href : '?some_text' ~ entry.title|kebab }, false) }} By including false, it adds to the already existing href, rather ...


1

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 language. For that reason, there is a customAsciiCharMappings config setting, which you can use as a workaround.


1

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&...


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