We’re rewarding the question askers & reputations are being recalculated! Read more.

Questions tagged [macro]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
0answers
36 views

What's the difference between `checkboxGroupField` and `checkboxSelectField` in Craft 3?

Short: What exactly is the checkboxGroupField used for? And why not just using the checkboxSelectField? Long: I am building a library that's gonna be required by a couple of my future Craft plugins ...
2
votes
1answer
82 views

Adding to default classes in a macro

I'm fairly new to using macros and haven't found anything online regarding this — sorry if this is a possible duplicate post. I've created a macro with 7 possible parameters but on different ...
1
vote
1answer
20 views

How to Update an If Rule Based on Different Parameters Passed to a Macro?

I have a macro that can take up to six parameters which serve to populate nav links. The parameters are basically grouped into twos, with someone needing to pass in both a link and a title to make a ...
2
votes
1answer
591 views

Craft 3 and Macros

re: macros I'm trying to use one for an SVG sprite. But I'm not sure what I'm doing wrong. macros template: {% macro icon(iconId) %} <svg class="icon icon-{{ iconId }}" role="img" title="{{ ...
2
votes
1answer
774 views

Just how do Macros work in Craft 3?

Here is what I am doing, but it's not working: In a template: {% import 'macros/global' as macros %} {{ macros sizedImage(image, alt, 't600X450', 600, 450, link, 'someClass') }} The I have a macro ...
1
vote
1answer
138 views

Templating - Get a string of the entry's field handle

I'm passing entry fields into a macro and I'd like to be able to get a string of the field handle myMacro(entry.field1) myMacro(entry.field2) {% macro myMacro(entry) %} {# I want a string of '...
1
vote
1answer
83 views

Macro for entry loop causing 'null' variable template error

I am new to the concept of Twig templates and I can’t get it right from the documentation … What I am trying to achieve is to have a reusable Blogroll snippet that outputs entries in different ...
1
vote
1answer
248 views

How Do I Add a rel=“canonical” HTTP Header to Embedded PDF Files?

I'm working on project where I would like to directly embed PDFs onto web pages. We'll be using Google's PDF viewer as the embed tool and implementing via a macro. Here is the macro as it stands now: ...
1
vote
1answer
241 views

Get all available asset transforms

Is there a way to get all available image transforms in a template? I would like to generate the srcset for responsive images programmatically. I thought of creating a macro for that but haven't ...
2
votes
2answers
521 views

How can I iterate through an array of field handles for an entry?

Background: One of my templates has up to 6 fields that should each go into the same markup. Here's an example with a courseDesign field: {% if entry.courseDesign|length %} <section class="col-...
1
vote
1answer
95 views

Dynamic Macro Variable

We're using the Digital Products plugin from Pixel and Tonic to sell 6 different types of products. I've set up a macro with matching icons for each of the product types (image, video, etc). On our ...
2
votes
1answer
175 views

Pass image transformations into macro

I have a very basic macro for a lazyloading responsive image tag: {% macro isotopeItem(imgParam) %} {% set thumb = { mode: 'fit', width: 100, quality: 60, } %} {% set tSM = { mode: 'fit', width: ...
0
votes
2answers
637 views

active state macro how to check if segment 2 is empty

I'm using a macro to check segment 1 to set an active state on my navigation and all is working correctly. However I would like to change it so that if segment_2 is not empty it doesn't display the ...
2
votes
1answer
385 views

Macros, Variables and Includes

Trying to get my head around this and make my code a little DRYer. I have a _helpers macro file. Like so: {% macro date_formatted(givenDate) %} {{ givenDate|date("l, jS F Y") }} {% endmacro %} {...
0
votes
1answer
342 views

Responsive Images Srcset and Sizes based on Image Transform

So I have setup a macro based on the excellent article here; https://straightupcraft.com/articles/responsive-images-with-twig-macros {% macro responsive(asset, options={}) %} {% import _self as self %...
12
votes
1answer
2k views

Macros vs Includes

Is there any guidance or pitfalls/benefits of using one over another? I see that includes can be done more or less the same way a Macro can be done (certainly for simple variable changes). An example ...
1
vote
2answers
268 views

Help with Macros

I have tried to setup the following Macro to reuse code for product teasers across my site. The macros looks like this; {% macro teaser(imgSize, className) %} <div class="product-teaser{% if ...
0
votes
0answers
110 views

Conditional to check position of previous matrix block

I'm referencing the Pixel & Tonic sample craft template Happy Lager where in the article body template there's a macro to get the position of a block. I'm trying to setup a conditional to check ...
2
votes
1answer
404 views

Using a macro for entries loop

I'm trying to use a macro to output a list of entries (and the title of a section), this is my code: {% macro sections(sectionName)%} {% set index = craft.entries.section('{{ sectionName }}Index'...
2
votes
2answers
343 views

Add / Update context from macro

I'm trying to update the context from within a macro. Is this possible at all? I'm pretty close with the below code. The var_dump within the below function shows the updated context. But when doing a ...
1
vote
1answer
128 views

How would I create a select element with the 14 days from today as options

I'm looking to create a select (dropdown) form field that includes the days and dates for the next 14 days, starting from today, as options. My initial thoughts on how to construct this would be to ...
4
votes
1answer
207 views

How can I add an option group to the select Macro from CraftCMS forms macros

I'm trying to list out a select element with option groups and options using the awesome for macro that Pixel and Tonic created. I have imported the macros and am calling the select one like so: {{ ...
1
vote
1answer
3k views

Pass a variable array to a macro

I try to pass a variable from to a macro: {% set syName = { x: { name: "Schön"}, y: { name: "Leicht bewölkt"}, z: { name: "Stark Bewölkt"}, } %} {% macro prognose(symbol, syName) %} ...
1
vote
1answer
404 views

Switch in a Macro

I'm getting the following error: Unexpected end of template. Twig was looking for the following tags "case", "default", or "endswitch" to close the "switch" block started at line 11) Here is the ...
8
votes
2answers
14k views

Use a Twig macro to set a variable

Is there any way to use a macro in twig to define a variable outside that macro? For example, say you want to use a macro to set a variable that stores an entry's parent (I know you can do this much ...
0
votes
2answers
341 views

How to render a macro from a plugin?

Im trying to render a site macro from my plugin, i’m getting the following error: Argument 1 passed to Twig_Template::display() must be of the type array, object given, called in /path/to/file.php on ...
0
votes
0answers
196 views

Macros vs Variables, which one should I choose to give users of my plugin?

For Market commerce, good example templates and snippets of code are very important. There is a few ways I can give these to the users of my plugin: 1) Example template folder. The user would ...
1
vote
2answers
203 views

Problem with scope in macro

I've written a short macro to get the first image inside a matrix field. {% macro firstImage(entry) %} {% set image = 'hello' %} {% if entry.sliderMatrix.total() %} {% set block = ...
0
votes
1answer
1k views

For performance is using a macro better than using include with the 'with' statement?

Which is better for performance? Using a macro for output in a for loop or calling an include using the 'with' params to pass data? Both are loading a file. But is stacking up a bunch of code in a ...
1
vote
1answer
156 views

What is the best way to format a button macro?

I'm looking for a solution on the best way to setup a macro for buttons that are defined in a matrix fieldtype. My current code works fine but it really isn't that DRY and I feel it could be made ...
3
votes
1answer
458 views

What is the advantages/disadvantages of separating macro imports?

In my base template extended by all other templates, I import my macros, so I have global use of them throughout my templates. Question 1: Am I taking a performance hit for including these whether or ...
2
votes
1answer
2k views

How pass an ElementModel to a Macro in Twig

I'm trying to pass an AssetModel into a twig macro but all I'm getting is: Object of class Craft\ElementCriteriaModel could not be converted to string Is there any way to define my twig macro ...
2
votes
1answer
309 views

Field name in reusable macro

I'm creating a macro I'd like to reuse multiple times but the only thing that changes is the field name (matrix). Here's an example: {% for block in entry.fieldname1 %} <h3>{{ block.title }}<...
9
votes
3answers
929 views

Is it possible to use _includes/forms outside of the Control Panel?

From working on plugin development, importing (I believe the correct term for this is macro) the 'forms' to quickly generate text areas, selects and etc. is incredibly useful. Adding the import now ...