2022 Developer Survey is open! Take survey.

Questions tagged [twigextension]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1 vote
1 answer
38 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 ...
user avatar
  • 95
-1 votes
1 answer
76 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
user avatar
  • 13
0 votes
0 answers
17 views

Custom GraphQL Resolver that utilizes the same craft function used in twig

I'm pretty new to Craft CMS or PHP for that matter. I have custom code that unfortunately can't be done with just GraphQL so I need to utilize the same functions used in a twig template, in a custom ...
user avatar
1 vote
1 answer
67 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 ...
user avatar
1 vote
2 answers
790 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 ...
user avatar
  • 195
2 votes
1 answer
526 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 ...
user avatar
  • 3,664
1 vote
1 answer
332 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 ...
user avatar
  • 107
1 vote
1 answer
418 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 ...
user avatar
  • 107
4 votes
2 answers
3k 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 ...
user avatar
  • 107
1 vote
1 answer
156 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" /> ...
user avatar
  • 207
1 vote
1 answer
1k 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" ...
user avatar
1 vote
2 answers
92 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 $...
user avatar
1 vote
1 answer
573 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 = "...
user avatar
  • 1,263
1 vote
0 answers
26 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 ...
user avatar
  • 589
1 vote
1 answer
68 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 ...
user avatar
  • 499
2 votes
2 answers
49 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 ...
user avatar
  • 123
2 votes
1 answer
382 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 ...
user avatar
  • 3,594
3 votes
1 answer
319 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; } ...
user avatar
2 votes
1 answer
765 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:...
user avatar
  • 2,265
1 vote
2 answers
671 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?
user avatar
2 votes
1 answer
419 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&...
user avatar
1 vote
1 answer
246 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 /...
user avatar
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 ...
user avatar
1 vote
1 answer
857 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 ...
user avatar
1 vote
1 answer
414 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 ...
user avatar
1 vote
1 answer
201 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 [...
user avatar
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: ...
user avatar
1 vote
1 answer
138 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 ...
user avatar
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 ...
user avatar
  • 891
3 votes
2 answers
269 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 ...
user avatar
  • 2,265
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 ...
user avatar
  • 485
4 votes
1 answer
290 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 ...
user avatar
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 ...
user avatar
  • 2,707
1 vote
1 answer
668 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....
user avatar
  • 972
1 vote
1 answer
96 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 ...
user avatar
  • 2,265