Hot answers tagged

16

Add the following macro to your template or import it with a separate twig file. The script allows the start and end date to be the same and outputs a single date instead of a timeframe. Example: 2014-06-13 to 2014-06-13 yields 13 June 2014 The macro: {% macro timeframe(dateStart, dateEnd) %} {# Output day of the start date if necessary #} {{ ...


15

Brandon commented on getting tag usage count (and how it can be expensive) in this Google+ post and again in this SE question where I was trying to think through a plugin. So to completely steal from that, you could loop through your tags and get a count for each... {% set tags = craft.tags.find() %} {% for tag in tags %} {% set count = craft.entries....


14

Marion Newlevant had a clever, efficient idea in this Straight Up Craft Hangout if you don't mind having a slight lag (like a day) for your tag counts to be up to date. That idea: store the counts you need in a Preparse field on each tag, and set up a cron job to re-save all tags periodically. 1. Set Up Preparse Preparse is a free plugin from André Elvan ...


12

Craft comes with a currency filter that makes this pretty easy – you don’t even need the £: {{ entry.priceFrom|currency('GBP', true) }} The 'GBP' establishes the currency (so the price is prefixed with £), and passing true to the second argument tells Craft to strip out the cents from the formatted string if there aren’t any (so £100 instead of £...


12

Set the Nested Entries URL format to: {parent.uri}/{slug}


10

How about just using the getHeight() and getWidth() methods available on the AssetFileModel? Something like this: {% set myImage = entry.imagesField.first %} {% if myImage.getWidth() < myImage.getHeight() %} {# portrait #} {% elseif myImage.getWidth() > myImage.getHeight() %} {# landscape #} {% else %} {# square #} {% endif %}


9

This should do it: {% set bytes = asset.size %} {% set kiloBytes = (bytes / 1024)|round(2) %} {% set megaBytes = (kiloBytes / 1024)|round(2) %} bytes = {{ bytes }} kiloBytes = {{ kiloBytes }} megaBytes = {{ megaBytes }} You can look at Twig's number_format filter if you want to get fancy with how to display the numbers.


7

Your example code attempts to create a new Redactor instance, which (as Brad says) won't work inside the control panel (where Craft has created the Redactor instances already). You can, however, use the Redactor config files to add an element with the classname .note. To do that, add the following to the file config/redactor/Standard.json (or if you ...


5

Instead of: {{ entry.postDate.format('d M Y') }} Use: {{ entry.postDate | date('d M Y') }} That should work.


5

You now can use the filesize filter: https://docs.craftcms.com/v3/dev/filters.html#filesize. This will make the file size readable, and the unit will be selected automatically.


4

It looks as though Craft 3’s built-in duration Twig filter might help: Posted {{ entry.myDateField.diff(now) | duration }} ago. With a bit of additional logic, you can make this more useful, after an Entry leaves a specified window: {% set dateOneYearAgo = now.modify('- 1 year') %} {% set dateOneMonthAgo = now.modify('- 1 month') %} {% set dateOneDayAgo = ...


3

Though I don't know the Element API, have you tried return $allOverChallenges; instead of return [ $allOverChallenges ]; It looks like you are simply adding these brackets (Array) on the return.


3

Chiming in with a solution made from reading the above solution and comments. I also wrapped it in a macro. {% macro timeAgo(entry) %} {% set dateOneHourAgo = now.modify('- 1 hour') %} {% set elapsedMinutes = entry.myDateField.diff(now).m %} {% if not (entry.myDateField < dateOneHourAgo) %} {{ entry.myDateField.diff(now) | ...


3

How about something like: &pound;{{ 1000.00 | number_format(2,'.',',') | replace({".00" : ""}) }} gives: £1,000 Got a similar idea from How would I omit zero value decimals in a number format?.


3

If your Redactor config included a "formatting" value in the "buttons" array, you need to rename that to "format". https://craftcms.com/help/redactor-ii-configs


2

I'm not sure this is possible in Twig alone, but it should be easy enough in PHP. To do that, you could write a Craft plugin that adds a custom Twig extension that has a ScormIt filter that uses PHP's DateInterval class to output it in the format that you're looking for. That way, you could do something like: {{ videoObject.duration|ScormIt }}


2

Just for future reference, Craft 3 is a little different with the currency filter. {{ entry.price|currency('CAD', stripZeros = true) }}


2

You could refactor it using ternary syntax and see if it fits. Otherwise you could also do it with a plugin and listen to beforeSaveEntry events.


2

The Slugify plugin has a Twig filter that should do what you're looking for.


2

What about using a table field with a column for HH/MM/SS, with each one restricted to numbers? Alternatively you could use https://github.com/fruitstudios/craft-validateit with a custom regex rule, but I've no idea what that would need to be.


2

The link you referenced is assuming that you've got full-control over the output/HTML of the page and you've already got an instance of Redactor up-and-running. In the context of Craft, the only place you have full control over is the front-end of your site, not the control panel. You certainly can include the HTML/CSS/JS to load Redactor on the front-end ...


2

Assuming you want the first category that's selected in the field, you'd set your "Entry URL Format" setting to: {categoryFieldHandle.first().slug} You can access any of your custom fields by their handle in a similar way.


1

Setup a dynamic route that points to the same template that you have set up in the section settings for your "slug URLs". Use a pattern like so: news/<id>, which passes the 2rd URL segment as a variable named id to the template. Add this snippet to the top of your template. {% if not entry and id %} {% set entry = craft.entries.id(id).first() %} {...


1

Well I've got this working with: {% set decimal = entry.priceFrom|reverse|slice(1,2) %} {% if decimal == "0" %}{{ entry.priceFrom|number_format(0,'.',',') }}{% else %}{{ entry.priceFrom|number_format(2,'.',',') }}{% endif %} Basically I'm reversing the string so I can check for a zero at the end (or not). It's fairly clean, but if anyone can improve on it ...


1

One thing that might help is to use the Ternary Operator described here: http://twig.sensiolabs.org/doc/templates.html#other-operators Example: {{object.mfgCategory.first.slug=="1hack"?'hack':mfgCategory.first.slug}} instead of: {%if object.mfgCategory.first.slug=="1hack"%}hack{%else%}{mfgCategory.first.slug}{%endif%} It's slightly shorter so might ...


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