Questions tagged [twigextension]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1 vote
1 answer
60 views

How to use twig function inside another module or plugin?

How to use twig function extensions inside another module or plugin? Say I created filterCats() twig function and it is already available in twig Is it possible to use it inside a module or plugin? ...
  • 63
1 vote
1 answer
106 views

Uppercase certain words in string

We have some content that contains words that should be presented in uppercase - the content comes via an API and is in all uppercase, we change it to title case to make it presentable. I had started ...
  • 147
-1 votes
1 answer
85 views

how to create a custom plugin if the user enables plugin settings then it will replace the website home page to my custom design? [duplicate]

I am trying to create a plugin if the user enables plugin settings then it will replace the homepage of the website with my custom designed template
  • 23
1 vote
1 answer
288 views

How to display controller response on twig template?

In the plugin, I created a form and after the process, return the below response from the controller and redirect to the template. I am trying to print the response to the template but it's not ...
1 vote
2 answers
2k views

Twig json_decode gives Syntax Error

I'm trying to use the json_decode twig filter from Craft to decode an array of entry objects that got encoded in a macro. In the macro, the array of entry objects gets encoded by json_encode and ...
  • 305
2 votes
1 answer
741 views

.first() vs. .one() in craft 3

I'm confused about the new .one() argument in craft 3 If I want to grab all entries I write: {% for entry in craft.entries.section('thing').all() %} {{ entry.title }} {% endfor %} Alls is nice ...
  • 3,734
1 vote
1 answer
530 views

Craft 3 CMS How to get all Channel Names/Titles

I am trying to retrieve all Channel sections' names/titles. This snippet contains the troublesome part. {% set allSections = craft.entries.all() %} {% for section in allSections %} {% if ...
  • 107
1 vote
1 answer
492 views

Custom field plugin: Unable to find template / General custom field plugin information

I am still relatively new to Craft CMS. I am trying to make a custom field plugin for Craft 3 but I am struggling to understand why I can't render my form template. The fieldtype should render a map ...
  • 107
4 votes
2 answers
4k views

How to access HTML input values from Twig and vice versa

I am new to Craft and Twig in general. I have encountered the following problems and struggling to find a solution for hours. I want to access the value of "foo" input field inside the twig ...
  • 107
1 vote
1 answer
177 views

Output raw html from if statement and combining strings?

I have the following code: <img{{ block.imageWidth|length ? ' width="' ~ block.imageWidth ~ 'px"' }} /> If imageWidth is specified as 120, it should output: <img width="120px" /> ...
  • 269
1 vote
1 answer
2k views

How to strip out src="" value with twig from iframe code

I have have a text field for copy-pasting an iFrame code. <iframe src="https://webpagelink.com/l/3556456/2019-03-06/34576" width="100%" height="500" type="text/html" frameborder="0" ...
1 vote
2 answers
94 views

TwigExtension added to Craft don’t work in emails

I added tomodomo/twig-pluralize-extension to my project composer, loaded with a custom module like that : if (Craft::$app->request->getIsSiteRequest()) { // Add in our Twig extension $...
1 vote
1 answer
718 views

PHP eval filter for Twig

If I am confident in the users of our webpage that they will never be messing with the Twig templates, then what risk is there to creating a |php filter for Twig that just does this? {% set text = "...
  • 1,293
1 vote
0 answers
27 views

Recursive relations – how to build list properly?

I started building a list of recursive relations: We're in a project, we want to check interviews related to that project, see which persons participated in those related interviews, and then build ...
  • 599
1 vote
1 answer
76 views

UnexpectedValueException after loading plugin

I'm attempting my first Craft plugin and trying to update https://github.com/matthewattanasio/Craft-Url-Decoder-Twig-Extension for Craft 3 and followed the instructions in the docs for loading plugins ...
  • 563
2 votes
2 answers
50 views

How can I validate that I am passing an Asset into my custom twig filter?

I'm creating a custom twig filter in Craft 3 and am struggling with a really simple thing. {{ entry.backgroundImage.one() | filter }} In my filter, I would like to validate that I am being passed an ...
  • 123
2 votes
1 answer
395 views

Craft 3 - Fixing deprecation errors

I'm used to writing Twig 1 syntax, and since switching to Craft 3 (which uses Twig 2) I keep getting lots of deprecation errors in the control panel. Is there any reference material that provides the ...
  • 3,604
3 votes
1 answer
331 views

Craft or Twig converting html tags to alt characters

Just starting with plugin development. I modified the code from pluginfactory.io for a custom tag: public function navHTML() { $result = '<h2>Hello again</h2>'; return $result; } ...
2 votes
1 answer
829 views

Register multiple TwigExtensions in one plugin in Craft 3

In my plugins init() function I have this: Craft::$app->view->registerTwigExtension(new TestTwigExtension()); If I duplicate that and change the Twig Extension class name, I just get an error:...
  • 2,315
1 vote
2 answers
700 views

Is there any way to create twig extension without a plugin?

I want to create custom twig extension without any plugin dependency. Is anyone has solution for this?
2 votes
1 answer
432 views

How to get a list item <li> using a plain text field every time I hit enter

I'm trying to get a list from a simple plain text field which will accept line breaks. I need to find a way to wrap an li tag around the content on that line. So everytime I hit enter, it wraps <li&...
1 vote
1 answer
265 views

Craft 3 Plugin: Saved image asset has no width / height

In my Craft 3 Plugin I am saving an image (YouTube poster) to an asset volume like this: $asset = new Asset(); $asset->title = $url; $asset->tempFilePath = $tmpPath; // temp path for image in /...
1 vote
1 answer
1k views

Create Craft 3 Plugin - Call to a member function getVersion() on null

I'm trying to write my own Craft 3 TWIG filter / functions. For this purpose I created a scaffolding using pluginfactory.io. I did a composer require and composer correctly symlinks the plugin to the ...
1 vote
1 answer
1k views

How do I add a new item or object to an existing object?

I'm using the Imager plugin and Craft for image compression. Currently, I have something like this: {% set imagerConfig = { jpegQuality: 80, pngCompressionLevel: 4 } %} In another part of my ...
1 vote
1 answer
482 views

Cannot install custom Twig extension plugin

So I started building this plugin to add a twig extension to escape but leave &nbsp; intact. For some reason when I try install it or use it without the force argument on the import command in the ...
1 vote
1 answer
211 views

Unable to load the "Craft\xyz" runtime after Craft update

After updating from Craft CMS 2.6.2973 to Craft CMS 2.6.2981 I get the following error message: "Unable to load the "Craft\xyz" runtime after Craft update" "Craft\xyz" is a Twig extension Fails in [...
13 votes
1 answer
2k views

The Definitive Guide to Extending Twig in Craft CMS

So Craft's implementation of Twig is already very powerful and suitable to most common tasks. The Craft Docs already provide a great intro into using Twig and the handy extensions provided by Craft: ...
1 vote
1 answer
143 views

How to retrieve the mode of an image transformation from its name?

I work on a portfolio with a large number of images with very high sizes and a lot of images transforms. The server on which this site is hosted reach these limits (images generations process are ...
1 vote
1 answer
1k views

How do I print a value from Craft/Twig into a Javascript function?

I just want to print a value from Craft into a JavaScript function to filter a dashboard. The function is this: showOnly(filterName, values); I tried this: showOnly('Short Names','{{ entry.title ...
  • 911
3 votes
2 answers
295 views

Is it possible to extend twig with 'Twig_SimpleTest'?

I was on the Twig StackExchange forum and found an answer that suited my needs. But I can't for the life of me figure out how to implement this into a Craft CMS Plugin. I'm familiar with ...
  • 2,315
5 votes
1 answer
2k views

Creating sessions in Twig

I am trying to create the PHP $_SESSION equivalent in Craft CMS using Twig. I have searched very long and I can't find how you can create sessions in Twig. In previous posts I have found that I ...
  • 495
4 votes
1 answer
299 views

Is is possible to add a bespoke Twig template loader?

Twig lets you provide alternate (or multiple) template loaders. I'd like to take advantage of this to do use a bespoke loader to pull in components from a pattern library using a shorthand syntax like ...
0 votes
1 answer
172 views

Path to plugin class from a Twig_Node

In my custom Twig tag I am trying to reference a class in the Twig_Node compile method, however I receive an error every time stating it can't find it. What would the path be to a custom plugin class ...
  • 2,797
1 vote
1 answer
681 views

Craft rich text field excerpt with slice and raw breaks when cut inside double tags

I got a rich text field. I made an excerpt with striptags slice and raw twig functions. It works fine most of the time (raw close tags when slice cuts inside a <p>). <div>{{ textBlock....
1 vote
1 answer
98 views

Is it possible to target a fieldtype by it's Type name, rather than the handle name?

I've created an Asset Source and assigned my own field via the field layout. Then, I created an Assets Field to display a single image on a page. Using my plugins twig extension I can query the ...
  • 2,315