Hot answers tagged

14

Twig variables are only accessible inside their own scope. . What is a scope: Tags like {% block %}, {% for %}, {% nav %} create a 'scope', which means that variables defined inside them, can't be accessed from outside. This is similar to local variables inside php-functions: {% block A %} {% set var = 'apple' %} {{ var }} {# This prints apple #} ...


13

block.type is actually an alias for a getType() function, which returns a new MatrixBlockTypeModel object. Those will output their handle by default, which is why it's possible to compare block.type against block type handles. But MatrixBlockTypeModel also has a handle property, so this is totally possible: {{ block.type.handle }}


6

This is a great chance to use the group filter. First, get all your matrix blocks as an array: {% set talkBlocks = entry.talks.find() %} Then group them by type: {% set talksByCategories = talkBlocks|group('type') %} At this point, you have an associative array where the keys are the block types, and the values are arrays of the blocks with that type. ...


6

Assuming the field where your client can select the download is an asset field, you can use the following: {% elseif block.type == "bladeBrochure" %} {% set download = block.download.first() %} {% if download %} <a href="{{ download.url }}">Click me to download</a> {% endif %} Replacing block.download with the name of your ...


5

I'm assuming that your PDF field is an assets field? If so, it works exactly the same way as images, and you can output it using something like this: {% for block in entry.matrixFieldHandle %} {% set pdf = block.pdfFieldHandle.first() %} <a href="{{pdf.getUrl}}">Text</a> {% endfor %}`


5

What about comparing the currently looped block ID with the ID of the last block of type "text"? _entry.html: {% set lastTextBlock = entry.simpleMatrix.type('text').last() %} {% set lastTextBlockId = lastTextBlock ? lastTextBlock.id : null %} {% for block in entry.contentBlocks %} {% include '_blocks/' ~ block.type|lower %} {% endfor %} _blocks/text....


5

I just published a plugin called Limit Block Type, which adds the ability to limit Matrix blocks per block type. There's also a feature request for adding this feature to core.


4

It seems to be what you need is Twigs extend functionality. What you have here is a very common template structure so there is nothing out of the ordinary! Firstly, going from your code I'd recommend your main layout template file looks something like this. The idea here being that these elements will not change for each page, but the content or information ...


4

You could try something like this: {% set block = post.buildStory.type('textEditorBlock').first() %} {{block.textEditor}}


4

I believe it is possible @jack. Take a look at this working demo - http://twigfiddle.com/9rwjeo Update: On closer inspection the original example did not work. You can not have a block inside an include how you are describing. I have update the twig fiddle with a work around though


3

Unfortunately, there's no way around having the {% block %} tag somewhere in the template that's being extended. In other words, your main layout template it's going to have {% block %} tags. However, you can use the {% embed %} tag to "wrap" a block in a third template (e.g. "components/main-wrapper.html"): {# base.html #} <body> {% block main %}...


3

I believe you're referring to Matrix block types. You can create additional block types for a Matrix field, or edit existing ones by going to that Matrix's field settings under Settings->Fields->(your matrix field) in the control panel.


3

This article may help as well: Twig Processing Order & Scope


3

From your last update it looks like you simply need to get the entry that contains that matrix block, the error Variable "entry" doesn't exist simply means nothing is assigned to entry in your templates. There are a couple of ways you can solve this: Option 1 - Tell Craft which template to load for this entry If you go into your section settings, there ...


3

This is because Craft stores the sortOrder values in the database with a MySQL column type of tinyint that happens to be signed, which means it can store a range of -128 to 127. Aside from the fact that negative sort orders don't make much sense, that means if you have 127 or more Matrix blocks to sort, things will stop working as expected. Because 127 ...


2

thanks a lot for all the answers! I finally got my entries to show up! What I did was move that entry to Homepage section(like what Alec Ritson suggested) together with other entry I already have in Homepage and wrapped the block code with {% for entry in craft.entries.section('homepage') %} So the full code is: {% for entry in craft.entries.section('...


2

I would try this: {% for block in entry.bannerMatrix %} <div> {% set blockImage = block.image.first %} {% if blockImage %} <img src="{{ blockImage.getUrl() }}" alt="{{ blockImage.filename }}"> {% endif %} <div class="hero_msg"> <h3>{{ block.heading }}</h3> <h4>{{ block.subheading }}</...


2

Hi Sylvia and welcome to Craft. You are almost there with your markup, there are only 2 little things I can spot straight away: Line 4 you are calling {{ image.title }} however the image variable doesn't exist anywhere from what I can see in your code. In Line 1 you are looping through your Matrix field with a block variable: {% for block in ... %}, so this ...


2

The question is a little unclear, is this what you are looking for? {% set requiredProgram = false %} {% for block in entry.workshopInstructor %} {% if block.requiredPrograms.contains('Photoshop')%} {% set requiredProgram = true %} {% endif %} {% endfor %} {% if requiredProgram %} Photoshop is enabled on one of this entry's blocks field. {% ...


2

Anything that is defined in index.twig outside of a block will be available in skeleton.twig, and so also in template.twig. This should work: skeleton.twig {% include '_includes/template' %} index.twig {% extends "skeleton" %} {# Set parameter #} {% set param = { section: ['handle1', 'handle2'] } %}


2

Reading your description I see no reason to not use all three fields in a single matrix block. If you need additional control (e.g. for positioning the title), you could add another field to that block that allows you to set your settings (e.g. a dropdown with "Title left" / "Title right" options). If you really need a per block type limit (now), you could ...


2

It does not appear to be in the matrix block model. http://buildwithcraft.com/docs/templating/matrixblockmodel


2

What about using the length filter on a type filtered array of block models to get the total number of your "text" blocks. And then combine that with a counter in your block include to conditionally output the buttons? _entry.html: {% set lastTextBlock = entry.contentBlocks.type('text')|length %} {% set loopIndexText = 0 %} {% for block in entry....


2

You have two main options with a challenge like this. 1) Do it manually - While this option doesn't always sound like the most glamourous option, it's often a fairly reasonable step to consider during migrations. Writing and debugging advanced code that is only going to be used once can be costly, and there are plenty of people who specialize in data entry ...


2

You can check against the Entry Type as well. I would try to validate it with something like: {% set exclusions = ['home'] %} {% if entry.type not in exclusions %} ....


2

So what you're running into is Twig scoping rules, which is discussed at length in the article Twig Processing Order & Scope The TL;DR is that a variable is only available in the context in which it is defined. Since you're setting the meetings variable in a file you included, that variable exists only in the context of the anonVisitor.logic file. ...


2

This question comes up in support tickets every so often from client users. We like to say that Craft fills the space between where WordPress ends and Enterprise begins. It's meant for designers and developers to build bespoke websites for clients. That would include Squarespace-like website building services as well. If your design, technical, and ...


2

Unfortunately it’s not possible for an included template to override the contents of a block in the parent template, or its layout. You’d need to just override the seo block from page.twig do whatever you need to do from there instead. {% extends 'layout.twig' %} {% block main %} some content.. {% include 'pagination.twig' %} {% endblock %} {% block ...


2

If partial_with_form.twig isn’t making any of its own customizations to form.twig, then I’d just include the form.twig template using an {% include %} tag, and wrap that whole thing with a {% block %}, so if a template wants to include the partial and make changes to the form, they can just redefine the form themselves. partial_with_form.twig: {% block ...


1

There is a currentUser global variable that is available to all Twig templates. If there is a currently logged in user, it will return that user's UserModel class. From there, it's just a matter of: {% if currentUser %} form=http://formsite.com/formname?email={{ currentUser.email }} {% endif %} Of course, what's inside the if block will look ...


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