Hot answers tagged

22

There are multiple ways to solve this: Native: Twigs url_encode filter: {{ "apples and carrots"|url_encode }} {# outputs "apples%20and%20carrots" #} Twigs replace filter: {{ "apples and carrots"|replace(' ', '-') }} {# outputs "apples-and-carrots" #} Plugins: Twig PCRE Filters - Plugin: {{ "apples and carrots"|preg_replace('/ /', '-') }} {# outputs "...


17

There's a couple of special variables available while for looping through an array. In your case I'd make use of loop.last. {{ tag }}{{ not loop.last ? ', ' }} This is the expression using the ternary operator to keep the syntax short.


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


12

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 has a filter for "Kebab Case", which I think is what you need. It strips out punctuation and replaces spaces with dashes. Useful if you need to format a field as a HTML attribute. {{ tag.name|kebab }}


9

An alternative, if you are getting the tags from the Tag Field type, is the Join filter: {% if post.tags | length %} Tags: {{ post.tags | join(', ') }} {% endif %}


8

I'm not sure if this feature is newer than this question but you can also say {{ tag.slug }} to get apples-and-carrots. All letters become lowercase as well so the result is more "scripting-friendly".


7

This is based on Matt's answer to your previous question. Likewise this should better be used within cache tags! This is untested, but it hopefully works out for you. . Edit: This is still untested but it came to my ears, that it could make problems to assign variables as array keys. That's why I changed the code to use objects instead. If anyone uses ...


6

To list all tags on a particular entry use: {% for tag in entry.tagField %} {{ tag }} {% endfor %} If you only want the first tag you can just use: {{ entry.tagField.first }}


6

Here's a really DRY way to do it. Setup a route, something like "news/tags/(tag)" to load the "news/index" template (the same one used for listing all entries). In the "news/index" template: {# If there is a tag token in the URL, set a filtered variable. #} {% if tag is defined %} <h2>Tag: {{ tag }}</h2> {% set tag = craft.tags.name(...


6

Currently it’s not possible to delete tags (or otherwise manage them).


6

Brandon at P&T was able to fill in the piece I was missing. He said: To only get tags that were related from a specific locale, you just need to specify that in your relatedTo param: {% set tags = craft.tags.relatedTo({ sourceElement: entries, sourceLocale: craft.locale }) %} With the addition of order('dateCreated desc').limit(10) ...


6

Not very elegant, but... :) {% for tag in tags %} {{ tag.type }}{{ loop.index < loop.length-1 ? ', ' : loop.index < loop.length ? ' and' }} {% endfor %}


6

You can do this with the twigPCRE filter. Once you have your allTags, group them like this: {% set allTagsGrouped = allTags|group('name|first|upper|preg_replace("/[A-M]/", "A-M", 1)|preg_replace("/[N-Z]/", "N-Z", 1)') %} This takes the tag name, gets the first character, converts it to uppercase (so 'a' and 'A' are grouped together), and then maps all ...


6

Edited I just learned this, but as of Craft 2.4, there's a kebab filter which you can use here. This would probably give you the effect you're after, where the string "two words" would become "two-words", and punctuation characters are dropped by default. You could use this like so: <li><a href="{{ siteUrl }}tags/{{ tag.title | kebab }}">{{ ...


6

A Categories field can be related to several categories. So you'll either need to limit your Category field to a single Category relationship or you'll need to make some assumption in how you build your URL as to which category will appear in the URL. The simplest assumption to make will be that you will always return the first category for your URL. Entry ...


6

There currently isn't any Tag management in Craft, but if you think there should be, consider voting for this feature request. In the meantime, there is a 3rd party plugin called Tag Manager that will let you delete tags, although it won't show you which ones aren't in use. There is a help article that shows how to select all of the tags that are in use. ...


6

The Tags field classifiedTags needs an array of tag IDs, not slugs/titles. Also, if this is an existing entry and you want to retain tags already added to the field, you'll need to pull the existing tag IDs using $entry->classifiedTags->ids() and merge with those. $tag = craft()->elements->getCriteria(ElementType::Tag, [ 'groupId' => 1, ...


5

Before worrying about how templating should work, let’s just get one thing out of the way: Computing this data every time it’s requested is horribly inefficient, and could easily take a site down. You’ll absolutely need to find a way to store the tag counts in a more permanent location, and calculate it when entries are saved rather than when the template ...


5

Try this: {% for block in availability[foo] %} The square brackets let you access a variable variable name on an object. As a general rule you never put {{ }} inside of {% %} tags in twig.


5

As of Craft 2.0 you can rename tags by double-clicking on them.


5

This should work: {% set tags = entry.q_tags %} {% set entries = craft.entries.relatedTo(tags).order('score').find() %}


5

I think it'll be easier and more efficient to achieve that if you use the loop functionality instead of querying the actual tags. I think this code does what you want: {% set topics = '' %} {% for topic in entry.topics %} {% set topics = topics ~ topic ~ (loop.index < loop.length-1 ? ', ' : loop.index < loop.length ? ' and ') %} {% endfor %} <...


5

Definitely use a custom Element Type.


5

In your working example, you’re mapping request.tags to the search param, but in the non-working example, you’re mapping it to the slug param. Those two params are handled in very different ways. search will do some normalization routines on the value, and perform a very loose search for those keywords (see https://craftcms.com/docs/searching for more ...


5

You'd need to get the tag model first. {% set tag = craft.tags.search('My Tag').first() %} {% if tag %} {% set entries = craft.entries.relatedTo(tag) %} {% endif %} Replace search with whatever other information you have from the tag, to get more precise results, e.g., slug, id properties.


5

If you don’t have access to another admin account, you could unlock the account directly in the database. Change the lock value to 0 for the account in the craft_users table.


5

You told Craft to look for tags/_tag And then you created templates/tags/_tags.html Note you have no s on the end in the route definition, but you do in the template name.


4

Please make sure the tag settings in CraftImportService.php are set properly.


4

Your code returns an TagModel and saves it as "tag". You can now get all model properties like this: {{ tag.property_name }} In your case it would look like this: {% if tag.name == 'Flower' %} Do stuff {% endif %}


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