Comments (14)
Hi matt3224,
You were so close. You can target by handle name by using:
{{ entry.logos.level(2).type('logoSingle').logoImage.first().url() }}
You could even use the type handle if appropriate instead of targeting level(2), but I wasn't sure what your exact query/data structure was.
from craft-neo.
The beauty about Craft's templating system is that there isn't one right way to do this, but I'll show you a couple of ways you could template this field.
Given a Neo field with the handle body
added to an entry:
<div class="body">
{% for block in entry.body.level(1) %}
{% switch block.type.handle %}
{% case 'text' %}
{{ block.textField }}
{% case ...
{% case 'slideshow' %}
<div class="slideshow">
{% for slide in block.children %}
<div class="slide">
...
</div>
{% endfor %}
</div>
{% endswitch %}
{% endfor %}
</div>
An alternative approach, which I typically do for larger Neo fields, is create a new template file for each block type, and dynamically include it instead of using a switch/case:
<div class="body">
{% for block in entry.body.level(1) %}
{% include 'body_blocks/' ~ block.type.handle with {block: block} only %}
{% endfor %}
</div>
I'd then create a body_blocks/
folder in my templates directory, with files text.twig
, cards.twig
, slideshow.twig
, etc.
Ultimately Neo's API is practically identical to entries and categories, and very similar to Matrix fields, since Neo uses the same underlying "Elements" system.
from craft-neo.
That did it! Crossing fingers too. ;)
from craft-neo.
Awesome! Didn't realize I could use .handle! Thanks so much. Loving the plugin, really cleans things up in the admin, even compared to SuperTable/Matrix setups.
If I have a level(1) being "row", and "row" has children like text/image/video/slideshow etc. I would then access those in a for loop as level(2), or {% for block in block.children %}, or what would be the best use case there? Essentially it's the above templating setup with another level.
from craft-neo.
No problem, glad you're liking it so far 👍
I would use something similar to the above examples. My initial loop I would just assume the blocks were all rows, then I'd use an inner loop and check block types in that. Like this:
{% for row in entry.body.level(1) %}
<div class="row">
{% for block in row.children %}
<div class="col">
{% switch block.type.handle %}
{% case 'text' %}
{{ block.textField }}
{% case ...
{% case 'slideshow' %}
<div class="slideshow">
{% for slide in block.children %}
<div class="slide">
...
</div>
{% endfor %}
</div>
{% endswitch %}
</div>
{% endfor %}
</div>
{% endfor %}
I generally wouldn't use the .level(2)
filter, as that will select all blocks on the second level, rather than blocks just inside a specific row.
from craft-neo.
Gotcha! Allright, so now for some reason I can't get the children, and the first one in the loop is a Rich Text Field named textContent, and I'm getting Internal Server Error: Craft\MatrixBlockModel and its behaviors do not have a method or closure named "textContent".
The textContent block is called as {{ block.textContent }}, and so is everything else like images being called as {{ block.imageFile.first() }}
Here's my code (fairly new to twig and Craft CMS, sorry for harassing, haha);
{% block columnStart %}
<div class="{% include '_inc/col_classes' %}">
<div class="hv-item {{ block.moveHorizontal }} {{ block.moveVertical }}" style="z-index: {{ block.layer }};">
<div class="move">
{% endblock %}
{% block columnEnd %}
</div>
</div>
</div>
{% endblock %}
{% for block in entry.post.level(1) %}
{% if block.bgColor|trim|length %}<div class="container-bg" style="background: {{ block.bgColor }};">{% endif %}
{% if block.rowType == 'normal' %}
<div class="container{% if row.fullBleed %}-fluid p-x-0{% endif %}"><div class="row">
{% elseif block.rowType == 'masonry' %}
<div class="container{% if row.fullBleed %}-fluid p-x-0{% endif %}"><div class="row packery">
{% endif %}
{% for block in row.children %}
{% switch block.type.handle %}
{% case 'text' %}
{{ block('columnStart') }}
{% include '_blocks/text' %}
{{ block('columnEnd') }}
{% case 'image' %}
{{ block('columnStart') }}
{% include '_blocks/image' %}
{{ block('columnEnd') }}
{% case 'video' %}
{{ block('columnStart') }}
{% include '_blocks/video' %}
{{ block('columnEnd') }}
{% case 'slideshow' %}
{{ block('columnStart') }}
{% include '_blocks/slideshow' %}
{{ block('columnEnd') }}
{% case 'taxonomy' %}
{% include '_blocks/entries' %}
{% case 'spacer' %}
{% include '_blocks/spacer' %}
{% case 'separator' %}
{% include '_blocks/separator' %}
{% case 'clear' %}
<div class="clearfix"></div>
{% endswitch %}
{% endfor %}
</div></div>
{% if block.bgColor|trim|length %}
</div>
{% endif %}
{% endfor %}
from craft-neo.
Ugh, sorry for being such a Craft and Twig n00b. Really can't seem to figure out the templating. Trying to port a current setup from SuperTable to Neo to give it a go, as I prefer the way it's set up in terms of not having all fields tied to just one SuperTable field, but rather reusing vanilla fields inside the Neo field (and reusing in multiple different Neo fields).
from craft-neo.
I'm not entirely sure what the issue is, however it's likely something to do with using the variable block
in multiple areas, and it's conflicting. You're using block
two loops, with one nested in the other, and that might be causing conflicts. Also, I'm not sure where your row
variable is coming from, as well as your block
macro (that might also be a problem – might be worth just avoiding using the variable name block
altogether).
from craft-neo.
Ah, gotcha. Gonna give it a go when I get a sec. So technically the for loop can be any variable (and block of course) if needed, like "item"? Would it then be possible to do a {% switch item.type.handle %}
? I was under the impression that "block" itself was somehow important in the craft twig syntax.
Currently running the SuperTable version of what I'm trying to achieve at www.23d.one, gonna see if I can get Neo to work. Really looking forward to the ability to drag items between rows.
Cheers!
from craft-neo.
So technically the for loop can be any variable (and block of course) if needed, like "item"? Would it then be possible to do a
{% switch item.type.handle %}?
Yep, that's right. There isn't anything special about Neo's API, the template examples are just using normal Twig syntax so you can use whatever variable names you want. I'd suggest using something like {% for row in entry.post.level(1) %}
and{% for item in row.children %}
.
Really looking forward to the ability to drag items between rows.
Me too haha. Fingers crossed it'll be done soon.
from craft-neo.
How can i target a specific child block handle without looping?
{% for size in entry.logos.level(2) %}
{{ size.type.handle == 'logoSingle' ? size.logoImage.first().url() ?? '' : '' }}
{% endfor %}
I thought it would be one of these but none work:
{{ entry.logos.level(2).logoSingle.logoImage.first().url() }}
{{ entry.logos.level(2).type.logoSingle.logoImage.first().url() }}
{{ entry.logos.level(2).type.handle.logoSingle.logoImage.first().url() }}
{{ entry.logos.level(2).type.handle('logoSingle').logoImage.first().url() }}
{{ entry.logos.level(2).type.handle['logoSingle'].logoImage.first().url() }}
{{ entry.logos.level(2).type['logoSingle'].logoImage.first().url() }}
{{ entry.logos.level(2)['logoSingle'].logoImage.first().url() }}
I'm pulling my hair out trying to get it to work how I want, any help would be much appreciated.
Thank you
from craft-neo.
I'm having trouble accessing any properties of the currently displayed entry from my Neo Blocks. I have an entry with a neo field in it to build page content. I am using includes to load in the code for the blocks. In one of my blocks I am trying to display a video from an entry that is related to the current entry being displayed. Here's my code:
{% set videoContent = craft.entries.section('videos').relatedTo(entry).first() %}
{% set videoThumb = videoContent.videoThumbnailImage.first() %}
{% if videoThumb != null %}
{% set thumbnailURL = videoThumb.url %}
{% else %}
{% set thumbnailURL = "" %}
{% endif %}
<div id="{{ contentBlock.type.handle }}-{{contentBlock.id}}" ></div>
<script type='text/javascript'>
jwplayer('{{ contentBlock.type.handle }}-{{contentBlock.id}}').setup({
sharing: {},
file: "{{videoContent.contentUrl}}",
image: "{{thumbnailURL}}",
width: "100%",
aspectratio: "16:9"
});
</script>
<!-- {{entry.title}} -->
When it outputs, it doesn't seem to be limiting the entries being set in Video Content o the ones that are related to the current entry. To test this I also tried to output the title for the current entry in a comment at the bottom. It doesn't output anything. Can you tell me what I am doing wrong here? Do I need to pass the entry as a variable into the include?
from craft-neo.
I'm having trouble outputting the fields within my neo field (layout). Everything is outputting as expected except for the fields i.e. block.heading and block.body. I think there must be some kind of syntax for the children here that I'm not grasping. Thanks very much in advance. I've attached a screenshot of the cp as well.
{% for row in entry.layout.level(1) %}
{% switch block.type.handle %}
{% case 'accordions' %}
<ul class="accordion accordion-2">
{% for accordions in block.children %}
<li>
<a class="opener" href="#">{{ block.heading }}</a>
<div class="slide">
{{ block.body }}
</div>
</li>
{% endfor %}
</ul>
{% endswitch %}
{% endfor %}
from craft-neo.
@fodney I guess it must be {{ accordions.heading }}
and {{ accordions.body }}
instead of {{ block.heading }}
and {{ block.body }}
because you named the expression (accordions) that way.
from craft-neo.
Related Issues (20)
- New Content will be dropped after saving HOT 4
- [Craft 5] If I miss a required field while saving, all neo blocks get flattened HOT 12
- Querying a Draft containing a Neo field with nested blocks (level 4) using Craft's Graphiql playground returns an empty result when modified HOT 4
- No owner ID assigned to the Neo block error with Solspace Calendar events HOT 2
- [Craft 5] "Save as new entry" looses all content from the new entry HOT 3
- Issue with Asset Fields Not Showing in Neo Plugin HOT 5
- Neo block does not render in a fresh commerce product HOT 1
- Class "craft\commerce\elements\Product" not found HOT 3
- Block type entry type setting: set project config name/handle/colour/field layout values to `__entrytype__` instead of replicating
- Console command for converting block type data to entry type HOT 1
- Propagation rules + Translation rules HOT 1
- Can't edit neo conditionals, getting array key 0 error HOT 7
- [5.x] Child blocks in settings don't indent until interacted with. HOT 1
- Installing Neo 5.1.2 fails on CraftCMS 5 HOT 3
- [5.x] Invalid field handle error when switching from an entry type with Neo field(s) to one without HOT 1
- Issue with Content Loss When Migrating Neo Blocks to CraftCMS Entry Types HOT 2
- New version of Craft CMS breaks NEO HOT 3
- Invalid field layout element UUID when converting to Craft matrix HOT 3
- Querying entries based on Neo field's block's sub-fields?
- Problem with Neo draft behavior HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from craft-neo.