Code Monkey home page Code Monkey logo

Comments (14)

darbymanning avatar darbymanning commented on August 15, 2024 2

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.

benjamminf avatar benjamminf commented on August 15, 2024 1

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.

23d1 avatar 23d1 commented on August 15, 2024 1

That did it! Crossing fingers too. ;)

from craft-neo.

23d1 avatar 23d1 commented on August 15, 2024

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.

benjamminf avatar benjamminf commented on August 15, 2024

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.

23d1 avatar 23d1 commented on August 15, 2024

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.

23d1 avatar 23d1 commented on August 15, 2024

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.

benjamminf avatar benjamminf commented on August 15, 2024

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.

23d1 avatar 23d1 commented on August 15, 2024

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.

benjamminf avatar benjamminf commented on August 15, 2024

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.

mattpilott avatar mattpilott commented on August 15, 2024

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.

brianrivet avatar brianrivet commented on August 15, 2024

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.

fodney avatar fodney commented on August 15, 2024

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 %}

screen shot 2017-10-03 at 5 49 18 pm
`

from craft-neo.

klick avatar klick commented on August 15, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.