14

To encourage input, this is a community wiki post. Any user with at least 100 reputation points may contribute. Functions, filters, objects, macros and statements – when to use which A function is one of the simplest constructs in Twig – it takes one or more inputs and returns a single output: {{ round(42.9) }} will output 43 These are best used for ...


9

Go to https://pluginfactory.io/ to generate a boilerplate Craft plugin. API Version: Craft CMS version 2.5.x PluginName: Twig Session Description: Anything Version: 1.0.0 Plugin Author: Anything Author URL: Anything Github Name: Blank Uncheck: Tweet it out Check: Variables Click: Build Plugin Download twigsession.zip and unzip it. Place the unzipped ...


7

There are several things going on here. This will be a general answer that can hopefully help you move forward. This example will use Sprout Active as a reference and you can see the code here for an example of a plugin that implements multiple, simple Twig Extensions. Define your Twig Extension class and Namespace In Craft 3, you can register a Twig ...


4

You'll want to split on the line breaks, then loop through the resulting array... {% for ingredient in listOfIngredients|trim|split('\n') %} <li>{{ ingredient }}</li> {% endfor %}


4

You need to make sure that you're returning the raw version of the HTML. In Craft 2, it looks like this... return TemplateHelper::getRaw($result); In Craft 3, it looks like this... return Template::raw($result); If you're doing this in Craft 3, you need to also be sure that you are loading the Template helper class at the top: use craft\helpers\...


4

You can do it without JavaScript (if you don't mind the page refreshing) by accessing the request variable: https://docs.craftcms.com/api/v3/craft-web-twig-variables-request.html#public-methods {{ craft.app.request.getParam('foo') }} {# Should be the value of the input named 'foo' #}


4

.all() returns a collection of elements, which you can loop through with a for loop. .one() returns a single element, so you cannot use a for loop to access it. In this case you should just store the entry like: {% set entry = craft.entries.section('thing').one() %} and then access {{ entry.title }} afterwards. (Also keep in mind that entry is ...


3

Twig (at least within the context of Craft) doesn't work this way I'm afraid. The Twig template is processed on the server and HTML is returned to the browser. You're trying to change the Twig based on an event in the Browser, but that at that time, there is no longer Twig, just HTML. So your solution will need to involve Javascript to change the HTML/DOM ...


3

In Craft 3 it is actually possible. {{ craft.app.view.evaluateDynamicContent('phpinfo();') }} See: Can we use PHP in our templates instead of Twig?


3

Actually, you had your code just right imo: use craft\elements\Asset; ... if (!$asset instanceof Asset) { return false; }


3

Short answer: No. Longer answer: Sorry, no... but there's a good reason for that. Twig is a template language. PHP is business logic. In proper application design, you separate the business logic from its presentation typically in some sort of MVC structure. This sounds complicated, but it really makes your website/application a lot easier to maintain over ...


3

Thanks Brad, that's what I was after. In the case of what I was trying to achieve; the following in my plugins init function did the job: $twig = craft()->templates->getTwig(); $twig->addTest(new \Twig_SimpleTest('array', function ($value) { return is_array($value); })); I always struggle with syntax, so hopefully this example will help others. ...


3

When you call Craft's TemplatesService->getTwig() (craft()->templates->getTwig()), you get an instance of the Twig environment and from there you could be able to call addTest. 100% untested, but something like this should work: $twig = craft()->templates->getTwig(); $twig->addTest(new My_Twig_SimpleTest());


2

Assuming PluginCustomClass is a class inside of a Craft plugin, then the issue is probably that you need to reference it in the Craft namespace. Something like this should work: ->raw(') = new \Craft\PluginCustomClass(') Depending on what type of class that is (Service, Variable, Helper, standalone, etc.), Craft might not have auto-loaded it, so you ...


2

Raw doesn't close tags tags for you, it just allows them to be processed rather than printed only. The only reason you are seeing closing tags I'm guessing is because your inspector (chrome/safari) is adding them in. Do "view source" to see the real output. Your problem here is just rogue open tags


2

I am not sure if I am understanding your question correctly, so here is my understanding before I give my solution: You want to be able to access a field on an asset by it's field type rather than it's handle. Given this understanding, here is my solution: I have not been able to accomplish this using Craft relations. However, you can loop through the ...


2

Fixed by changing: class xyz extends \Twig_Extension { //... public function getFunctions() { return array( new \Twig_SimpleFunction('kex_cache_breaker', array(__CLASS__, 'cacheBreaker')) ); } //... } To: class xyz extends \Twig_Extension { //... public function getFunctions() { return ...


2

That is the correct way of doing it but you need to be sure that entry is a variable and set to an entry model at that point. And that you're in an appropriate part of your template in general for outputting the code. E.g. this works for analytics for example, placed at the end of my main structure block. {# ANALYTICS GOOGLE #} {% set js %} <!-- Google ...


2

Assuming you're using Craft 3, this should work: if (\get_class($asset) !== 'craft\elements\Asset) { return false; }


2

Yes, with the launch of Craft 3, Pixel and Tonic have provided some documentation on switching from Craft 2 syntax to Craft 3.


2

{# set test variable, to make your code work #} {% set block = {imageWidth: 300} %} {# the real output #} <img{{ block.imageWidth|length ? (' width="' ~ block.imageWidth~ 'px"')|raw }} /> Works for me


2

You could write a module that adds a twig extension to return the value of src from the string. That way you get to utilise PHP as per https://stackoverflow.com/questions/17017684/how-to-get-url-from-the-iframe-using-php to get the value. https://pluginfactory.io/ is a good starting point for adding a module to Craft. alternative very dirty approach: If ...


1

Where are you storing your mapElement template? - Unless it is stored in the same folder as the templates for the front end of your site, then Craft::$app->getView()->setTemplateMode(View::TEMPLATE_MODE_SITE); is telling craft to look in the wrong place. If you've got it stored in your plugin directory, then you don't need to change the template ...


1

Replace my if condition by if ( Craft::$app->request->getIsSiteRequest() || Craft::$app->getRequest()->getIsCpRequest() ) { Thanks to Luke Holder for pushing in the right direction!


1

All you should need to do is remove this condition check: if (Craft::$app->request->getIsSiteRequest()) { You are only registering the twig extension for front end (site) requests, but emails can be triggered both on a cart complete front end (site) requests AND a back-end (CP) order status changes.


1

The solution was to replace all references to "reference":null with "reference":"" in /craft/vendor/composer/installed.json.


1

I don’t believe there’s a way to create an actual Twig extension without making a plugin. However, depending on what you’re wanting your extension to do, would a Twig macro work? If you change your mind and decide to go the plugin route, creating a Twig extension that way isn’t too difficult.


1

As Andris Sevcenko pointed out, the problem is Craft 3 Beta 22 and a Multisite Configuration. Fixed in Beta 23.


1

Shame on me, but maybe still interesting to others who have this problem: I'm using vagrant and the path of my extension was NOT mounted to the vagrant box. That's all. So it simply could not find the files. Maybe a warning by craft would be nice, that it can not load the plugin files Though I do not understand, why it found the composer.json to read the ...


1

When you use the Twig curly brace {}, key/value pair syntax like you've got imagerConfig set to, you're using the Twig hash literal. In Twig, if you want to add to an array or hash after it has already been created, you can use the merge filter. Something like this should do what you're looking for: {% set imagerConfig = { jpegQuality: 80, ...


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